Ver código fonte

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.
George Barnett 6 anos atrás
pai
commit
d556b362cf
1 arquivos alterados com 18 adições e 4 exclusões
  1. 18 4
      Tests/GRPCTests/ClientConnectionBackoffTests.swift

+ 18 - 4
Tests/GRPCTests/ClientConnectionBackoffTests.swift

@@ -17,15 +17,27 @@ import Foundation
 import GRPC
 import NIO
 import XCTest
+import NIOConcurrencyHelpers
 
 class ConnectivityStateCollectionDelegate: ConnectivityStateDelegate {
-  var states: [ConnectivityState] = []
+  private var _states: [ConnectivityState] = []
+  private var lock = Lock()
+
+  var states: [ConnectivityState] {
+    get {
+      return self.lock.withLock {
+        return self._states
+      }
+    }
+  }
 
   func clearStates() -> [ConnectivityState] {
+    self.lock.lock()
     defer {
-      self.states = []
+      self._states.removeAll()
+      self.lock.unlock()
     }
-    return self.states
+    return self._states
   }
 
   var idleExpectation: XCTestExpectation?
@@ -49,7 +61,9 @@ class ConnectivityStateCollectionDelegate: ConnectivityStateDelegate {
   }
 
   func connectivityStateDidChange(from oldState: ConnectivityState, to newState: ConnectivityState) {
-    self.states.append(newState)
+    self.lock.withLockVoid {
+      self._states.append(newState)
+    }
 
     switch newState {
     case .idle: