1
- import XCTest
1
+ import Testing
2
+ import Foundation
2
3
@testable import DataCacheKit
3
4
4
5
func yield( until condition: @autoclosure ( ) async -> Bool , message: @autoclosure ( ) -> String ? = nil , limit: Int = 10000 ) async throws {
@@ -16,8 +17,7 @@ func yield(until condition: @autoclosure () async -> Bool, message: @autoclosure
16
17
throw E ( errorDescription: message ( ) )
17
18
}
18
19
19
- @MainActor
20
- final class DiskCacheTests : XCTestCase {
20
+ final class DiskCacheTests {
21
21
private var tmpDir : URL !
22
22
private var numberOfItems : Int {
23
23
( try ? FileManager . default. contentsOfDirectory ( atPath: tmpDir. path) . count) ?? 0
@@ -28,21 +28,19 @@ final class DiskCacheTests: XCTestCase {
28
28
return options
29
29
}
30
30
31
- override func setUp( ) async throws {
32
- try await super. setUp ( )
31
+ init ( ) async throws {
33
32
tmpDir = URL ( fileURLWithPath: NSTemporaryDirectory ( ) ) . appendingPathComponent ( UUID ( ) . uuidString)
34
33
print ( tmpDir. absoluteString)
35
- XCTAssertEqual ( numberOfItems, 0 )
34
+ #expect ( numberOfItems == 0 )
36
35
}
37
36
38
- override func tearDown( ) async throws {
39
- try await super. tearDown ( )
37
+ deinit {
40
38
try ? FileManager . default. removeItem ( at: tmpDir)
41
39
}
42
40
41
+ @Test
42
+ @available ( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * )
43
43
func testStoreData( ) async throws {
44
- guard #available( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * ) else { return }
45
-
46
44
let clock = ManualClock ( )
47
45
let cache = DiskCache < String > ( options: cacheOptions ( ) , clock: clock, logger: . init( . default) )
48
46
@@ -53,39 +51,35 @@ final class DiskCacheTests: XCTestCase {
53
51
do {
54
52
// load from staging (memory)
55
53
let data = try await cache. value ( for: " empty " )
56
- XCTAssertNotNil ( data)
54
+ #expect ( data != nil )
57
55
58
- let url = try XCTUnwrap ( cache. url ( for: " empty " ) )
59
- XCTAssertFalse ( FileManager . default. fileExists ( atPath: url. path) )
60
- } catch {
61
- XCTFail ( " \( error) " )
56
+ let url = try #require( cache. url ( for: " empty " ) )
57
+ #expect( !FileManager. default. fileExists ( atPath: url. path) )
62
58
}
63
59
64
60
clock. advance ( by: . milliseconds( 500 ) )
65
- try ? await SuspendingClock ( ) . sleep ( until: . now. advanced ( by: . microseconds( 300 ) ) )
66
61
67
- XCTAssertEqual ( numberOfItems, 0 )
62
+ #expect ( numberOfItems == 0 )
68
63
69
64
clock. advance ( by: . milliseconds( 500 ) )
70
- try ? await SuspendingClock ( ) . sleep ( until: . now. advanced ( by: . microseconds( 300 ) ) )
71
65
72
- XCTAssertEqual ( numberOfItems, 1 )
66
+ try await yield ( until: await !cache. isFlushScheduled)
67
+
68
+ #expect( numberOfItems == 1 )
73
69
74
70
do {
75
71
// load from disk
76
72
let data = try await cache. value ( for: " empty " )
77
- XCTAssertNotNil ( data)
73
+ #expect ( data != nil )
78
74
79
- let url = try XCTUnwrap ( cache. url ( for: " empty " ) )
80
- XCTAssertTrue ( FileManager . default. fileExists ( atPath: url. path) )
81
- } catch {
82
- XCTFail ( " \( error) " )
75
+ let url = try #require( cache. url ( for: " empty " ) )
76
+ #expect( FileManager . default. fileExists ( atPath: url. path) )
83
77
}
84
78
}
85
79
80
+ @Test
81
+ @available ( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * )
86
82
func testStoreDataMultiple( ) async throws {
87
- guard #available( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * ) else { return }
88
-
89
83
let clock = ManualClock ( )
90
84
let cache = DiskCache < String > ( options: cacheOptions ( ) , clock: clock, logger: . init( . default) )
91
85
@@ -97,19 +91,19 @@ final class DiskCacheTests: XCTestCase {
97
91
do {
98
92
cache. logger. debug ( " check staging items " )
99
93
let count = await cache. staging. stages. first? . changes. count
100
- XCTAssertEqual ( count, 2 )
94
+ #expect ( count == 2 )
101
95
}
102
96
103
97
clock. advance ( by: . milliseconds( 1000 ) )
104
98
105
99
try ? await cache. flushingTask? . value
106
100
107
- XCTAssertEqual ( numberOfItems, 2 )
101
+ #expect ( numberOfItems == 2 )
108
102
}
109
103
104
+ @Test
105
+ @available ( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * )
110
106
func testRemoveData( ) async throws {
111
- guard #available( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * ) else { return }
112
-
113
107
let clock = ManualClock ( )
114
108
let cache = DiskCache < String > ( options: cacheOptions ( ) , clock: clock, logger: . init( . default) )
115
109
@@ -122,31 +116,29 @@ final class DiskCacheTests: XCTestCase {
122
116
do {
123
117
let data0 = try await cache. value ( for: " item0 " )
124
118
let data1 = try await cache. value ( for: " item1 " )
125
- XCTAssertNil ( data0)
126
- XCTAssertEqual ( data1, Data ( [ 1 , 2 ] ) )
127
- } catch {
128
- XCTFail ( " \( error) " )
119
+ #expect( data0 == nil )
120
+ #expect( data1 == Data ( [ 1 , 2 ] ) )
129
121
}
130
122
131
123
do {
132
124
cache. logger. debug ( " check staging layers " )
133
125
let count = await cache. staging. stages. count
134
- XCTAssertEqual ( count, 2 )
126
+ #expect ( count == 2 )
135
127
136
128
let change0 = await cache. staging. stages. last? . changes [ " item0 " ] ? . operation
137
- XCTAssertEqual ( change0, . remove)
129
+ #expect ( change0 == . remove)
138
130
}
139
131
140
132
clock. advance ( by: . milliseconds( 1000 ) )
141
133
142
134
try ? await cache. flushingTask? . value
143
135
144
- XCTAssertEqual ( numberOfItems, 1 )
136
+ #expect ( numberOfItems == 1 )
145
137
}
146
138
139
+ @Test
140
+ @available ( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * )
147
141
func testRemoveDataAll( ) async throws {
148
- guard #available( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * ) else { return }
149
-
150
142
let clock = ManualClock ( )
151
143
let cache = DiskCache < String > ( options: cacheOptions ( ) , clock: clock, logger: . init( . default) )
152
144
@@ -158,13 +150,11 @@ final class DiskCacheTests: XCTestCase {
158
150
do {
159
151
try ? await cache. flushingTask? . value
160
152
let data0 = try await cache. value ( for: " item0 " )
161
- XCTAssertEqual ( data0, Data ( [ 1 ] ) )
162
- XCTAssertEqual ( numberOfItems, 1 )
153
+ #expect ( data0 == Data ( [ 1 ] ) )
154
+ #expect ( numberOfItems == 1 )
163
155
164
156
let isEmpty = await cache. staging. stages. isEmpty
165
- XCTAssertTrue ( isEmpty)
166
- } catch {
167
- XCTFail ( " \( error) " )
157
+ #expect( isEmpty)
168
158
}
169
159
170
160
cache. removeAll ( )
@@ -174,23 +164,21 @@ final class DiskCacheTests: XCTestCase {
174
164
175
165
do {
176
166
var isEmpty = await cache. staging. stages. isEmpty
177
- XCTAssertFalse ( isEmpty)
167
+ #expect ( ! isEmpty)
178
168
179
169
try ? await cache. flushingTask? . value
180
170
let data0 = try await cache. value ( for: " item0 " )
181
- XCTAssertNil ( data0)
182
- XCTAssertEqual ( numberOfItems, 0 )
171
+ #expect ( data0 == nil )
172
+ #expect ( numberOfItems == 0 )
183
173
184
174
isEmpty = await cache. staging. stages. isEmpty
185
- XCTAssertTrue ( isEmpty)
186
- } catch {
187
- XCTFail ( " \( error) " )
175
+ #expect( isEmpty)
188
176
}
189
177
}
190
178
179
+ @Test
180
+ @available ( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * )
191
181
func testSweep( ) async throws {
192
- guard #available( macOS 13 . 0 , iOS 16 . 0 , watchOS 9 . 0 , tvOS 16 . 0 , * ) else { return }
193
-
194
182
let allocationUnit = 4096
195
183
196
184
var options = cacheOptions ( ) as DiskCache < String > . Options
@@ -211,9 +199,9 @@ final class DiskCacheTests: XCTestCase {
211
199
try ? await cache. flushingTask? . value
212
200
213
201
let data2 = try ? await cache. value ( for: " item2 " )
214
- XCTAssertEqual ( data2, Data ( [ 1 , 2 , 3 ] ) )
202
+ #expect ( data2 == Data ( [ 1 , 2 , 3 ] ) )
215
203
216
- XCTAssertEqual ( numberOfItems, 3 )
204
+ #expect ( numberOfItems == 3 )
217
205
}
218
206
219
207
do {
@@ -227,14 +215,12 @@ final class DiskCacheTests: XCTestCase {
227
215
228
216
try ? await cache. sweepingTask? . value
229
217
230
- XCTAssertEqual ( numberOfItems, 2 )
218
+ #expect ( numberOfItems == 2 )
231
219
232
220
let data0 = try ? await cache. value ( for: " item0 " )
233
221
let data1 = try ? await cache. value ( for: " item2 " )
234
- XCTAssertEqual ( data0, Data ( [ 1 ] ) )
235
- XCTAssertEqual ( data1, Data ( [ 1 , 2 , 3 ] ) )
236
- } catch {
237
- XCTFail ( " \( error) " )
222
+ #expect( data0 == Data ( [ 1 ] ) )
223
+ #expect( data1 == Data ( [ 1 , 2 , 3 ] ) )
238
224
}
239
225
}
240
226
}
0 commit comments