Skip to content

Commit 8560fb5

Browse files
authored
Update AppStateTests.swift
1 parent d47eef6 commit 8560fb5

File tree

1 file changed

+0
-104
lines changed

1 file changed

+0
-104
lines changed

Tests/AppStateTests/AppStateTests.swift

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -190,108 +190,4 @@ final class AppStateTests: XCTestCase {
190190
XCTAssertTrue(Application.isLoggingEnabled)
191191
Application.logger.debug("This should be logged again from testLoggingToggle.")
192192
}
193-
194-
@MainActor
195-
func testConcurrentWrites() async {
196-
let iterations = 1000
197-
let concurrentWriters = 5
198-
let keyPath = \Application.count
199-
let targetValueBase = 999
200-
201-
// Since testConcurrentWrites is @MainActor, `Application.state(keyPath)` is a synchronous call.
202-
var countState = Application.state(keyPath)
203-
countState.value = 0 // This is also synchronous as countState is a local var.
204-
205-
await withTaskGroup(of: Void.self) { group in
206-
for writerId in 0..<concurrentWriters {
207-
group.addTask { // These tasks are nonisolated
208-
for i in 0..<iterations {
209-
let valueToWrite = targetValueBase + writerId
210-
// Perform state mutation on the MainActor
211-
await MainActor.run {
212-
var state = Application.state(keyPath) // Accessing @MainActor static var
213-
state.value = valueToWrite // Accessing @MainActor .value
214-
}
215-
216-
if i % 10 == 0 { await Task.yield() }
217-
}
218-
}
219-
}
220-
}
221-
222-
// After all writes, the value should be one of the values written by the writers.
223-
// Accessing state and its value on the MainActor.
224-
let finalValue = await MainActor.run { Application.state(keyPath).value }
225-
let possibleValues = (0..<concurrentWriters).map { targetValueBase + $0 }
226-
XCTAssertTrue(possibleValues.contains(finalValue), "Final value \(finalValue) is not one of the expected written values \(possibleValues). This might indicate a race condition in the set operation or the test logic itself.")
227-
}
228-
229-
@MainActor
230-
func testConcurrentReadsAndWrites() async {
231-
let keyPath = \Application.username
232-
let writeValuePrefix = "ConsistentValue"
233-
let iterations = 1000
234-
let numReaders = 5
235-
236-
// Since test function is @MainActor, these are synchronous calls.
237-
var usernameState = Application.state(keyPath)
238-
usernameState.value = "Initial"
239-
240-
await withTaskGroup(of: Void.self) { group in
241-
// Writer Task
242-
group.addTask { // nonisolated task
243-
for i in 0..<iterations {
244-
await MainActor.run {
245-
var state = Application.state(keyPath)
246-
state.value = "\(writeValuePrefix)_\(i)"
247-
}
248-
if i % 10 == 0 { await Task.yield() }
249-
}
250-
await MainActor.run {
251-
var finalState = Application.state(keyPath)
252-
finalState.value = "FinalStableValue"
253-
}
254-
}
255-
256-
// Reader Tasks
257-
for readerId in 0..<numReaders {
258-
group.addTask { // nonisolated task
259-
var reads = 0
260-
var sawFinal = false
261-
while reads < iterations {
262-
let currentValue = await MainActor.run { Application.state(keyPath).value }
263-
XCTAssertNotNil(currentValue, "Reader \(readerId) read a nil value unexpectedly.")
264-
if currentValue == "FinalStableValue" {
265-
sawFinal = true
266-
break
267-
}
268-
reads += 1
269-
await Task.yield()
270-
}
271-
272-
if !sawFinal {
273-
for _ in 0..<20 {
274-
let valueOnMain = await MainActor.run { Application.state(keyPath).value }
275-
if valueOnMain == "FinalStableValue" {
276-
sawFinal = true
277-
break
278-
}
279-
do {
280-
try await Task.sleep(nanoseconds: 20_000_000)
281-
} catch {
282-
XCTFail("Task.sleep threw an error in reader \(readerId): \(error)")
283-
}
284-
}
285-
}
286-
await MainActor.run {
287-
XCTAssertEqual(Application.state(keyPath).value, "FinalStableValue", "Reader \(readerId) did not observe 'FinalStableValue' as the final state.")
288-
}
289-
}
290-
}
291-
}
292-
293-
// Final assertion after task group completion, already on MainActor.
294-
let finalValue = Application.state(keyPath).value
295-
XCTAssertEqual(finalValue, "FinalStableValue", "The final state was not the expected 'FinalStableValue'.")
296-
}
297193
}

0 commit comments

Comments
 (0)