|
421 | 421 | #expect(count == 1)
|
422 | 422 | #expect(try String(decoding: Data(contentsOf: .fileURL), as: UTF8.self) == "1")
|
423 | 423 | }
|
| 424 | + |
| 425 | + @Test func twoShareds() async throws { |
| 426 | + let count1URL = URL(fileURLWithPath: NSTemporaryDirectory()) |
| 427 | + .appendingPathComponent("file.json") |
| 428 | + let count2URL = URL(fileURLWithPath: NSTemporaryDirectory() + "/") |
| 429 | + .appendingPathComponent("file.json") |
| 430 | + try? FileManager.default.removeItem(at: count1URL) |
| 431 | + try? FileManager.default.removeItem(at: count2URL) |
| 432 | + |
| 433 | + @Shared(.fileStorage(count1URL)) var count1 = 0 |
| 434 | + @Shared(.fileStorage(count2URL)) var count2 = 0 |
| 435 | + |
| 436 | + $count1.withLock { $0 = 42 } |
| 437 | + #expect(count1 == 42) |
| 438 | + try await Task.sleep(for: .seconds(1.5)) |
| 439 | + #expect(count2 == 42) |
| 440 | + |
| 441 | + $count2.withLock { $0 = 1728 } |
| 442 | + #expect(count2 == 1728) |
| 443 | + try await Task.sleep(for: .seconds(1.5)) |
| 444 | + #expect(count1 == 1728) |
| 445 | + |
| 446 | + $count1.withLock { $0 = 999 } |
| 447 | + #expect(count1 == 999) |
| 448 | + try await Task.sleep(for: .seconds(1.5)) |
| 449 | + #expect(count2 == 999) |
| 450 | + } |
| 451 | + |
| 452 | + @Test func externalAtomicWrite() async throws { |
| 453 | + @Shared(.fileStorage(.fileURL)) var count = 0 |
| 454 | + |
| 455 | + try Data("42".utf8).write(to: .fileURL, options: .atomic) |
| 456 | + try await Task.sleep(for: .seconds(1.5)) |
| 457 | + #expect(count == 42) |
| 458 | + |
| 459 | + try Data("1728".utf8).write(to: .fileURL, options: .atomic) |
| 460 | + try await Task.sleep(for: .seconds(1.5)) |
| 461 | + #expect(count == 1728) |
| 462 | + |
| 463 | + $count.withLock { $0 = 999 } |
| 464 | + try await Task.sleep(for: .seconds(1.5)) |
| 465 | + #expect(count == 999) |
| 466 | + #expect(try String(decoding: Data(contentsOf: .fileURL), as: UTF8.self) == "999") |
| 467 | + } |
424 | 468 | }
|
425 | 469 | }
|
426 | 470 |
|
|
0 commit comments