Skip to content

Commit d556b36

Browse files
glbrnttMrMage
authored andcommitted
Make recording state in tests thread-safe (#521)
Motivation: When the tests are run with TSAN, it complained about concurrent modification of states in the connectivity state recording delegate. Modifications: Guard access to the recorded states via a lock. Result: TSAN doesn't complain when running the tests.
1 parent 42af0d7 commit d556b36

File tree

1 file changed

+18
-4
lines changed

1 file changed

+18
-4
lines changed

Tests/GRPCTests/ClientConnectionBackoffTests.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,27 @@ import Foundation
1717
import GRPC
1818
import NIO
1919
import XCTest
20+
import NIOConcurrencyHelpers
2021

2122
class ConnectivityStateCollectionDelegate: ConnectivityStateDelegate {
22-
var states: [ConnectivityState] = []
23+
private var _states: [ConnectivityState] = []
24+
private var lock = Lock()
25+
26+
var states: [ConnectivityState] {
27+
get {
28+
return self.lock.withLock {
29+
return self._states
30+
}
31+
}
32+
}
2333

2434
func clearStates() -> [ConnectivityState] {
35+
self.lock.lock()
2536
defer {
26-
self.states = []
37+
self._states.removeAll()
38+
self.lock.unlock()
2739
}
28-
return self.states
40+
return self._states
2941
}
3042

3143
var idleExpectation: XCTestExpectation?
@@ -49,7 +61,9 @@ class ConnectivityStateCollectionDelegate: ConnectivityStateDelegate {
4961
}
5062

5163
func connectivityStateDidChange(from oldState: ConnectivityState, to newState: ConnectivityState) {
52-
self.states.append(newState)
64+
self.lock.withLockVoid {
65+
self._states.append(newState)
66+
}
5367

5468
switch newState {
5569
case .idle:

0 commit comments

Comments
 (0)