diff --git a/Example/SBTUITestTunnel_Tests/Info.plist b/Example/SBTUITestTunnel_Tests/Info.plist index 8fb12470..e54c7f3c 100644 --- a/Example/SBTUITestTunnel_Tests/Info.plist +++ b/Example/SBTUITestTunnel_Tests/Info.plist @@ -21,6 +21,6 @@ CFBundleVersion 1 SBTUITestTunnelDisableIPC - + diff --git a/Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m b/Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m index 2834c75f..7c10e8e9 100755 --- a/Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m +++ b/Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m @@ -249,6 +249,15 @@ - (void)waitForConnection - (void)serverDidConnect:(id)sender { + while (!self.ipcConnection.isValid) { + if (CFAbsoluteTimeGetCurrent() - self.launchStart > SBTUITunneledApplicationDefaultTimeout) { + [self shutDownWithErrorMessage:@"[SBTUITestTunnel] IPC tunnel did fail to connect" code:SBTUITestTunnelErrorConnectionToApplicationFailed]; + return; + } + + [NSRunLoop.mainRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]]; + } + __weak typeof(self)weakSelf = self; dispatch_async(dispatch_get_main_queue(), ^{ weakSelf.connected = YES; diff --git a/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.h b/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.h index 4c56c015..7249d3a2 100644 --- a/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.h +++ b/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.h @@ -76,6 +76,8 @@ NS_ASSUME_NONNULL_BEGIN /// Invalidate the connection. All outstanding error handling blocks will be called on the message handling queue. The connection must be invalidated before it is deallocated. After a connection is invalidated, no more messages may be sent or received. - (void)invalidate; +- (BOOL)isValid; + @end NS_ASSUME_NONNULL_END diff --git a/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m b/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m index 9e666ebd..c05677d1 100644 --- a/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m +++ b/Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m @@ -184,6 +184,8 @@ - (NSMethodSignature *)protocolMethodSignatureForSelector:(SEL)aSelector @implementation DTXIPCConnection { dispatch_queue_t _dispatchQueue; + dispatch_queue_t _otherConnectionQueue; + NSRunLoop* _runLoop; NSString* _actualServiceName; @@ -224,7 +226,8 @@ - (instancetype)initWithServiceName:(NSString *)serviceName _slave = NO; _dispatchQueue = dispatch_queue_create([NSString stringWithFormat:@"com.wix.DTXIPCConnection:%@", _serviceName].UTF8String, dispatch_queue_attr_make_with_autorelease_frequency(NULL, DISPATCH_AUTORELEASE_FREQUENCY_WORK_ITEM)); - + _otherConnectionQueue = dispatch_queue_create([NSString stringWithFormat:@"com.wix.DTXIPCOtherConnection:%@", _serviceName].UTF8String, NULL); + //Attempt becoming a master if([self _commonInit] == NO) { @@ -294,7 +297,12 @@ - (void)invalidate - (BOOL)isValid { - return _connection.isValid && _otherConnection.isValid; + __block BOOL ret = NO; + dispatch_sync(_otherConnectionQueue, ^{ + ret = _connection.isValid && _otherConnection.isValid; + }); + + return ret; } - (id)remoteObjectProxy @@ -323,8 +331,10 @@ - (void)setExportedObject:(id)exportedObject - (oneway void)_slaveDidConnectWithName:(NSString*)slaveServiceName { - _otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil]; - [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:_otherConnection]; + dispatch_sync(_otherConnectionQueue, ^{ + _otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil]; + [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:_otherConnection]; + }); } - (oneway void)_invokeFromRemote:(NSDictionary*)serializedInvocation diff --git a/Sources/SBTUITestTunnelServer/SBTUITestTunnelServer.m b/Sources/SBTUITestTunnelServer/SBTUITestTunnelServer.m index 6617cfb7..ac647e7d 100644 --- a/Sources/SBTUITestTunnelServer/SBTUITestTunnelServer.m +++ b/Sources/SBTUITestTunnelServer/SBTUITestTunnelServer.m @@ -157,6 +157,7 @@ - (BOOL)takeOffOnce - (BOOL)takeOffOnceIPCWithServiceIdentifier:(NSString *)serviceIdentifier { self.ipcConnection = [[DTXIPCConnection alloc] initWithServiceName:[NSString stringWithFormat:@"com.subito.sbtuitesttunnel.ipc.%@", serviceIdentifier]]; + self.ipcConnection.remoteObjectInterface = [DTXIPCInterface interfaceWithProtocol:@protocol(SBTIPCTunnel)]; self.ipcConnection.exportedInterface = [DTXIPCInterface interfaceWithProtocol:@protocol(SBTIPCTunnel)]; self.ipcConnection.exportedObject = self;