@@ -27,12 +27,18 @@ @implementation InetAddress
2727@end
2828
2929#define CustomIPValue @" 192.168.1.1"
30+ #define kCustomHost @" uplog.qbox.me"
31+ #define kDnsTestToken token_na0
32+
3033@interface CustomDns : NSObject <QNDnsDelegate>
3134@property (nonatomic , assign )BOOL isTestTtl;
3235@end
3336@implementation CustomDns
3437
3538- (NSArray <id<QNIDnsNetworkAddress>> *)lookup : (NSString *)host {
39+ if (![host isEqualToString: kCustomHost ]) {
40+ return nil ;
41+ }
3642
3743 InetAddress *inetAddress = [[InetAddress alloc ] init ];
3844 inetAddress.hostValue = host;
@@ -56,8 +62,6 @@ @interface QNDnsPrefetcherTest : XCTestCase
5662
5763@implementation QNDnsPrefetcherTest
5864
59- #define kCustomHost @" uplog.qbox.me"
60- #define kDnsTestToken token_na0
6165- (void )setUp {
6266
6367 [kQNTransactionManager destroyResource ];
@@ -73,7 +77,10 @@ - (void)tearDown {
7377}
7478
7579- (void )testLocalLoad {
80+ [self testPreFetch ];
7681
82+ kQNGlobalConfiguration .dns = [[CustomDns alloc ] init ];
83+ kQNGlobalConfiguration .isDnsOpen = YES ;
7784 [kQNTransactionManager addDnsLocalLoadTransaction ];
7885
7986 AGWW_WAIT_WHILE ([kQNDnsPrefetch getInetAddressByHost: kCustomHost ] == nil , 60 * 5 );
@@ -156,4 +163,41 @@ - (void)testMutiThreadPrefetch{
156163 QN_TEST_CASE_WAIT_TIME (2 );
157164}
158165
166+ - (void )testClearCache {
167+ [kQNTransactionManager destroyResource ];
168+ QN_TEST_CASE_WAIT_TIME (2 );
169+
170+ CustomDns *dns = [[CustomDns alloc ] init ];
171+ dns.isTestTtl = YES ;
172+ kQNGlobalConfiguration .dns = dns;
173+ NSString *host = @" uplog.qbox.me" ;
174+ dispatch_group_t group = dispatch_group_create ();
175+
176+ int times = 10 ;
177+ dispatch_group_async (group, dispatch_get_global_queue (0 , 0 ), ^{
178+ dispatch_group_enter (group);
179+ [kQNDnsPrefetch prefetchHostBySafeDns: host error: nil ];
180+ for (int i=0 ; i<times; i++) {
181+ for (int i=0 ; i<times; i++) {
182+ [kQNDnsPrefetch prefetchHostBySafeDns: [NSString stringWithFormat: @" %d %@ " , i, host] error: nil ];
183+ }
184+ [NSThread sleepForTimeInterval: 0.0001 ];
185+ }
186+ dispatch_group_leave (group);
187+ });
188+
189+ dispatch_group_async (group, dispatch_get_global_queue (0 , 0 ), ^{
190+ dispatch_group_enter (group);
191+ for (int i=0 ; i<times; i++) {
192+ [kQNDnsPrefetch clearDnsCache: nil ];
193+ [NSThread sleepForTimeInterval: 0.001 ];
194+ }
195+ dispatch_group_leave (group);
196+ });
197+
198+ dispatch_group_wait (group, dispatch_time (DISPATCH_TIME_NOW, 60 * NSEC_PER_SEC));
199+
200+ QN_TEST_CASE_WAIT_TIME (2 );
201+ }
202+
159203@end
0 commit comments