|
| 1 | +// |
| 2 | +// Copyright 2023, Optimizely, Inc. and contributors |
| 3 | +// |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// |
| 8 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +// |
| 10 | +// Unless required by applicable law or agreed to in writing, software |
| 11 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +// See the License for the specific language governing permissions and |
| 14 | +// limitations under the License. |
| 15 | +// |
| 16 | + |
| 17 | +import XCTest |
| 18 | +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 19 | +final class OptimizelyClientTests_Init_Async_Await: XCTestCase { |
| 20 | + let kUserId = "11111" |
| 21 | + let kExperimentKey = "exp_with_audience" |
| 22 | + let kFlagKey = "feature_1" |
| 23 | + let kVariationKey = "a" |
| 24 | + let kRevisionUpdated = "34" |
| 25 | + let kRevision = "241" |
| 26 | + |
| 27 | + func testInitAsyncAwait() async throws { |
| 28 | + let testSdkKey = OTUtils.randomSdkKey // unique but consistent with registry + start |
| 29 | + |
| 30 | + let handler = FakeDatafileHandler(mode: .successWithData) |
| 31 | + let optimizely = OptimizelyClient(sdkKey: testSdkKey, |
| 32 | + datafileHandler: handler) |
| 33 | + |
| 34 | + try await optimizely.start() |
| 35 | + let user = OptimizelyUserContext(optimizely: optimizely, userId: self.kUserId) |
| 36 | + let decision = user.decide(key: self.kFlagKey) |
| 37 | + |
| 38 | + XCTAssert(decision.variationKey == self.kVariationKey) |
| 39 | + } |
| 40 | + |
| 41 | + func testInitAsyncAwait_fetchError() async throws { |
| 42 | + let testSdkKey = OTUtils.randomSdkKey // unique but consistent with registry + start |
| 43 | + |
| 44 | + let handler = FakeDatafileHandler(mode: .failure) |
| 45 | + let optimizely = OptimizelyClient(sdkKey: testSdkKey, |
| 46 | + datafileHandler: handler) |
| 47 | + var _error: Error? |
| 48 | + do { |
| 49 | + try await optimizely.start() |
| 50 | + } catch { |
| 51 | + _error = error |
| 52 | + } |
| 53 | + |
| 54 | + XCTAssertNotNil(_error) |
| 55 | + } |
| 56 | + |
| 57 | + func testInitAsync_fetchNil_whenCacheLoadFailed() async { |
| 58 | + let testSdkKey = OTUtils.randomSdkKey // unique but consistent with registry + start |
| 59 | + |
| 60 | + let handler = FakeDatafileHandler(mode: .failedToLoadFromCache) |
| 61 | + let optimizely = OptimizelyClient(sdkKey: testSdkKey, |
| 62 | + datafileHandler: handler) |
| 63 | + |
| 64 | + var _error: Error? |
| 65 | + do { |
| 66 | + try await optimizely.start() |
| 67 | + } catch { |
| 68 | + _error = error |
| 69 | + } |
| 70 | + |
| 71 | + XCTAssertNotNil(_error) |
| 72 | + } |
| 73 | + |
| 74 | +} |
| 75 | + |
| 76 | +@available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) |
| 77 | +extension OptimizelyClientTests_Init_Async_Await { |
| 78 | + |
| 79 | + enum DataFileResponse { |
| 80 | + case successWithData |
| 81 | + case failedToLoadFromCache |
| 82 | + case failure |
| 83 | + } |
| 84 | + |
| 85 | + class FakeDatafileHandler: DefaultDatafileHandler { |
| 86 | + var mode: DataFileResponse |
| 87 | + var fileFlag: Bool = true |
| 88 | + |
| 89 | + init(mode: DataFileResponse) { |
| 90 | + self.mode = mode |
| 91 | + } |
| 92 | + |
| 93 | + required init() { |
| 94 | + fatalError("init() has not been implemented") |
| 95 | + } |
| 96 | + |
| 97 | + override func downloadDatafile(sdkKey: String, |
| 98 | + returnCacheIfNoChange: Bool, |
| 99 | + resourceTimeoutInterval: Double?, |
| 100 | + completionHandler: @escaping DatafileDownloadCompletionHandler) { |
| 101 | + |
| 102 | + switch mode { |
| 103 | + case .successWithData: |
| 104 | + let filename = fileFlag ? OptimizelyClientTests_Init_Async.JSONfilename : OptimizelyClientTests_Init_Async.JSONfilenameUpdated |
| 105 | + fileFlag.toggle() |
| 106 | + |
| 107 | + let data = OTUtils.loadJSONDatafile(filename) |
| 108 | + completionHandler(.success(data)) |
| 109 | + case .failedToLoadFromCache: |
| 110 | + completionHandler(.success(nil)) |
| 111 | + case .failure: |
| 112 | + completionHandler(.failure(.dataFileInvalid)) |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments