File tree Expand file tree Collapse file tree 1 file changed +41
-0
lines changed
Expand file tree Collapse file tree 1 file changed +41
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments