Skip to content

Commit ba72c4c

Browse files
committed
just using TimeoutExecutor
1 parent fd187ab commit ba72c4c

File tree

3 files changed

+41
-13
lines changed

3 files changed

+41
-13
lines changed

LaunchDarkly/LaunchDarkly/LDClient.swift

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -855,20 +855,36 @@ public class LDClient {
855855
start(serviceFactory: serviceFactory, config: config, context: context)
856856
completion?(true) // offline is considered a short circuited timed out case
857857
} else {
858-
let startTime = Date().timeIntervalSince1970
858+
// let startTime = Date().timeIntervalSince1970
859859

860860
TimeoutExecutor.run(
861861
timeout: startWaitSeconds,
862862
queue: internalCompletedQueue,
863863
operation: { done in
864-
Self.start(serviceFactory: serviceFactory, config: config, context: context) {
865-
let onTime = startWaitSeconds > Date().timeIntervalSince1970 - startTime
866-
done(!onTime)
864+
start(serviceFactory: serviceFactory, config: config, context: context) {
865+
// let onTime = startWaitSeconds > Date().timeIntervalSince1970 - startTime
866+
done(false)
867867
}
868868
},
869869
timeoutValue: true,
870870
completion: completion
871871
)
872+
873+
// var completed = false
874+
// start(serviceFactory: serviceFactory, config: config, context: context) {
875+
// internalCompletedQueue.async {
876+
// if startTime + startWaitSeconds > Date().timeIntervalSince1970 && !completed {
877+
// completed = true
878+
// completion?(false) // false for not timedOut
879+
// }
880+
// }
881+
// }
882+
// internalCompletedQueue.asyncAfter(deadline: .now() + startWaitSeconds) {
883+
// if !completed {
884+
// completed = true
885+
// completion?(true) // true for timedOut
886+
// }
887+
// }
872888
}
873889
}
874890

LaunchDarkly/LaunchDarkly/ServiceObjects/TimeoutExecutor.swift

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ final class TimeoutExecutor {
4343
let lockQueue = DispatchQueue(label: "launchdarkly.timeout.executor.lock")
4444
var finished = false
4545

46-
func finish(_ value: @autoclosure () -> T) {
46+
// Start the user operation
47+
operation { value in
4748
var shouldCall = false
4849
lockQueue.sync {
4950
if !finished {
@@ -52,17 +53,20 @@ final class TimeoutExecutor {
5253
}
5354
}
5455
guard shouldCall else { return }
55-
queue.async { completion(timeoutValue()) }
56-
}
57-
58-
// Start the user operation (they can call `done` from any queue)
59-
operation { value in
60-
finish(value)
56+
queue.async { completion(value) }
6157
}
6258

63-
// Timeout fallback scheduled
59+
// Timeout fallback
6460
queue.asyncAfter(deadline: .now() + timeout) {
65-
finish(timeoutValue())
61+
var shouldCall = false
62+
lockQueue.sync {
63+
if !finished {
64+
finished = true
65+
shouldCall = true
66+
}
67+
}
68+
guard shouldCall else { return }
69+
completion(timeoutValue())
6670
}
6771
}
6872
}

LaunchDarkly/LaunchDarklyTests/LDClientSpec.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,12 +326,20 @@ final class LDClientSpec: QuickSpec {
326326
}
327327
}
328328
context("after receiving flags") {
329+
beforeEach {
330+
completed = false
331+
didTimeOut = nil
332+
startTime = nil
333+
completeTime = nil
334+
}
329335
it("does complete without timeout") {
336+
//expect(didTimeOut) == nil
330337
testContext.start(completion: startCompletion)
331338
testContext.onSyncComplete?(.flagCollection((FeatureFlagCollection([:]), nil)))
332339
expect(completed).toEventually(beTrue(), timeout: DispatchTimeInterval.seconds(2))
333340
}
334341
it("does complete with timeout") {
342+
//expect(didTimeOut) == nil
335343
waitUntil(timeout: .seconds(3)) { done in
336344
testContext.start(timeOut: 5.0, timeOutCompletion: startTimeoutCompletion(done))
337345
testContext.onSyncComplete?(.flagCollection((FeatureFlagCollection([:]), nil)))

0 commit comments

Comments
 (0)