Created
March 27, 2012 09:45
-
-
Save n-b/2214443 to your computer and use it in GitHub Desktop.
A minimal test for a NSNetService bug (rdar://11018654). The TXTRecordData property is never updated.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// clang main.m -framework Foundation && ./a.out | |
#import <Foundation/Foundation.h> | |
/****************************************************************************/ | |
#pragma mark - | |
@interface Advertiser : NSObject | |
@end | |
@implementation Advertiser | |
{ | |
NSNetService * _service; | |
} | |
- (void) advertise | |
{ | |
_service = [[NSNetService alloc] initWithDomain:@"local." type:@"_netservicetest._tcp." name:@"advertisetest" port:12345]; | |
[_service publish]; | |
[self updateTXT]; | |
} | |
- (void) updateTXT | |
{ | |
NSString * str = [NSString stringWithFormat:@"%d",(int)[NSDate timeIntervalSinceReferenceDate]]; | |
NSDictionary * txt = [NSDictionary dictionaryWithObject:str forKey:@"t"]; | |
NSLog(@"Advertiser: Updating TXT : %@",str); | |
_service.TXTRecordData = [NSNetService dataFromTXTRecordDictionary:txt]; | |
[self performSelector:_cmd withObject:nil afterDelay:5]; | |
} | |
@end | |
/****************************************************************************/ | |
#pragma mark - | |
@interface Browser : NSObject <NSNetServiceDelegate> | |
@end | |
@implementation Browser | |
{ | |
NSNetService * _service; | |
} | |
- (void) browse | |
{ | |
_service = [[NSNetService alloc] initWithDomain:@"local." type:@"_netservicetest._tcp." name:@"advertisetest"]; | |
_service.delegate = self; | |
[_service startMonitoring]; | |
// [_service resolveWithTimeout:10]; // Removing the resolve makes _service.TXTRecordData work correctly. However, not resolving services is kinda useless. | |
} | |
- (void)netService:(NSNetService *)sender didUpdateTXTRecordData:(NSData *)data | |
{ | |
NSAssert(sender==_service,@"WTF"); | |
NSString * newStr = [[NSString alloc] initWithData:[[NSNetService dictionaryFromTXTRecordData:data] objectForKey:@"t"] encoding:NSUTF8StringEncoding]; | |
NSString * oldStr = [[NSString alloc] initWithData:[[NSNetService dictionaryFromTXTRecordData:_service.TXTRecordData] objectForKey:@"t"] encoding:NSUTF8StringEncoding]; | |
NSLog(@" Browser: didUpdateTXT : %@",newStr); | |
NSLog(@" _service.TXTRecordData : %@%@",oldStr,[newStr isEqualToString:oldStr]?@"":@" <- WRONG"); | |
} | |
@end | |
/****************************************************************************/ | |
#pragma mark - | |
int main(int argc, const char * argv[]) | |
{ | |
@autoreleasepool { | |
Advertiser * advertiser = [Advertiser new]; | |
[advertiser advertise]; | |
Browser * browser = [Browser new]; | |
[browser browse]; | |
[[NSRunLoop currentRunLoop] run]; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
~/gist-2214443 >clang main.m -framework Foundation && ./a.out | |
2012-03-27 11:46:57.579 a.out[5282:707] Advertiser: Updating TXT : 354534417 | |
2012-03-27 11:46:57.608 a.out[5282:707] Browser: didUpdateTXT : 354534346 | |
2012-03-27 11:46:57.609 a.out[5282:707] _service.TXTRecordData : 354534346 | |
2012-03-27 11:46:59.535 a.out[5282:707] Browser: didUpdateTXT : 354534417 | |
2012-03-27 11:46:59.536 a.out[5282:707] _service.TXTRecordData : 354534417 | |
2012-03-27 11:47:02.582 a.out[5282:707] Advertiser: Updating TXT : 354534422 | |
2012-03-27 11:47:03.583 a.out[5282:707] Browser: didUpdateTXT : 354534422 | |
2012-03-27 11:47:03.583 a.out[5282:707] _service.TXTRecordData : 354534422 | |
2012-03-27 11:47:07.583 a.out[5282:707] Advertiser: Updating TXT : 354534427 | |
2012-03-27 11:47:08.584 a.out[5282:707] Browser: didUpdateTXT : 354534427 | |
2012-03-27 11:47:08.584 a.out[5282:707] _service.TXTRecordData : 354534422 | |
2012-03-27 11:47:12.584 a.out[5282:707] Advertiser: Updating TXT : 354534432 | |
2012-03-27 11:47:13.585 a.out[5282:707] Browser: didUpdateTXT : 354534432 | |
2012-03-27 11:47:13.586 a.out[5282:707] _service.TXTRecordData : 354534422 | |
2012-03-27 11:47:17.586 a.out[5282:707] Advertiser: Updating TXT : 354534437 | |
2012-03-27 11:47:18.587 a.out[5282:707] Browser: didUpdateTXT : 354534437 | |
2012-03-27 11:47:18.588 a.out[5282:707] _service.TXTRecordData : 354534422 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
have u found a workaround?