Skip to content

Commit 130ed26

Browse files
authored
Merge pull request #20 from swiftty/feature/swift6
Feature/swift6
2 parents d33d994 + 557b90b commit 130ed26

File tree

4 files changed

+75
-85
lines changed

4 files changed

+75
-85
lines changed

.github/workflows/ci.yml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@ name: ci
22

33
on:
44
push:
5-
branches: [ "main" ]
5+
branches: ["main"]
66
pull_request:
7-
branches: [ "main" ]
7+
branches: ["main"]
88
workflow_dispatch:
99

1010
jobs:
1111
mac-os:
12-
runs-on: macos-13
12+
runs-on: macos-15
1313
strategy:
1414
matrix:
15-
xcode: [ "14.1" ]
15+
xcode: ["16.0"]
1616
steps:
1717
- uses: maxim-lobanov/setup-xcode@v1
1818
with:
@@ -22,7 +22,6 @@ jobs:
2222
run: swift build
2323
- name: Run tests
2424
run: swift test
25-
2625
# linux:
2726
# runs-on: ubuntu-latest
2827
# strategy:

Package.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// swift-tools-version: 5.7
1+
// swift-tools-version: 6.0
22
// The swift-tools-version declares the minimum version of Swift required to build this package.
33

44
import PackageDescription

Sources/DataCacheKit/DiskCache.swift

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,26 +43,7 @@ public final class DiskCache<Key: Hashable & Sendable>: Caching, @unchecked Send
4343
}
4444
}
4545
private var _path: URL!
46-
47-
private lazy var _prepare: () throws -> Void = {
48-
let dir: URL?
49-
switch options.path {
50-
case .default(let name):
51-
dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent(name)
52-
53-
case .custom(let url):
54-
dir = url
55-
}
56-
_path = dir
57-
Task {
58-
await scheduleSweep(after: 10)
59-
}
60-
return {
61-
if dir == nil {
62-
throw CocoaError(.fileNoSuchFile)
63-
}
64-
}
65-
}()
46+
private var prepared = false
6647

6748
private let queueingLock = NSLock()
6849
private var queueingTask: Task<Void, Never>?
@@ -206,6 +187,30 @@ public final class DiskCache<Key: Hashable & Sendable>: Caching, @unchecked Send
206187
}
207188

208189
// MARK: -
190+
private func _prepare() throws {
191+
if prepared {
192+
return
193+
}
194+
defer { prepared = true }
195+
196+
let dir: URL?
197+
switch options.path {
198+
case .default(let name):
199+
dir = FileManager.default.urls(for: .cachesDirectory, in: .userDomainMask).first?.appendingPathComponent(name)
200+
201+
case .custom(let url):
202+
dir = url
203+
}
204+
_path = dir
205+
Task {
206+
await scheduleSweep(after: 10)
207+
}
208+
209+
if dir == nil {
210+
throw CocoaError(.fileNoSuchFile)
211+
}
212+
}
213+
209214
@DiskCacheActor
210215
private func _storeData(_ data: Data, for key: Key) async {
211216
logger.debug("\(self.logKey)store data: \(data) for \(String(describing: key))")
Lines changed: 45 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import XCTest
1+
import Testing
2+
import Foundation
23
@testable import DataCacheKit
34

45
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
1617
throw E(errorDescription: message())
1718
}
1819

19-
@MainActor
20-
final class DiskCacheTests: XCTestCase {
20+
final class DiskCacheTests {
2121
private var tmpDir: URL!
2222
private var numberOfItems: Int {
2323
(try? FileManager.default.contentsOfDirectory(atPath: tmpDir.path).count) ?? 0
@@ -28,21 +28,19 @@ final class DiskCacheTests: XCTestCase {
2828
return options
2929
}
3030

31-
override func setUp() async throws {
32-
try await super.setUp()
31+
init() async throws {
3332
tmpDir = URL(fileURLWithPath: NSTemporaryDirectory()).appendingPathComponent(UUID().uuidString)
3433
print(tmpDir.absoluteString)
35-
XCTAssertEqual(numberOfItems, 0)
34+
#expect(numberOfItems == 0)
3635
}
3736

38-
override func tearDown() async throws {
39-
try await super.tearDown()
37+
deinit {
4038
try? FileManager.default.removeItem(at: tmpDir)
4139
}
4240

41+
@Test
42+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
4343
func testStoreData() async throws {
44-
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { return }
45-
4644
let clock = ManualClock()
4745
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
4846

@@ -53,39 +51,35 @@ final class DiskCacheTests: XCTestCase {
5351
do {
5452
// load from staging (memory)
5553
let data = try await cache.value(for: "empty")
56-
XCTAssertNotNil(data)
54+
#expect(data != nil)
5755

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))
6258
}
6359

6460
clock.advance(by: .milliseconds(500))
65-
try? await SuspendingClock().sleep(until: .now.advanced(by: .microseconds(300)))
6661

67-
XCTAssertEqual(numberOfItems, 0)
62+
#expect(numberOfItems == 0)
6863

6964
clock.advance(by: .milliseconds(500))
70-
try? await SuspendingClock().sleep(until: .now.advanced(by: .microseconds(300)))
7165

72-
XCTAssertEqual(numberOfItems, 1)
66+
try await yield(until: await !cache.isFlushScheduled)
67+
68+
#expect(numberOfItems == 1)
7369

7470
do {
7571
// load from disk
7672
let data = try await cache.value(for: "empty")
77-
XCTAssertNotNil(data)
73+
#expect(data != nil)
7874

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))
8377
}
8478
}
8579

80+
@Test
81+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
8682
func testStoreDataMultiple() async throws {
87-
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { return }
88-
8983
let clock = ManualClock()
9084
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
9185

@@ -97,19 +91,19 @@ final class DiskCacheTests: XCTestCase {
9791
do {
9892
cache.logger.debug("check staging items")
9993
let count = await cache.staging.stages.first?.changes.count
100-
XCTAssertEqual(count, 2)
94+
#expect(count == 2)
10195
}
10296

10397
clock.advance(by: .milliseconds(1000))
10498

10599
try? await cache.flushingTask?.value
106100

107-
XCTAssertEqual(numberOfItems, 2)
101+
#expect(numberOfItems == 2)
108102
}
109103

104+
@Test
105+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
110106
func testRemoveData() async throws {
111-
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { return }
112-
113107
let clock = ManualClock()
114108
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
115109

@@ -122,31 +116,29 @@ final class DiskCacheTests: XCTestCase {
122116
do {
123117
let data0 = try await cache.value(for: "item0")
124118
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]))
129121
}
130122

131123
do {
132124
cache.logger.debug("check staging layers")
133125
let count = await cache.staging.stages.count
134-
XCTAssertEqual(count, 2)
126+
#expect(count == 2)
135127

136128
let change0 = await cache.staging.stages.last?.changes["item0"]?.operation
137-
XCTAssertEqual(change0, .remove)
129+
#expect(change0 == .remove)
138130
}
139131

140132
clock.advance(by: .milliseconds(1000))
141133

142134
try? await cache.flushingTask?.value
143135

144-
XCTAssertEqual(numberOfItems, 1)
136+
#expect(numberOfItems == 1)
145137
}
146138

139+
@Test
140+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
147141
func testRemoveDataAll() async throws {
148-
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { return }
149-
150142
let clock = ManualClock()
151143
let cache = DiskCache<String>(options: cacheOptions(), clock: clock, logger: .init(.default))
152144

@@ -158,13 +150,11 @@ final class DiskCacheTests: XCTestCase {
158150
do {
159151
try? await cache.flushingTask?.value
160152
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)
163155

164156
let isEmpty = await cache.staging.stages.isEmpty
165-
XCTAssertTrue(isEmpty)
166-
} catch {
167-
XCTFail("\(error)")
157+
#expect(isEmpty)
168158
}
169159

170160
cache.removeAll()
@@ -174,23 +164,21 @@ final class DiskCacheTests: XCTestCase {
174164

175165
do {
176166
var isEmpty = await cache.staging.stages.isEmpty
177-
XCTAssertFalse(isEmpty)
167+
#expect(!isEmpty)
178168

179169
try? await cache.flushingTask?.value
180170
let data0 = try await cache.value(for: "item0")
181-
XCTAssertNil(data0)
182-
XCTAssertEqual(numberOfItems, 0)
171+
#expect(data0 == nil)
172+
#expect(numberOfItems == 0)
183173

184174
isEmpty = await cache.staging.stages.isEmpty
185-
XCTAssertTrue(isEmpty)
186-
} catch {
187-
XCTFail("\(error)")
175+
#expect(isEmpty)
188176
}
189177
}
190178

179+
@Test
180+
@available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *)
191181
func testSweep() async throws {
192-
guard #available(macOS 13.0, iOS 16.0, watchOS 9.0, tvOS 16.0, *) else { return }
193-
194182
let allocationUnit = 4096
195183

196184
var options = cacheOptions() as DiskCache<String>.Options
@@ -211,9 +199,9 @@ final class DiskCacheTests: XCTestCase {
211199
try? await cache.flushingTask?.value
212200

213201
let data2 = try? await cache.value(for: "item2")
214-
XCTAssertEqual(data2, Data([1, 2, 3]))
202+
#expect(data2 == Data([1, 2, 3]))
215203

216-
XCTAssertEqual(numberOfItems, 3)
204+
#expect(numberOfItems == 3)
217205
}
218206

219207
do {
@@ -227,14 +215,12 @@ final class DiskCacheTests: XCTestCase {
227215

228216
try? await cache.sweepingTask?.value
229217

230-
XCTAssertEqual(numberOfItems, 2)
218+
#expect(numberOfItems == 2)
231219

232220
let data0 = try? await cache.value(for: "item0")
233221
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]))
238224
}
239225
}
240226
}

0 commit comments

Comments
 (0)