|
17 | 17 | @Shared(.fileStorage(.fileURL)) var users = [User]()
|
18 | 18 | #expect($users.loadError == nil)
|
19 | 19 | expectNoDifference(
|
20 |
| - fileSystem.value, [.fileURL: Data("co.pointfree.Sharing.FileStorage.stub".utf8)] |
| 20 | + fileSystem.value, [.fileURL: Data()] |
21 | 21 | )
|
22 | 22 | $users.withLock { $0.append(.blob) }
|
23 | 23 | try expectNoDifference(fileSystem.value.users(for: .fileURL), [.blob])
|
|
31 | 31 | @Shared(.utf8String) var string = ""
|
32 | 32 | #expect($string.loadError == nil)
|
33 | 33 | expectNoDifference(
|
34 |
| - fileSystem.value, [.utf8StringURL: Data("co.pointfree.Sharing.FileStorage.stub".utf8)] |
| 34 | + fileSystem.value, [.utf8StringURL: Data()] |
35 | 35 | )
|
36 | 36 | $string.withLock { $0 = "hello" }
|
37 | 37 | expectNoDifference(
|
|
269 | 269 | }
|
270 | 270 | }
|
271 | 271 |
|
| 272 | + @Test func moveFileThenWrite() async throws { |
| 273 | + try await withMainSerialExecutor { |
| 274 | + try JSONEncoder().encode([User.blob]).write(to: .fileURL) |
| 275 | + |
| 276 | + @Shared(.fileStorage(.fileURL)) var users = [User]() |
| 277 | + await Task.yield() |
| 278 | + expectNoDifference(users, [.blob]) |
| 279 | + |
| 280 | + try FileManager.default.moveItem(at: .fileURL, to: .anotherFileURL) |
| 281 | + try await Task.sleep(nanoseconds: 100_000_000) |
| 282 | + expectNoDifference(users, []) |
| 283 | + |
| 284 | + try JSONEncoder().encode([User.blobEsq]).write(to: .fileURL) |
| 285 | + try await Task.sleep(nanoseconds: 1_000_000_000) |
| 286 | + expectNoDifference(users, [.blobEsq]) |
| 287 | + } |
| 288 | + } |
| 289 | + |
272 | 290 | @Test func testDeleteFileThenWriteToFile() async throws {
|
273 | 291 | try await withMainSerialExecutor {
|
274 | 292 | try JSONEncoder().encode([User.blob]).write(to: .fileURL)
|
|
299 | 317 | try await Task.sleep(nanoseconds: 1_200_000_000)
|
300 | 318 | expectNoDifference(users, [.blob])
|
301 | 319 | #expect(
|
302 |
| - try Data(contentsOf: .fileURL) == Data("co.pointfree.Sharing.FileStorage.stub".utf8) |
| 320 | + try Data(contentsOf: .fileURL) == Data() |
303 | 321 | )
|
304 | 322 | }
|
305 | 323 | }
|
|
356 | 374 | @MainActor
|
357 | 375 | @Test func multipleMutations() async throws {
|
358 | 376 | @Shared(.counts) var counts
|
359 |
| - for m in 1...1000 { |
360 |
| - for n in 1...10 { |
| 377 | + let iterations = 1_000 |
| 378 | + let buckets = 10 |
| 379 | + for m in 1...iterations { |
| 380 | + for n in 1...buckets { |
361 | 381 | $counts.withLock {
|
362 | 382 | $0[n, default: 0] += 1
|
363 | 383 | }
|
364 | 384 | }
|
365 | 385 | expectNoDifference(
|
366 |
| - Dictionary((1...10).map { n in (n, m) }, uniquingKeysWith: { $1 }), |
| 386 | + Dictionary((1...buckets).map { n in (n, m) }, uniquingKeysWith: { $1 }), |
367 | 387 | counts
|
368 | 388 | )
|
369 | 389 | try await Task.sleep(nanoseconds: 1_000_000)
|
|
386 | 406 |
|
387 | 407 | #expect(counts[0] == 10_000)
|
388 | 408 | }
|
| 409 | + |
| 410 | + @Test func emptyData() throws { |
| 411 | + try? FileManager.default.removeItem(at: .fileURL) |
| 412 | + try Data().write(to: .fileURL) |
| 413 | + @Shared(.fileStorage(.fileURL)) var count = 0 |
| 414 | + #expect(count == 0) |
| 415 | + } |
| 416 | + |
| 417 | + @Test func corruptData() async throws { |
| 418 | + try? FileManager.default.removeItem(at: .fileURL) |
| 419 | + try Data("corrupted".utf8).write(to: .fileURL) |
| 420 | + @Shared(value: 0) var count: Int |
| 421 | + withKnownIssue { |
| 422 | + $count = Shared(wrappedValue: 0, .fileStorage(.fileURL)) |
| 423 | + } matching: { |
| 424 | + $0.description.hasPrefix(""" |
| 425 | + Caught error: dataCorrupted(Swift.DecodingError.Context(codingPath: [], \ |
| 426 | + debugDescription: "The given data was not valid JSON.", underlyingError: \ |
| 427 | + Optional(Error Domain=NSCocoaErrorDomain Code=3840 "Unexpected character |
| 428 | + """) |
| 429 | + } |
| 430 | + #expect(count == 0) |
| 431 | + $count.withLock { $0 = 1 } |
| 432 | + try await Task.sleep(for: .seconds(0.01)) |
| 433 | + #expect(count == 1) |
| 434 | + #expect(try String(decoding: Data(contentsOf: .fileURL), as: UTF8.self) == "1") |
| 435 | + } |
389 | 436 | }
|
390 | 437 | }
|
391 | 438 |
|
392 | 439 | extension [URL: Data] {
|
393 | 440 | fileprivate func users(for url: URL) throws -> [User]? {
|
394 | 441 | guard
|
395 | 442 | let data = self[url],
|
396 |
| - data != Data("co.pointfree.Sharing.FileStorage.stub".utf8) |
| 443 | + !data.isEmpty |
397 | 444 | else { return nil }
|
398 | 445 | return try JSONDecoder().decode([User].self, from: data)
|
399 | 446 | }
|
|
0 commit comments