Skip to content

Commit

Permalink
fix possible race condition
Browse files Browse the repository at this point in the history
Before, PatrolServer.appReady was set once to true during the initial run, and it
stayed like that in subsequent runs. This was a race conditions, and a temporary
solution with 1 sec timeout was applied.

Now the timeout is removed, and the logic should be fixed.
  • Loading branch information
bartekpacia committed Oct 24, 2023
1 parent 5ba8427 commit 86715a6
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
8 changes: 7 additions & 1 deletion packages/patrol/example/ios/RunnerUITests/RunnerUITests.m
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,18 @@ @implementation __test_class
/* Step 1 - dynamically create test cases */

IMP implementation = imp_implementationWithBlock(^(id _self) {
/* Reset server's appReady state, because new app process will be started */
server.appReady = NO;

XCUIApplication *app = [[XCUIApplication alloc] init];
NSDictionary *args = @{ @"PATROL_INITIAL_RUN" : @"false" };
[app setLaunchEnvironment:args];
[app launch];

// TODO: wait for patrolAppService to be ready
/* Spin the runloop waiting until the app reports that PatrolAppService is up */
while (!server.appReady) {
[NSRunLoop.currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:1.0]];
}

__block BOOL callbacksSet = NO;
[appServiceClient setDartLifecycleCallbacksState:callbacksState completion:^(NSError * _Nullable err) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import Telegraph
#endif

@objc
public private(set) var appReady = false
public var appReady = false

private let onLifecycleCallbackExecuted: (String) -> Void

Expand Down
47 changes: 20 additions & 27 deletions packages/patrol/ios/Classes/ObjCPatrolAppServiceClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,40 +65,33 @@
) {
NSLog("PatrolAppService.setDartLifecycleCallbacksState(\(state)")

DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
let request = SetLifecycleCallbacksStateRequest(state: state)
self.client.setLifecycleCallbacksState(request: request) { result in
switch result {
case .success(_):
completion(nil)
case .failure(let error):
completion(error)
}
let request = SetLifecycleCallbacksStateRequest(state: state)
self.client.setLifecycleCallbacksState(request: request) { result in
switch result {
case .success(_):
completion(nil)
case .failure(let error):
completion(error)
}
}
}

@objc public func runDartTest(
_ name: String, completion: @escaping (ObjCRunDartTestResponse?, Error?) -> Void
) {
// TODO: simple workaround - patrolAppService starts running too slowly.
// We should wait for appReady in the dynamically created test case method,
// before calling runDartTest() (in PATROL_INTEGRATION_TEST_IOS_MACRO)
DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
NSLog("PatrolAppServiceClient.runDartTest(\(name))")

let request = RunDartTestRequest(name: name)
self.client.runDartTest(request: request) { result in
switch result {
case .success(let result):
let testRespone = ObjCRunDartTestResponse(
passed: result.result == .success,
details: result.details
)
completion(testRespone, nil)
case .failure(let error):
completion(nil, error)
}
NSLog("PatrolAppServiceClient.runDartTest(\(name))")

let request = RunDartTestRequest(name: name)
self.client.runDartTest(request: request) { result in
switch result {
case .success(let result):
let testRespone = ObjCRunDartTestResponse(
passed: result.result == .success,
details: result.details
)
completion(testRespone, nil)
case .failure(let error):
completion(nil, error)
}
}
}
Expand Down

0 comments on commit 86715a6

Please sign in to comment.