|
| 1 | +// |
| 2 | +// QNNetworkStatusTest.m |
| 3 | +// QiniuSDK |
| 4 | +// |
| 5 | +// Created by yangsen on 2022/6/10. |
| 6 | +// Copyright © 2022 Qiniu. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +#import <XCTest/XCTest.h> |
| 10 | +#import "QNNetworkStatusManager.h" |
| 11 | +#import "QNUploadServerNetworkStatus.h" |
| 12 | + |
| 13 | +@interface QNNetworkStatusTest : XCTestCase |
| 14 | + |
| 15 | +@end |
| 16 | + |
| 17 | +@implementation QNNetworkStatusTest |
| 18 | + |
| 19 | +- (void)setUp { |
| 20 | + // Put setup code here. This method is called before the invocation of each test method in the class. |
| 21 | +} |
| 22 | + |
| 23 | +- (void)tearDown { |
| 24 | + // Put teardown code here. This method is called after the invocation of each test method in the class. |
| 25 | +} |
| 26 | + |
| 27 | +- (void)testGetNetworkStatus { |
| 28 | + NSString *host = @"qiniu.com"; |
| 29 | + [kQNNetworkStatusManager updateNetworkStatus:host speed:100]; |
| 30 | + QNNetworkStatus *status = [kQNNetworkStatusManager getNetworkStatus:host]; |
| 31 | + XCTAssertEqual(status.speed, 100, @"getNetworkStatus error"); |
| 32 | +} |
| 33 | + |
| 34 | +- (void)testNetworkStatusUpdate { |
| 35 | + NSString *host00 = @"qiniu.com"; |
| 36 | + NSString *host01 = @"qiniu01.com"; |
| 37 | + |
| 38 | + for (int i=0; i<100000; i++) { |
| 39 | + [kQNNetworkStatusManager updateNetworkStatus:host00 speed:100]; |
| 40 | + [kQNNetworkStatusManager updateNetworkStatus:host01 speed:1000]; |
| 41 | + } |
| 42 | + |
| 43 | + QNNetworkStatus *status00 = [kQNNetworkStatusManager getNetworkStatus:host00]; |
| 44 | + XCTAssertEqual(status00.speed, 100, @"getNetworkStatus error"); |
| 45 | + QNNetworkStatus *status01 = [kQNNetworkStatusManager getNetworkStatus:host01]; |
| 46 | + XCTAssertEqual(status01.speed, 1000, @"getNetworkStatus error"); |
| 47 | +} |
| 48 | + |
| 49 | +- (void)testNetworkStatusCompare { |
| 50 | + NSString *host00 = @"qiniu.com"; |
| 51 | + NSString *host01 = @"qiniu01.com"; |
| 52 | + [kQNNetworkStatusManager updateNetworkStatus:host00 speed:100]; |
| 53 | + [kQNNetworkStatusManager updateNetworkStatus:host01 speed:1000]; |
| 54 | + |
| 55 | + QNUploadServer *server00 = [QNUploadServer server:host00 ip:@"" source:@"c" ipPrefetchedTime:nil]; |
| 56 | + QNUploadServer *server01 = [QNUploadServer server:host01 ip:@"" source:@"c" ipPrefetchedTime:nil]; |
| 57 | + NSLog(@"==== start compare"); |
| 58 | + |
| 59 | + dispatch_group_t group = dispatch_group_create(); |
| 60 | + dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ |
| 61 | + for (int i=0; i<100000; i++) { |
| 62 | + NSString *host = [[NSString alloc]initWithFormat:@"qiniu%2d.com", i]; |
| 63 | + [kQNNetworkStatusManager updateNetworkStatus:host speed:100]; |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ |
| 68 | + dispatch_group_enter(group); |
| 69 | + for (int i=0; i<100000; i++) { |
| 70 | + [QNUploadServerNetworkStatus isServerNetworkBetter:server00 thanServerB:server01]; |
| 71 | + } |
| 72 | + dispatch_group_leave(group); |
| 73 | + }); |
| 74 | + |
| 75 | + dispatch_group_async(group, dispatch_get_global_queue(0, 0), ^{ |
| 76 | + for (int i=0; i<100000; i++) { |
| 77 | + [kQNNetworkStatusManager performSelector:@selector(recoverNetworkStatusFromDisk)]; |
| 78 | + } |
| 79 | + }); |
| 80 | + |
| 81 | + |
| 82 | + dispatch_group_wait(group, dispatch_time(DISPATCH_TIME_NOW, 20 * NSEC_PER_SEC)); |
| 83 | +} |
| 84 | + |
| 85 | +@end |
0 commit comments