-
Notifications
You must be signed in to change notification settings - Fork 32
[FSSDK-9419]: feat: Swift async-await support added #513
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
Changes from all commits
c1652c3
f226025
120a2c3
69b971a
e5876db
d6b0706
8c78a27
c4ad209
ded9ed6
d22040e
711b0b1
736856b
9c8e2e8
33ee36a
649a33b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -302,14 +302,15 @@ class SamplesForAPI { | |
| defaultLogLevel: .debug) | ||
|
|
||
| guard let localDatafileUrl = Bundle.main.url(forResource: "demoTestDatafile", withExtension: "json"), | ||
| let localDatafile = try? Data(contentsOf: localDatafileUrl) | ||
| let localDatafile = try? Data(contentsOf: localDatafileUrl) | ||
| else { | ||
| fatalError("Local datafile cannot be found") | ||
| } | ||
|
|
||
| try? optimizely.start(datafile: localDatafile) | ||
|
|
||
| let user = optimizely.createUserContext(userId: "user_123", attributes: ["location": "NY"]) | ||
|
|
||
| user.fetchQualifiedSegments(options: [.ignoreCache]) { error in | ||
| guard error == nil else { | ||
| print("[AudienceSegments] \(error!)") | ||
|
|
@@ -319,6 +320,27 @@ class SamplesForAPI { | |
| let decision = user.decide(key: "show_coupon", options: [.includeReasons]) | ||
| print("[AudienceSegments] decision: \(decision)") | ||
| } | ||
|
|
||
| if #available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) { | ||
| Task { [user] in | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this explicit capturing [user] here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yah, as we declare user as local variable inside the static method, that's why we needed to capture it. Otherwise, the compiler throws errors. Normally we don't need to capture it, as we did in appdelegate. |
||
| do { | ||
| try await user.fetchQualifiedSegments(options: [.ignoreCache]) | ||
| let decision = user.decide(key: "show_coupon", options: [.includeReasons]) | ||
| print("[AudienceSegments] decision: \(decision)") | ||
| } catch { | ||
| print("[AudienceSegments] \(error)") | ||
| } | ||
|
|
||
| // Without segment option | ||
| do { | ||
muzahidul-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try await user.fetchQualifiedSegments() | ||
| let decision = user.decide(key: "show_coupon") | ||
| print("[AudienceSegments] decision: \(decision)") | ||
| } catch { | ||
| print("[AudienceSegments] \(error)") | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Initializations | ||
|
|
@@ -386,6 +408,23 @@ class SamplesForAPI { | |
| } | ||
|
|
||
| print("activated variation: \(String(describing: variationKey))") | ||
|
|
||
| // [A3] Asynchronous initialization (aync-await) | ||
| // 1. A datafile is downloaded from the server and the SDK is initialized with the datafile | ||
| // 2. Polling datafile periodically. | ||
| // The cached datafile is used immediately to update the SDK project config. | ||
| optimizely = OptimizelyClient(sdkKey: "<Your_SDK_Key>", | ||
| periodicDownloadInterval: 60) | ||
| if #available(iOS 13, *) { | ||
muzahidul-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Task { [optimizely] in | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need this explicit capturing [optimizely] here?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Discussed above |
||
| do { | ||
| try await optimizely.start() | ||
| } catch { | ||
| print("Optimizely SDK initiliazation failed: \(error)") | ||
| } | ||
| } | ||
| } | ||
|
|
||
muzahidul-opti marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| // | ||
| // Copyright 2023, Optimizely, Inc. and contributors | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
| // | ||
|
|
||
| import XCTest | ||
| @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
| final class OptimizelyClientTests_Init_Async_Await: XCTestCase { | ||
| let kUserId = "11111" | ||
| let kExperimentKey = "exp_with_audience" | ||
| let kFlagKey = "feature_1" | ||
| let kVariationKey = "a" | ||
| let kRevisionUpdated = "34" | ||
| let kRevision = "241" | ||
|
|
||
| func testInitAsyncAwait() async throws { | ||
| let testSdkKey = OTUtils.randomSdkKey // unique but consistent with registry + start | ||
|
|
||
| let handler = FakeDatafileHandler(mode: .successWithData) | ||
| let optimizely = OptimizelyClient(sdkKey: testSdkKey, | ||
| datafileHandler: handler) | ||
|
|
||
| try await optimizely.start() | ||
| let user = OptimizelyUserContext(optimizely: optimizely, userId: self.kUserId) | ||
| let decision = user.decide(key: self.kFlagKey) | ||
|
|
||
| XCTAssert(decision.variationKey == self.kVariationKey) | ||
| } | ||
|
|
||
| func testInitAsyncAwait_fetchError() async throws { | ||
| let testSdkKey = OTUtils.randomSdkKey // unique but consistent with registry + start | ||
|
|
||
| let handler = FakeDatafileHandler(mode: .failure) | ||
| let optimizely = OptimizelyClient(sdkKey: testSdkKey, | ||
| datafileHandler: handler) | ||
| var _error: Error? | ||
| do { | ||
| try await optimizely.start() | ||
| } catch { | ||
| _error = error | ||
| } | ||
|
|
||
| XCTAssertNotNil(_error) | ||
| } | ||
|
|
||
| func testInitAsync_fetchNil_whenCacheLoadFailed() async { | ||
| let testSdkKey = OTUtils.randomSdkKey // unique but consistent with registry + start | ||
|
|
||
| let handler = FakeDatafileHandler(mode: .failedToLoadFromCache) | ||
| let optimizely = OptimizelyClient(sdkKey: testSdkKey, | ||
| datafileHandler: handler) | ||
|
|
||
| var _error: Error? | ||
| do { | ||
| try await optimizely.start() | ||
| } catch { | ||
| _error = error | ||
| } | ||
|
|
||
| XCTAssertNotNil(_error) | ||
| } | ||
|
|
||
| } | ||
|
|
||
| @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) | ||
| extension OptimizelyClientTests_Init_Async_Await { | ||
|
|
||
| enum DataFileResponse { | ||
| case successWithData | ||
| case failedToLoadFromCache | ||
| case failure | ||
| } | ||
|
|
||
| class FakeDatafileHandler: DefaultDatafileHandler { | ||
| var mode: DataFileResponse | ||
| var fileFlag: Bool = true | ||
|
|
||
| init(mode: DataFileResponse) { | ||
| self.mode = mode | ||
| } | ||
|
|
||
| required init() { | ||
| fatalError("init() has not been implemented") | ||
| } | ||
|
|
||
| override func downloadDatafile(sdkKey: String, | ||
| returnCacheIfNoChange: Bool, | ||
| resourceTimeoutInterval: Double?, | ||
| completionHandler: @escaping DatafileDownloadCompletionHandler) { | ||
|
|
||
| switch mode { | ||
| case .successWithData: | ||
| let filename = fileFlag ? OptimizelyClientTests_Init_Async.JSONfilename : OptimizelyClientTests_Init_Async.JSONfilenameUpdated | ||
| fileFlag.toggle() | ||
|
|
||
| let data = OTUtils.loadJSONDatafile(filename) | ||
| completionHandler(.success(data)) | ||
| case .failedToLoadFromCache: | ||
| completionHandler(.success(nil)) | ||
| case .failure: | ||
| completionHandler(.failure(.dataFileInvalid)) | ||
| } | ||
| } | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit - extra spaces. swift lint?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't see any lint warning.