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/dispatch queue #79

Merged
merged 15 commits into from
Dec 1, 2023
6 changes: 5 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,9 @@ jobs:
- uses: actions/checkout@v2
- name: Build
run: swift build
test:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
- name: Run tests
run: swift test
run: swift test --sanitize=thread
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,13 @@ Once that succeeds, you can do the actual release:
```sh
pod trunk push UnleashProxyClientSwift.podspec --allow-warnings
```

## Testing

In order to test this package you can run the swift test command. To test thread safety, run swift test with:

```
swift test --sanitize=thread
```

This will give you warnings in the console when you have any data races.
13 changes: 10 additions & 3 deletions Sources/UnleashProxyClientSwift/Poller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,26 @@ public protocol StorageProvider {

public class DictionaryStorageProvider: StorageProvider {
private var storage: [String: Toggle] = [:]
private let queue = DispatchQueue(label: "com.unleash.storageprovider")

public init() {}

public func set(value: Toggle?, key: String) {
storage[key] = value
queue.async {
self.storage[key] = value
}
}

public func value(key: String) -> Toggle? {
return storage[key]
queue.sync {
return self.storage[key]
}
}

public func clear() {
storage = [:]
queue.sync {
self.storage = [:]
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ class UnleashIntegrationTests: XCTestCase {
});

unleashClient.start()
// Run isEnabled many times to trigger a data race when the poller is updating the data cache
// This is to test that the poller is thread safe, and you can verify this by running the test with
// swift test --sanitize=thread
for _ in 1...15000 {
let result = unleashClient.isEnabled(name: "dataRaceTest")
XCTAssertFalse(result)
}

wait(for: [expectation], timeout: 5)
}
Expand Down
2 changes: 1 addition & 1 deletion UnleashProxyClientSwift.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |spec|
spec.name = "UnleashProxyClientSwift"
spec.version = "1.1.0"
spec.version = "1.1.1"
spec.summary = "Allows frontend clients to talk to unleash through the unleash edge, frontend API or the (deprecated) unleash proxy"
spec.homepage = "https://www.getunleash.io"
spec.license = { :type => "MIT", :file => "LICENSE" }
Expand Down
Loading