Skip to content

Commit d746359

Browse files
authored
Add concurrency stress tests (#21)
* Add thread safety tests * Update ThreadSafetyTests.swift * Update ThreadSafetyTests.swift * Update ThreadSafetyTests.swift
1 parent 74a0f03 commit d746359

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import XCTest
2+
@testable import Cache
3+
4+
final class ThreadSafetyTests: XCTestCase {
5+
func testCacheConcurrentAccess() {
6+
let iterations = 1000
7+
let cache = Cache<Int, Int>()
8+
9+
DispatchQueue.concurrentPerform(iterations: iterations) { i in
10+
cache.set(value: i, forKey: i)
11+
XCTAssertEqual(cache.get(i), i)
12+
cache.remove(i)
13+
}
14+
15+
XCTAssertEqual(cache.allValues.count, 0)
16+
}
17+
18+
func testLRUCacheConcurrentAccess() {
19+
let iterations = 500
20+
let cache = LRUCache<Int, Int>(capacity: 500)
21+
22+
DispatchQueue.concurrentPerform(iterations: iterations) { i in
23+
cache.set(value: i, forKey: i)
24+
XCTAssertEqual(cache.get(i), i)
25+
}
26+
27+
XCTAssertEqual(cache.allValues.count, 500)
28+
}
29+
30+
func testExpiringCacheConcurrentAccess() {
31+
let iterations = 200
32+
let cache = ExpiringCache<Int, Int>(duration: .hours(1))
33+
34+
DispatchQueue.concurrentPerform(iterations: iterations) { i in
35+
cache.set(value: i, forKey: i)
36+
XCTAssertEqual(cache.get(i), i)
37+
}
38+
39+
XCTAssertEqual(cache.allValues.count, iterations)
40+
}
41+
}

0 commit comments

Comments
 (0)