Skip to content

Commit

Permalink
WIP: More logs and hop threads
Browse files Browse the repository at this point in the history
  • Loading branch information
alvar-bolt committed May 17, 2024
1 parent c01a677 commit e96bbc4
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
54 changes: 45 additions & 9 deletions Sources/SBTUITestTunnelClient/SBTUITestTunnelClient.m
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,14 @@ - (void)launchTunnelWithStartupBlock:(void (^)(void))startupBlock
[self.ipcConnection resume];

self.ipcProxy = [self.ipcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
[self shutDownWithErrorMessage:[NSString stringWithFormat:@"[SBTUITestTunnelClient] Failed getting IPC proxy, %@", error.description] code:SBTUITestTunnelErrorLaunchFailed];
[self shutDownWithErrorMessage:[
NSString stringWithFormat:@"[SBTUITestTunnelClient] Failed getting IPC proxy, %@ %@",
error.description,
self.ipcConnection
] code:SBTUITestTunnelErrorLaunchFailed];
}];


NSLog(@"[SBTUITestTunnel] Resolving IPC connection with service identifier %@", serviceIdentifier);
launchEnvironment[SBTUITunneledApplicationLaunchEnvironmentIPCKey] = serviceIdentifier;
self.application.launchEnvironment = launchEnvironment;
} else {
Expand Down Expand Up @@ -249,21 +254,45 @@ - (void)waitForConnection

- (void)serverDidConnect:(id)sender
{
NSLog(@"[SBTUITestTunnel] %s - %@", __PRETTY_FUNCTION__, [NSThread currentThread]);
__weak typeof(self)weakSelf = self;
dispatch_async(dispatch_get_main_queue(), ^{
weakSelf.connected = YES;

NSLog(@"[SBTUITestTunnel] IPC tunnel did connect after, %fs", CFAbsoluteTimeGetCurrent() - weakSelf.launchStart);
NSLog(@"[SBTUITestTunnel] IPC tunnel did connect after, %fs - %@", CFAbsoluteTimeGetCurrent() - weakSelf.launchStart, [NSThread currentThread]);

[weakSelf checkConnectionAndProceed];
// NSLog(@"[SBTUITestTunnel] IPC connection is valid: %d", weakSelf.ipcConnection.isValid);

// if (weakSelf.startupBlock) {
// weakSelf.startupBlock();
// NSLog(@"[SBTUITestTunnel] Did perform startupBlock");
// }

// weakSelf.startupCompleted = [[weakSelf sendSynchronousRequestWithPath:SBTUITunneledApplicationCommandStartupCommandsCompleted params:@{}] isEqualToString:@"YES"];

// NSLog(@"[SBTUITestTunnel] Tunnel (2) ready after %fs", CFAbsoluteTimeGetCurrent() - weakSelf.launchStart);
});
}

if (weakSelf.startupBlock) {
weakSelf.startupBlock();
// check if connection is valid and proceed to startup. if not, check again in next run loop
- (void)checkConnectionAndProceed
{
if (self.ipcConnection.isValid) {
if (_startupBlock) {
_startupBlock();
NSLog(@"[SBTUITestTunnel] Did perform startupBlock");
}

weakSelf.startupCompleted = [[weakSelf sendSynchronousRequestWithPath:SBTUITunneledApplicationCommandStartupCommandsCompleted params:@{}] isEqualToString:@"YES"];
_startupCompleted = [[self sendSynchronousRequestWithPath:SBTUITunneledApplicationCommandStartupCommandsCompleted params:@{}] isEqualToString:@"YES"];

NSLog(@"[SBTUITestTunnel] Tunnel ready after %fs", CFAbsoluteTimeGetCurrent() - weakSelf.launchStart);
});
NSLog(@"[SBTUITestTunnel] Tunnel (2) ready after %fs", CFAbsoluteTimeGetCurrent() - _launchStart);
} else {
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"[SBTUITestTunnel] IPC connection is not valid, retrying in next run loop");
[self checkConnectionAndProceed];
});
}
}

- (void)performCommandWithParameters:(NSDictionary *)parameters block:(void (^)(NSDictionary *))block {}
Expand Down Expand Up @@ -916,6 +945,7 @@ - (NSString *)base64SerializeData:(NSData *)data
- (NSString *)sendSynchronousRequestWithPath:(NSString *)path params:(NSDictionary<NSString *, NSString *> *)params assertOnError:(BOOL)assertOnError
{
if (self.ipcConnection) {
NSLog(@"[SBTUITestTunnel] Sending IPC request(%d): %@ %@", self.connected, path, self.ipcConnection);
if (!self.connected) {
return @"";
}
Expand All @@ -929,9 +959,12 @@ - (NSString *)sendSynchronousRequestWithPath:(NSString *)path params:(NSDictiona

NSLock *lock = [[NSLock alloc] init];

NSLog(@"[SBTUITestTunnel] Sending IPC request(%d) on background thread: %@", self.connected, path);
dispatch_async(dispatch_get_main_queue(), ^{
NSLog(@"[SBTUITestTunnel] Sending IPC request on main thread: %@", path);
ret = [weakSelf sendSynchronousRequestWithPath:path params:params assertOnError:assertOnError];

NSLog(@"[SBTUITestTunnel] Received IPC response on background thread: %@", path);
[lock lock];
lockedDone = YES;
[lock unlock];
Expand All @@ -952,14 +985,16 @@ - (NSString *)sendSynchronousRequestWithPath:(NSString *)path params:(NSDictiona

NSMutableDictionary *ipcParams = [(params ?: @{}) mutableCopy];
ipcParams[SBTUITunnelIPCCommand] = path;


NSLog(@"[SBTUITestTunnel] Sending IPC request(%d): %@", self.connected, path);
__block NSDictionary *ret = nil;
[self.ipcProxy performCommandWithParameters:ipcParams block:^void(NSDictionary *dict) {
ret = dict;
}];

return ret[SBTUITunnelResponseResultKey];
} else if (self.connectionlessBlock) {
NSLog(@"[SBTUITestTunnel] Sending connectionless request: %@", path);
if ([NSThread isMainThread]) {
return self.connectionlessBlock(path, params);
} else {
Expand All @@ -971,6 +1006,7 @@ - (NSString *)sendSynchronousRequestWithPath:(NSString *)path params:(NSDictiona
return ret;
}
} else if (self.connectionPort == 0) {
NSLog(@"[SBTUITestTunnel] Failed to send request: %@", path);
return nil; // connection still not established
}

Expand Down
18 changes: 16 additions & 2 deletions Sources/SBTUITestTunnelCommon/DetoxIPC/DTXIPCConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,22 @@ - (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];
NSLog(@"[DTXIPCConnection] %s - %@", __PRETTY_FUNCTION__, [NSThread currentThread]);

dispatch_block_t block = ^{
NSLog(@"[DTXIPCConnection] %s 2 - %@", __PRETTY_FUNCTION__, [NSThread currentThread]);
self->_otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil];
[NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:self->_otherConnection];
};

if ([NSThread isMainThread]) {
block();
} else {
dispatch_async(dispatch_get_main_queue(), block);
}

// _otherConnection = [NSConnection connectionWithRegisteredName:slaveServiceName host:nil];
// [NSNotificationCenter.defaultCenter addObserver:self selector:@selector(_otherConnectionDidDie:) name:NSConnectionDidDieNotification object:_otherConnection];
}

- (oneway void)_invokeFromRemote:(NSDictionary*)serializedInvocation
Expand Down
10 changes: 9 additions & 1 deletion Sources/SBTUITestTunnelServer/SBTUITestTunnelServer.m
Original file line number Diff line number Diff line change
Expand Up @@ -156,27 +156,34 @@ - (BOOL)takeOffOnce

- (BOOL)takeOffOnceIPCWithServiceIdentifier:(NSString *)serviceIdentifier
{
NSLog(@"[SBTUITestTunnel] Starting IPC tunnel");
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;

NSLog(@"[SBTUITestTunnel] Resuming IPC connection");
[self.ipcConnection resume];

NSLog(@"[SBTUITestTunnel] Getting IPC proxy");
self.ipcProxy = [self.ipcConnection synchronousRemoteObjectProxyWithErrorHandler:^(NSError * _Nonnull error) {
BlockAssert(NO, @"[UITestTunnelServer] Failed getting IPC proxy");
BlockAssert(NO, @"[UITestTunnelServer] Failed getting IPC proxy (2)");
}];

NSLog(@"[SBTUITestTunnel] Notifying IPC proxy of connection");
[self.ipcProxy serverDidConnect:nil];

NSLog(@"[SBTUITestTunnel] Processing launch options");
[self processLaunchOptionsIfNeeded];

NSLog(@"[SBTUITestTunnel] Checking for startup semaphore");
if (![[NSProcessInfo processInfo].arguments containsObject:SBTUITunneledApplicationLaunchSignal]) {
NSLog(@"[SBTUITestTunnel] Signal launch option missing, safely landing!");
return NO;
}

NSAssert([NSThread isMainThread], @"We synch startupCompleted on main thread");
NSLog(@"[SBTUITestTunnel] Waiting for startup semaphore");
NSTimeInterval start = CFAbsoluteTimeGetCurrent();
while (CFAbsoluteTimeGetCurrent() - start < SBTUITunneledServerDefaultTimeout) {
[NSRunLoop.mainRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.5]];
Expand Down Expand Up @@ -820,6 +827,7 @@ - (NSDictionary *)commandSetUIAnimationSpeed:(NSDictionary *)parameters

- (NSDictionary *)commandStartupCompleted:(NSDictionary *)parameters
{
NSLog(@"[SBTUITestTunnel] Startup completed");
__weak typeof(self)weakSelf = self;

dispatch_async(dispatch_get_main_queue(), ^{
Expand Down

0 comments on commit e96bbc4

Please sign in to comment.