GRPCTestCase.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright 2019, gRPC Authors All rights reserved.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. import GRPC
  17. import Logging
  18. import XCTest
  19. /// This should be used instead of `XCTestCase`.
  20. class GRPCTestCase: XCTestCase {
  21. /// Unless `GRPC_ALWAYS_LOG` is set, logs will only be printed if a test case fails.
  22. private static let alwaysLog = Bool(
  23. fromTruthLike: ProcessInfo.processInfo.environment["GRPC_ALWAYS_LOG"],
  24. defaultingTo: false
  25. )
  26. private static let runTimeSensitiveTests = Bool(
  27. fromTruthLike: ProcessInfo.processInfo.environment["ENABLE_TIMING_TESTS"],
  28. defaultingTo: true
  29. )
  30. override func setUp() {
  31. super.setUp()
  32. self.logFactory = CapturingLogHandlerFactory(printWhenCaptured: GRPCTestCase.alwaysLog)
  33. }
  34. override func tearDown() {
  35. // Only print logs when there's a failure and we're *not* always logging (when we are always
  36. // logging, logs will be printed as they're caught).
  37. if !GRPCTestCase.alwaysLog, self.testRun.map({ $0.totalFailureCount > 0 }) ?? false {
  38. let logs = self.capturedLogs()
  39. self.printCapturedLogs(logs)
  40. }
  41. super.tearDown()
  42. }
  43. func runTimeSensitiveTests() -> Bool {
  44. let shouldRun = GRPCTestCase.runTimeSensitiveTests
  45. if !shouldRun {
  46. print("Skipping '\(self.name)' as ENABLE_TIMING_TESTS=false")
  47. }
  48. return shouldRun
  49. }
  50. private(set) var logFactory: CapturingLogHandlerFactory!
  51. /// A general-use logger.
  52. var logger: Logger {
  53. return Logger(label: "grpc", factory: self.logFactory.make)
  54. }
  55. /// A logger for clients to use.
  56. var clientLogger: Logger {
  57. // Label is ignored; we already have a handler.
  58. return Logger(label: "client", factory: self.logFactory.make)
  59. }
  60. /// A logger for servers to use.
  61. var serverLogger: Logger {
  62. // Label is ignored; we already have a handler.
  63. return Logger(label: "server", factory: self.logFactory.make)
  64. }
  65. /// The default client call options using `self.clientLogger`.
  66. var callOptionsWithLogger: CallOptions {
  67. return CallOptions(logger: self.clientLogger)
  68. }
  69. /// Returns all captured logs sorted by date.
  70. private func capturedLogs() -> [CapturedLog] {
  71. assert(self.logFactory != nil, "Missing call to super.setUp()")
  72. var logs = self.logFactory.clearCapturedLogs()
  73. logs.sort(by: { $0.date < $1.date })
  74. return logs
  75. }
  76. /// Prints all captured logs.
  77. private func printCapturedLogs(_ logs: [CapturedLog]) {
  78. print("Test Case '\(self.name)' logs started")
  79. // The logs are already sorted by date.
  80. let formatter = CapturedLogFormatter()
  81. for log in logs {
  82. print(formatter.string(for: log))
  83. }
  84. print("Test Case '\(self.name)' logs finished")
  85. }
  86. }
  87. extension Bool {
  88. fileprivate init(fromTruthLike value: String?, defaultingTo defaultValue: Bool) {
  89. switch value?.lowercased() {
  90. case "0", "false", "no":
  91. self = false
  92. case "1", "true", "yes":
  93. self = true
  94. default:
  95. self = defaultValue
  96. }
  97. }
  98. }