Skip to content

Commit

Permalink
Fix/dispatch queue (#79)
Browse files Browse the repository at this point in the history
Add dispatch queue to DictionaryStorageProvider
  • Loading branch information
FredrikOseberg authored Dec 1, 2023
1 parent 8acb2c5 commit f1ae08f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 5 deletions.
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

0 comments on commit f1ae08f

Please sign in to comment.