Browse Source

Add toggle for time-sensitive tests. (#677)

Motivation:

Sometimes, like when running under TSan, it's useful to disable long
running tests, or those which are sensitive to time.

Modifications:

Add an option to disable time sensitive tests via an envrionment
variable.

Result:

Certain tests are skipped if `ENABLE_TIMING_TESTS` is set to false.
George Barnett 6 years ago
parent
commit
7f101723d1

+ 6 - 0
Tests/GRPCTests/FunctionalTests.swift

@@ -57,6 +57,8 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
   }
 
   func testUnaryLotsOfRequests() throws {
+    guard self.runTimeSensitiveTests() else { return }
+
     // Sending that many requests at once can sometimes trip things up, it seems.
     let clockStart = clock()
     let numberOfRequests = 2_000
@@ -130,6 +132,7 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
   }
 
   func testClientStreamingLotsOfMessages() throws {
+    guard self.runTimeSensitiveTests() else { return }
     self.defaultTestTimeout = 15.0
     XCTAssertNoThrow(try doTestClientStreaming(messages: lotsOfStrings))
   }
@@ -157,6 +160,7 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
   }
 
   func testServerStreamingLotsOfMessages() {
+    guard self.runTimeSensitiveTests() else { return }
     self.defaultTestTimeout = 15.0
     XCTAssertNoThrow(try doTestServerStreaming(messages: lotsOfStrings))
   }
@@ -198,11 +202,13 @@ class FunctionalTestsInsecureTransport: EchoTestCaseBase {
   }
 
   func testBidirectionalStreamingLotsOfMessagesBatched() throws {
+    guard self.runTimeSensitiveTests() else { return }
     self.defaultTestTimeout = 15.0
     XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: lotsOfStrings))
   }
 
   func testBidirectionalStreamingLotsOfMessagesPingPong() throws {
+    guard self.runTimeSensitiveTests() else { return }
     self.defaultTestTimeout = 15.0
     XCTAssertNoThrow(try doTestBidirectionalStreaming(messages: lotsOfStrings, waitForEachResponse: true))
   }

+ 31 - 1
Tests/GRPCTests/GRPCTestCase.swift

@@ -24,7 +24,16 @@ class GRPCTestCase: XCTestCase {
   // locally; conditionally enable it based on the environment.
   //
   // https://docs.travis-ci.com/user/environment-variables/#default-environment-variables
-  private static var isLoggingEnabled = ProcessInfo.processInfo.environment["CI"] != "true"
+  private static let isCI = Bool(
+      fromTruthLike: ProcessInfo.processInfo.environment["CI"],
+      defaultingTo: false
+  )
+  private static let isLoggingEnabled = !isCI
+
+  private static let runTimeSensitiveTests = Bool(
+      fromTruthLike: ProcessInfo.processInfo.environment["ENABLE_TIMING_TESTS"],
+      defaultingTo: true
+  )
 
   // `LoggingSystem.bootstrap` must be called once per process. This is the suggested approach to
   // workaround this for XCTestCase.
@@ -46,6 +55,14 @@ class GRPCTestCase: XCTestCase {
     super.setUp()
     XCTAssertTrue(GRPCTestCase.isLoggingConfigured)
   }
+
+  func runTimeSensitiveTests() -> Bool {
+    let shouldRun = GRPCTestCase.runTimeSensitiveTests
+    if !shouldRun {
+      print("Skipping '\(self.name)' as ENABLE_TIMING_TESTS=false")
+    }
+    return shouldRun
+  }
 }
 
 /// A `LogHandler` which does nothing with log messages.
@@ -66,3 +83,16 @@ struct BlackHole: LogHandler {
   var metadata: Logger.Metadata = [:]
   var logLevel: Logger.Level = .critical
 }
+
+fileprivate extension Bool {
+  init(fromTruthLike value: String?, defaultingTo defaultValue: Bool) {
+    switch value?.lowercased() {
+    case "0", "false", "no":
+      self = false
+    case "1", "true", "yes":
+      self = true
+    default:
+      self = defaultValue
+    }
+  }
+}

+ 1 - 0
Tests/GRPCTests/ServerWebTests.swift

@@ -109,6 +109,7 @@ extension ServerWebTests {
   }
 
   func testUnaryLotsOfRequests() {
+    guard self.runTimeSensitiveTests() else { return }
     // Sending that many requests at once can sometimes trip things up, it seems.
     let clockStart = clock()
     let numberOfRequests = 2_000