File tree Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Expand file tree Collapse file tree 3 files changed +30
-1
lines changed Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ public class TaskStore<Key: Hashable> {
46
46
/// - Parameter predicate: <#predicate description#>
47
47
/// - Returns: <#description#>
48
48
public func tasks( where predicate: ( Key ) -> Bool ) -> [ CancellableTask ] {
49
+ lock. lock ( ) ; defer { lock. unlock ( ) }
49
50
return taskMap
50
51
. filter { key, _ in predicate ( key) }
51
52
. map ( \. value)
Original file line number Diff line number Diff line change @@ -3,6 +3,29 @@ import XCTest
3
3
4
4
final class TaskStoreTests : XCTestCase {
5
5
6
+ func test_concurrentAccess( ) {
7
+ let taskStore = TaskStore < String > ( )
8
+ let task = Task {
9
+ sleep ( 1 )
10
+ throw TestError . test
11
+ }
12
+ taskStore. setTask ( task, forKey: " TestKey " )
13
+
14
+ ( 1 ... 100 ) . forEach { _ in
15
+ DispatchQueue . global ( ) . async {
16
+ taskStore. setTask ( task, forKey: " TestKey " )
17
+ _ = taskStore. task ( forKey: " TestKey " )
18
+ }
19
+ }
20
+
21
+ ( 1 ... 100 ) . forEach { _ in
22
+ DispatchQueue . global ( ) . async {
23
+ taskStore. setTask ( task, forKey: " TestKey " )
24
+ _ = taskStore. tasks ( where: { $0 == " TestKey " } )
25
+ }
26
+ }
27
+ }
28
+
6
29
func test_subscript( ) {
7
30
let taskStore = TaskStore < Int > ( )
8
31
taskStore. setTask (
@@ -14,7 +37,7 @@ final class TaskStoreTests: XCTestCase {
14
37
XCTAssertNotNil ( taskStore [ 0 ] )
15
38
}
16
39
17
- func test_searchTasks ( ) {
40
+ func test_searchTasksWithPredicate ( ) {
18
41
struct CustomKey : Hashable {
19
42
let number : Int
20
43
}
Original file line number Diff line number Diff line change
1
+ import Foundation
2
+
3
+ enum TestError : Error {
4
+ case test
5
+ }
You can’t perform that action at this time.
0 commit comments