Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: TypeMismatch doesn't trigger apply #172

Merged
merged 4 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions Sources/Confidence/FlagEvaluation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ extension FlagResolution {
)
}

if resolvedFlag.resolveReason != .targetingKeyError {
Task {
await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken)
}
} else {
if resolvedFlag.resolveReason == .targetingKeyError {
return Evaluation(
value: defaultValue,
variant: nil,
Expand All @@ -59,6 +55,9 @@ extension FlagResolution {
}

guard let value = resolvedFlag.value else {
Task {
await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken)
}
fabriziodemaria marked this conversation as resolved.
Show resolved Hide resolved
return Evaluation(
value: defaultValue,
variant: resolvedFlag.variant,
Expand All @@ -77,6 +76,9 @@ extension FlagResolution {
resolveReason = .stale
}
if let typedValue = typedValue {
Task {
await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken)
fabriziodemaria marked this conversation as resolved.
Show resolved Hide resolved
}
return Evaluation(
value: typedValue,
variant: resolvedFlag.variant,
Expand All @@ -87,6 +89,9 @@ extension FlagResolution {
} else {
// `null` type from backend instructs to use client-side default value
if parsedValue == .init(null: ()) {
Task {
await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken)
}
return Evaluation(
value: defaultValue,
variant: resolvedFlag.variant,
Expand All @@ -105,6 +110,9 @@ extension FlagResolution {
}
}
} else {
Task {
await flagApplier?.apply(flagName: parsedKey.flag, resolveToken: self.resolveToken)
}
return Evaluation(
value: defaultValue,
variant: resolvedFlag.variant,
Expand Down
41 changes: 41 additions & 0 deletions Tests/ConfidenceTests/ConfidenceTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,46 @@ class ConfidenceTest: XCTestCase {
XCTAssertEqual(flagApplier.applyCallCount, 1)
}

func testResolveAndApplyIntegerFlagNullValue() async throws {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is unrelated to the fix, but nice to have

class FakeClient: 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(
value: .init(structure: ["size": .init(null: ())]),
flag: "flag",
resolveReason: .match)
]

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

try await confidence.fetchAndActivate()
let evaluation = confidence.getEvaluation(
key: "flag.size",
defaultValue: 4)

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

func testResolveAndApplyIntegerFlagTwice() async throws {
class FakeClient: ConfidenceResolveClient {
var resolveStats: Int = 0
Expand Down Expand Up @@ -654,6 +694,7 @@ class ConfidenceTest: XCTestCase {
XCTAssertNil(evaluation.errorMessage, "")
XCTAssertEqual(evaluation.reason, .error)
XCTAssertEqual(evaluation.variant, nil)
XCTAssertEqual(flagApplier.applyCallCount, 0)
}

func testConcurrentActivate() async {
Expand Down
Loading