GRPCTestCase.swift 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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 XCTest
  17. import Logging
  18. /// A test case which initializes the logging system once.
  19. ///
  20. /// This should be used instead of `XCTestCase`.
  21. class GRPCTestCase: XCTestCase {
  22. // `LoggingSystem.bootstrap` must be called once per process. This is the suggested approach to
  23. // workaround this for XCTestCase.
  24. //
  25. // See: https://github.com/apple/swift-log/issues/77
  26. private static let isLoggingConfigured: Bool = {
  27. LoggingSystem.bootstrap { _ in BlackHole() }
  28. return true
  29. }()
  30. override class func setUp() {
  31. super.setUp()
  32. XCTAssertTrue(GRPCTestCase.isLoggingConfigured)
  33. }
  34. }
  35. /// A `LogHandler` which does nothing with log messages.
  36. struct BlackHole: LogHandler {
  37. func log(level: Logger.Level, message: Logger.Message, metadata: Logger.Metadata?, file: String, function: String, line: UInt) {
  38. ()
  39. }
  40. subscript(metadataKey key: String) -> Logger.Metadata.Value? {
  41. get {
  42. return metadata[key]
  43. }
  44. set(newValue) {
  45. self.metadata[key] = newValue
  46. }
  47. }
  48. var metadata: Logger.Metadata = [:]
  49. var logLevel: Logger.Level = .critical
  50. }