Skip to content

Commit

Permalink
fix/on-updateContext maintain print to console state (#89)
Browse files Browse the repository at this point in the history
* fix: updateContext keeps showPrintStatements state

* tests: Updates tests for keeping printToConsole state

* feat: Adds additional console logging for flag updates/fetching

* Update Sources/UnleashProxyClientSwift/Poller.swift

---------

Co-authored-by: Fredrik Strand Oseberg <[email protected]>
  • Loading branch information
benjamin-es-hall and FredrikOseberg authored Oct 10, 2024
1 parent dae3e2c commit ef3be33
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions Sources/UnleashProxyClientSwift/Poller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,10 @@ public class Poller {

self.createFeatureMap(features: json)
if (self.ready) {
Printer.printMessage("Flags updated")
SwiftEventBus.post("update")
} else {
Printer.printMessage("Initial flags fetched")
SwiftEventBus.post("ready")
self.ready = true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public class UnleashClientBase {

public func updateContext(context: [String: String], properties: [String:String]? = nil, completionHandler: ((PollerError?) -> Void)? = nil) {
self.context = self.calculateContext(context: context, properties: properties)
self.start(completionHandler: completionHandler)
self.start(Printer.showPrintStatements, completionHandler: completionHandler)
}

func calculateContext(context: [String: String], properties: [String:String]? = nil) -> Context {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@
return generateTestToggleMapWithVariant()
}

let unleash = setup(dataGenerator: dataGenerator)
let stubPrintToConsoleAtStart = true
let unleash = setup(
dataGenerator: dataGenerator,
printToConsole: stubPrintToConsoleAtStart
)

var context: [String: String] = [:]
context["userId"] = "uuid-123-test"
Expand All @@ -76,6 +80,7 @@
let url = unleash.poller.formatURL(context: unleash.context)!.absoluteString

XCTAssert(url.contains("appName=test") && url.contains("sessionId=uuid-234-test") && url.contains("userId=uuid-123-test") && url.contains("environment=dev"))
XCTAssertEqual(Printer.showPrintStatements, stubPrintToConsoleAtStart)
}

@MainActor
Expand Down
17 changes: 8 additions & 9 deletions Tests/UnleashProxyClientSwiftTests/testUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,44 +32,43 @@ func generateTestToggleMapWithVariant() -> [String: Toggle] {
}

@available(iOS 13, *)
func setup(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) -> UnleashClient {
func setup(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession(), printToConsole: Bool = false) -> UnleashClient {
let poller = MockPoller(callback: dataGenerator, unleashUrl: URL(string: "https://app.unleash-hosted.com/hosted/api/proxy")!, apiKey: "SECRET", session: session)
let metrics = MockMetrics(appName: "test")

let unleash = UnleashProxyClientSwift.UnleashClient(unleashUrl: "https://app.unleash-hosted.com/hosted/api/proxy", clientKey: "dss22d", refreshInterval: 15, appName: "test", environment: "dev", poller: poller, metrics: metrics)

unleash.start()
unleash.start(printToConsole)
return unleash
}

@available(iOS 13, *)
func setup(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) async throws -> UnleashClient {
func setup(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession(), printToConsole: Bool = false) async throws -> UnleashClient {
let poller = MockPoller(callback: dataGenerator, unleashUrl: URL(string: "https://app.unleash-hosted.com/hosted/api/proxy")!, apiKey: "SECRET", session: session)
let metrics = MockMetrics(appName: "test")

let unleash = UnleashProxyClientSwift.UnleashClient(unleashUrl: "https://app.unleash-hosted.com/hosted/api/proxy", clientKey: "dss22d", refreshInterval: 15, appName: "test", environment: "dev", poller: poller, metrics: metrics)

try await unleash.start()
try await unleash.start(printToConsole)
return unleash
}

func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) -> UnleashClientBase {
func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession(), printToConsole: Bool = false) -> UnleashClientBase {
let poller = MockPoller(callback: dataGenerator, unleashUrl: URL(string: "https://app.unleash-hosted.com/hosted/api/proxy")!, apiKey: "SECRET", session: session)
let metrics = MockMetrics(appName: "test")

let unleash = UnleashProxyClientSwift.UnleashClientBase(unleashUrl: "https://app.unleash-hosted.com/hosted/api/proxy", clientKey: "dss22d", refreshInterval: 15, appName: "test", environment: "dev", poller: poller, metrics: metrics)

unleash.start()
unleash.start(printToConsole)
return unleash
}

@available(iOS 13, *)
func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) async throws -> UnleashClientBase {
func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession(), printToConsole: Bool = false) async throws -> UnleashClientBase {
let poller = MockPoller(callback: dataGenerator, unleashUrl: URL(string: "https://app.unleash-hosted.com/hosted/api/proxy")!, apiKey: "SECRET", session: session)
let metrics = MockMetrics(appName: "test")

let unleash = UnleashProxyClientSwift.UnleashClientBase(unleashUrl: "https://app.unleash-hosted.com/hosted/api/proxy", clientKey: "dss22d", refreshInterval: 15, appName: "test", environment: "dev", poller: poller, metrics: metrics)

try await unleash.start()
unleash.start(printToConsole)
return unleash
}

0 comments on commit ef3be33

Please sign in to comment.