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

Feat/async start and updateContext overloads #90

Merged
Show file tree
Hide file tree
Changes from 4 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
30 changes: 29 additions & 1 deletion Sources/UnleashProxyClientSwift/UnleashProxyClientSwift.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,21 @@ public class UnleashClientBase {
poller.start(context: context, completionHandler: completionHandler)
metrics.start()
}


@available(iOS 13.0, *)
@MainActor
public func start(_ printToConsole: Bool = false) async throws {
return try await withCheckedThrowingContinuation { continuation in
start(printToConsole) { error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}
}

public func stop() -> Void {
poller.stop()
metrics.stop()
Expand Down Expand Up @@ -105,6 +119,20 @@ public class UnleashClientBase {
self.context = self.calculateContext(context: context, properties: properties)
self.start(completionHandler: completionHandler)
}

@available(iOS 13.0, *)
@MainActor
public func updateContext(context: [String: String], properties: [String:String]? = nil) async throws {
return try await withCheckedThrowingContinuation { continuation in
updateContext(context: context, properties: properties) { error in
if let error {
continuation.resume(throwing: error)
} else {
continuation.resume()
}
}
}
}

func calculateContext(context: [String: String], properties: [String:String]? = nil) -> Context {
let specialKeys: Set = ["appName", "environment", "userId", "sessionId", "remoteAddress"]
Expand Down
2 changes: 2 additions & 0 deletions Tests/UnleashProxyClientSwiftTests/MockPoller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public class MockDictionaryStorageProvider: StorageProvider {

class MockPoller: Poller {
var dataGenerator: () -> [String: Toggle];
var stubCompletionError: PollerError?

init(callback: @escaping () -> [String: Toggle], unleashUrl: URL, apiKey: String, session: PollerSession) {
self.dataGenerator = callback
Expand All @@ -57,5 +58,6 @@ class MockPoller: Poller {

override func getFeatures(context: Context, completionHandler: ((PollerError?) -> Void)? = nil) -> Void {
self.storageProvider = MockDictionaryStorageProvider(storage: dataGenerator())
completionHandler?(stubCompletionError)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,21 @@
XCTAssert(unleash.poller.timer != nil)
}

@MainActor
func testTimerWithAsyncStart() async throws {
func dataGenerator() -> [String: UnleashProxyClientSwift.Toggle] {
return generateTestToggleMapWithVariant()
}

let mockedPoller = MockPollerSession()
let unleash = try await setup(
dataGenerator: dataGenerator,
session: mockedPoller
)

XCTAssert(unleash.poller.timer != nil)
}

func testUpdateContext() {
func dataGenerator() -> [String: UnleashProxyClientSwift.Toggle] {
return generateTestToggleMapWithVariant()
Expand All @@ -62,6 +77,24 @@

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

@MainActor
func testUpdateContextAsync() async throws {
func dataGenerator() -> [String: UnleashProxyClientSwift.Toggle] {
return generateTestToggleMapWithVariant()
}

let unleash = try await setup(dataGenerator: dataGenerator)

var context: [String: String] = [:]
context["userId"] = "uuid-123-test"
context["sessionId"] = "uuid-234-test"
try await unleash.updateContext(context: context)

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"))
}
}

final class unleash_proxy_client_base_swiftTests: XCTestCase {
Expand Down
22 changes: 22 additions & 0 deletions Tests/UnleashProxyClientSwiftTests/testUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ func setup(dataGenerator: @escaping () -> [String: Toggle], session: PollerSessi
return unleash
}

@available(iOS 13, *)
func setup(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) 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()
return unleash
}

func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) -> 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")
Expand All @@ -51,3 +62,14 @@ func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerS
unleash.start()
return unleash
}

@available(iOS 13, *)
func setupBase(dataGenerator: @escaping () -> [String: Toggle], session: PollerSession = MockPollerSession()) 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()
return unleash
}
Loading