|
| 1 | +// |
| 2 | +// DVCConfigTests.swift |
| 3 | +// DevCycleTests |
| 4 | +// |
| 5 | +// |
| 6 | + |
| 7 | +import XCTest |
| 8 | +@testable import DevCycle |
| 9 | + |
| 10 | +final class DVCConfigTests: XCTestCase { |
| 11 | + |
| 12 | + var mockUser: DevCycleUser! |
| 13 | + |
| 14 | + override func setUp() { |
| 15 | + super.setUp() |
| 16 | + mockUser = try! DevCycleUser.builder() |
| 17 | + .userId("test-user") |
| 18 | + .isAnonymous(false) |
| 19 | + .build() |
| 20 | + } |
| 21 | + |
| 22 | + override func tearDown() { |
| 23 | + mockUser = nil |
| 24 | + super.tearDown() |
| 25 | + } |
| 26 | + |
| 27 | + func getMockUserConfig(config: String = "test_config_eval_reason") throws -> UserConfig { |
| 28 | + let data = getConfigData(name: config) |
| 29 | + let dictionary = try JSONSerialization.jsonObject(with: data, options: .fragmentsAllowed) as! [String:Any] |
| 30 | + return try UserConfig(from: dictionary) |
| 31 | + } |
| 32 | + |
| 33 | + func testConcurrentConfigAccess() throws { |
| 34 | + let testConfig = DVCConfig(sdkKey: "test-sdk-key", user: mockUser) |
| 35 | + |
| 36 | + let expectation = XCTestExpectation(description: "Concurrent config access completed") |
| 37 | + expectation.expectedFulfillmentCount = 100 // 50 reads + 50 writes |
| 38 | + |
| 39 | + let concurrentQueue = DispatchQueue(label: "test.concurrent", attributes: .concurrent) |
| 40 | + |
| 41 | + // Test concurrent reads |
| 42 | + for _ in 0..<50 { |
| 43 | + concurrentQueue.async { |
| 44 | + let _ = testConfig.getUserConfig() |
| 45 | + expectation.fulfill() |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + let evalConfig = try self.getMockUserConfig(config: "test_config_eval_reason") |
| 50 | + |
| 51 | + // Test concurrent writes |
| 52 | + for _ in 0..<50 { |
| 53 | + concurrentQueue.async { |
| 54 | + testConfig.setUserConfig(config: evalConfig) |
| 55 | + expectation.fulfill() |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + wait(for: [expectation], timeout: 10.0) |
| 60 | + |
| 61 | + // Verify the final state is consistent |
| 62 | + let finalConfig = testConfig.getUserConfig() |
| 63 | + XCTAssertNotNil(finalConfig) |
| 64 | + } |
| 65 | + |
| 66 | + func testConcurrentConfigAccessWithVariables() throws { |
| 67 | + let testConfig = DVCConfig(sdkKey: "test-sdk-key", user: mockUser) |
| 68 | + let mockUserConfig = try getMockUserConfig() |
| 69 | + testConfig.setUserConfig(config: mockUserConfig) |
| 70 | + |
| 71 | + let expectation = XCTestExpectation(description: "Concurrent variable access completed") |
| 72 | + expectation.expectedFulfillmentCount = 200 // 100 reads + 100 writes |
| 73 | + |
| 74 | + let concurrentQueue = DispatchQueue(label: "test.concurrent.variables", attributes: .concurrent) |
| 75 | + |
| 76 | + // Test concurrent reads of variables |
| 77 | + for _ in 0..<100 { |
| 78 | + concurrentQueue.async { |
| 79 | + let variables = testConfig.getUserConfig()?.variables |
| 80 | + XCTAssertNotNil(variables) |
| 81 | + expectation.fulfill() |
| 82 | + } |
| 83 | + } |
| 84 | + let evalConfig = try self.getMockUserConfig(config: "test_config_eval_reason") |
| 85 | + |
| 86 | + // Test concurrent writes with new configs |
| 87 | + for _ in 0..<100 { |
| 88 | + concurrentQueue.async { |
| 89 | + testConfig.setUserConfig(config: evalConfig) |
| 90 | + expectation.fulfill() |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + wait(for: [expectation], timeout: 10.0) |
| 95 | + |
| 96 | + // Verify final state |
| 97 | + let finalConfig = testConfig.getUserConfig() |
| 98 | + XCTAssertNotNil(finalConfig) |
| 99 | + XCTAssertNotNil(finalConfig?.variables) |
| 100 | + } |
| 101 | + |
| 102 | + func testRapidConfigUpdates() throws { |
| 103 | + let testConfig = DVCConfig(sdkKey: "test-sdk-key", user: mockUser) |
| 104 | + |
| 105 | + let expectation = XCTestExpectation(description: "Rapid config updates completed") |
| 106 | + expectation.expectedFulfillmentCount = 1000 |
| 107 | + |
| 108 | + let concurrentQueue = DispatchQueue(label: "test.rapid", attributes: .concurrent) |
| 109 | + let evalConfig = try self.getMockUserConfig(config: "test_config_eval_reason") |
| 110 | + |
| 111 | + // Rapidly update config from multiple threads |
| 112 | + for _ in 0..<1000 { |
| 113 | + concurrentQueue.async { |
| 114 | + testConfig.setUserConfig(config: evalConfig) |
| 115 | + expectation.fulfill() |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + wait(for: [expectation], timeout: 10.0) |
| 120 | + |
| 121 | + // Verify no crashes occurred |
| 122 | + let finalConfig = testConfig.getUserConfig() |
| 123 | + XCTAssertNotNil(finalConfig) |
| 124 | + } |
| 125 | + |
| 126 | + func testConfigAccessDuringUpdates() throws { |
| 127 | + |
| 128 | + let testConfig = DVCConfig(sdkKey: "test-sdk-key", user: mockUser) |
| 129 | + testConfig.setUserConfig(config: try self.getMockUserConfig()) |
| 130 | + |
| 131 | + let expectation = XCTestExpectation(description: "Config access during updates completed") |
| 132 | + expectation.expectedFulfillmentCount = 500 // 250 updates + 250 reads |
| 133 | + |
| 134 | + let updateQueue = DispatchQueue(label: "test.updates", attributes: .concurrent) |
| 135 | + let readQueue = DispatchQueue(label: "test.reads", attributes: .concurrent) |
| 136 | + |
| 137 | + let evalConfig = try self.getMockUserConfig(config: "test_config_eval_reason") |
| 138 | + |
| 139 | + // Continuously update config |
| 140 | + for _ in 0..<250 { |
| 141 | + updateQueue.async { |
| 142 | + testConfig.setUserConfig(config: evalConfig) |
| 143 | + expectation.fulfill() |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + // Continuously read config while updates are happening |
| 148 | + for _ in 0..<250 { |
| 149 | + readQueue.async { |
| 150 | + let config = testConfig.getUserConfig() |
| 151 | + _ = config?.variables |
| 152 | + _ = config?.features |
| 153 | + XCTAssertNotNil(config) |
| 154 | + expectation.fulfill() |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + wait(for: [expectation], timeout: 10.0) |
| 159 | + |
| 160 | + // Verify final state |
| 161 | + let finalConfig = testConfig.getUserConfig() |
| 162 | + XCTAssertNotNil(finalConfig) |
| 163 | + } |
| 164 | + |
| 165 | + func testSetConfigNil() throws { |
| 166 | + let testNilConfig = DVCConfig(sdkKey: "test-sdk-key", user: mockUser) |
| 167 | + testNilConfig.setUserConfig(config: try self.getMockUserConfig()) |
| 168 | + |
| 169 | + let expectation = XCTestExpectation(description: "Config access during setting config to nil completed") |
| 170 | + expectation.expectedFulfillmentCount = 500 // 250 updates + 250 reads |
| 171 | + |
| 172 | + let updateQueue = DispatchQueue(label: "test.updates", attributes: .concurrent) |
| 173 | + let readQueue = DispatchQueue(label: "test.reads", attributes: .concurrent) |
| 174 | + |
| 175 | + let evalConfig = try self.getMockUserConfig(config: "test_config_eval_reason") |
| 176 | + |
| 177 | + // Continuously update config |
| 178 | + for i in 0..<250 { |
| 179 | + updateQueue.async { |
| 180 | + testNilConfig.setUserConfig(config: i % 2 == 0 ? evalConfig : nil) |
| 181 | + expectation.fulfill() |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | + // Continuously read config while updates are happening |
| 186 | + for _ in 0..<250 { |
| 187 | + readQueue.async { |
| 188 | + let config = testNilConfig.getUserConfig() |
| 189 | + _ = config?.variables |
| 190 | + _ = config?.features |
| 191 | + expectation.fulfill() |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + wait(for: [expectation], timeout: 10.0) |
| 196 | + } |
| 197 | +} |
0 commit comments