Browse Source

Fix thread-unsafety bug in tests (#1126)

Motivation:

Some of tests rely on a delegate for recording errors, the delegate is
called from the EventLoop which is in most cases a different thread to
that accessing the recorded errors.

Modifications:

- Guard access to the errors behind a lock

Result:

No unsafe accesses to the errors recorded using that delegate in tests.
George Barnett 5 years ago
parent
commit
e8dc247d60
1 changed files with 14 additions and 2 deletions
  1. 14 2
      Tests/GRPCTests/ClientTLSFailureTests.swift

+ 14 - 2
Tests/GRPCTests/ClientTLSFailureTests.swift

@@ -19,19 +19,31 @@ import EchoModel
 import GRPCSampleData
 import Logging
 import NIO
+import NIOConcurrencyHelpers
 import NIOSSL
 import XCTest
 
 class ErrorRecordingDelegate: ClientErrorDelegate {
-  var errors: [Error] = []
+  private let lock: Lock
+  private var _errors: [Error] = []
+
+  internal var errors: [Error] {
+    return self.lock.withLock {
+      return self._errors
+    }
+  }
+
   var expectation: XCTestExpectation
 
   init(expectation: XCTestExpectation) {
     self.expectation = expectation
+    self.lock = Lock()
   }
 
   func didCatchError(_ error: Error, logger: Logger, file: StaticString, line: Int) {
-    self.errors.append(error)
+    self.lock.withLockVoid {
+      self._errors.append(error)
+    }
     self.expectation.fulfill()
   }
 }