Skip to content

Commit

Permalink
fix: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
bgiori committed Dec 17, 2024
1 parent 6da9fda commit beed8ac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 31 deletions.
19 changes: 5 additions & 14 deletions Sources/Experiment/ExperimentClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,8 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
}

public func fetch(user: ExperimentUser?, options: FetchOptions?, completion: ((ExperimentClient, Error?) -> Void)? = nil) -> Void {
userQueue.sync {
if user != nil && user != ExperimentUser() {
self.user = user
}
if user != nil && user != ExperimentUser() {
self.setUser(user)
}
fetchQueue.async {
do {
Expand Down Expand Up @@ -578,14 +576,14 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
self.backoff = nil
}

internal func mergeUserWithProvider() -> ExperimentUser {
internal func mergeUserWithProvider(_ providedUser: ExperimentUser? = nil) -> ExperimentUser {
userQueue.sync {
var libraryUser: ExperimentUser = self.user ?? ExperimentUser()
if self.user?.library == nil {
let library = "\(ExperimentConfig.Constants.Library)/\(ExperimentConfig.Constants.Version)"
libraryUser = libraryUser.copyToBuilder().library(library).build()
}
return libraryUser.merge(userProvider?.getUser())
return libraryUser.merge(providedUser ?? userProvider?.getUser())
}
}

Expand All @@ -596,14 +594,7 @@ internal class DefaultExperimentClient : NSObject, ExperimentClient {
} else {
providedUser = self.userProvider?.getUser()
}
return userQueue.sync {
var libraryUser: ExperimentUser = self.user ?? ExperimentUser()
if self.user?.library == nil {
let library = "\(ExperimentConfig.Constants.Library)/\(ExperimentConfig.Constants.Version)"
libraryUser = libraryUser.copyToBuilder().library(library).build()
}
return libraryUser.merge(providedUser)
}
return mergeUserWithProvider(providedUser)
}

private func parseResponseData(_ data: Data?) throws -> [String: Variant] {
Expand Down
32 changes: 15 additions & 17 deletions Tests/ExperimentTests/ExperimentClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,24 +28,22 @@ let serverVariant2 = Variant("on")
class ExperimentClientTests: XCTestCase {

func testFetch() {
for i in 0..<100 {
let s = DispatchSemaphore(value: 0)
let client = DefaultExperimentClient(
apiKey: API_KEY,
config: ExperimentConfigBuilder()
.debug(true)
.build(),
storage: InMemoryStorage()
)
client.fetch(user: testUser) { (client, error) in
XCTAssertNil(error)
let variant = client.variant(KEY, fallback: nil)
XCTAssertEqual(serverVariant, variant)
s.signal()
}
s.wait()
let s = DispatchSemaphore(value: 0)
let client = DefaultExperimentClient(
apiKey: API_KEY,
config: ExperimentConfigBuilder()
.debug(true)
.build(),
storage: InMemoryStorage()
)
client.fetch(user: testUser) { (client, error) in
XCTAssertNil(error)
let variant = client.variant(KEY, fallback: nil)
XCTAssertEqual(serverVariant, variant)
s.signal()
}
}
s.wait()
}

func testFetchTimeout() {
let client = DefaultExperimentClient(
Expand Down

0 comments on commit beed8ac

Please sign in to comment.