Skip to content

Commit

Permalink
test: putContextAndWait and awaitReconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Dec 10, 2024
1 parent a4e3dd1 commit 7d4f41f
Showing 1 changed file with 62 additions and 0 deletions.
62 changes: 62 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,68 @@ class ConfidenceTest: XCTestCase {
XCTAssertEqual(flagApplier.applyCallCount, 1)
}

func testAwaitReconciliationFailingTaskAwait() async throws {
class FakeClient: XCTestCase, ConfidenceResolveClient {
var resolveStats: Int = 0
var resolvedValues: [ResolvedValue] = []

func resolve(ctx: ConfidenceStruct) async throws -> ResolvesResult {
self.resolveStats += 1
if resolveStats == 1 {
// Delay to ensure the second putContext cancels this Task
try await Task.sleep(nanoseconds: 2000000)
XCTFail("This line shouldn't be reached as task is expected to be cancelled")
return .init(resolvedValues: [], resolveToken: "token")
} else {
if ctx["hello"] == .init(string: "world") {
return .init(resolvedValues: resolvedValues, resolveToken: "token")
} else {
return .init(resolvedValues: [], resolveToken: "token")
}
}
}
}

let client = FakeClient()
client.resolvedValues = [
ResolvedValue(
variant: "control",
value: .init(structure: ["size": .init(integer: 3)]),
flag: "flag",
resolveReason: .match)
]

let confidence = Confidence.Builder(clientSecret: "test")
.withContext(initialContext: ["targeting_key": .init(string: "user2")])
.withFlagResolverClient(flagResolver: client)
.withFlagApplier(flagApplier: flagApplier)
.withStorage(storage: storage)
.build()

Task {
await confidence.putContextAndWait(context: ["hello": .init(string: "not-world")])
}
try await Task.sleep(nanoseconds: 1000000)
Task {
await confidence.putContextAndWait(context: ["hello": .init(string: "world")])
}
try await Task.sleep(nanoseconds: 1000000)
await confidence.awaitReconciliation()
let evaluation = confidence.getEvaluation(
key: "flag.size",
defaultValue: 0
)

XCTAssertEqual(client.resolveStats, 2)
XCTAssertEqual(evaluation.value, 3)
XCTAssertNil(evaluation.errorCode)
XCTAssertNil(evaluation.errorMessage)
XCTAssertEqual(evaluation.reason, .match)
XCTAssertEqual(evaluation.variant, "control")
await fulfillment(of: [flagApplier.applyExpectation], timeout: 1)
XCTAssertEqual(flagApplier.applyCallCount, 1)
}

func testResolveBooleanFlag() async throws {
class FakeClient: ConfidenceResolveClient {
var resolveStats: Int = 0
Expand Down

0 comments on commit 7d4f41f

Please sign in to comment.