Skip to content

Commit

Permalink
feat: Add awaitReconciliation
Browse files Browse the repository at this point in the history
  • Loading branch information
fabriziodemaria committed Dec 5, 2024
1 parent f123b8a commit 6c4031c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
7 changes: 7 additions & 0 deletions Sources/Confidence/Confidence.swift
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,13 @@ public class Confidence: ConfidenceEventSender {
}
}

/**
Ensures all the already-started context changes prior to this function have been reconciliated
*/
public func awaitReconciliation() async {
await withSemaphoreAsync {}
}

private func internalFetch() async throws {
let context = getContext()
let resolvedFlags = try await remoteFlagResolver.resolve(ctx: context)
Expand Down
44 changes: 44 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,50 @@ class ConfidenceTest: XCTestCase {
XCTAssertEqual(flagApplier.applyCallCount, 1)
}

func testAwaitReconciliation() async throws {
class FakeClient: XCTestCase, ConfidenceResolveClient {
var resolveStats: Int = 0
var resolvedValues: [ResolvedValue] = []
func resolve(ctx: ConfidenceStruct) async throws -> ResolvesResult {
self.resolveStats += 1
return .init(resolvedValues: 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)
.build()
Task {
await confidence.putContext(context: ["hello": .init(string: "world")])
}
try await Task.sleep(nanoseconds: 100 * 1000) // 100 ms wait
await confidence.awaitReconciliation()
let evaluation = confidence.getEvaluation(
key: "flag.size",
defaultValue: 0)

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

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

0 comments on commit 6c4031c

Please sign in to comment.