TestSendable.swift 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // This source file is part of the Swift Logging API open source project
  4. //
  5. // Copyright (c) 2022 Apple Inc. and the Swift Logging API project authors
  6. // Licensed under Apache License v2.0
  7. //
  8. // See LICENSE.txt for license information
  9. // See CONTRIBUTORS.txt for the list of Swift Logging API project authors
  10. //
  11. // SPDX-License-Identifier: Apache-2.0
  12. //
  13. //===----------------------------------------------------------------------===//
  14. import Dispatch
  15. @testable import Logging
  16. import XCTest
  17. class SendableTest: XCTestCase {
  18. #if compiler(>=5.6)
  19. func testSendableLogger() async {
  20. let testLogging = TestLogging()
  21. LoggingSystem.bootstrapInternal(testLogging.make)
  22. let logger = Logger(label: "test")
  23. let message1 = Logger.Message(stringLiteral: "critical 1")
  24. let message2 = Logger.Message(stringLiteral: "critical 2")
  25. let metadata: Logger.Metadata = ["key": "value"]
  26. let task = Task.detached {
  27. logger.info("info")
  28. logger.critical(message1)
  29. logger.critical(message2)
  30. logger.warning(.init(stringLiteral: "warning"), metadata: metadata)
  31. }
  32. await task.value
  33. testLogging.history.assertExist(level: .info, message: "info", metadata: [:])
  34. testLogging.history.assertExist(level: .critical, message: "critical 1", metadata: [:])
  35. testLogging.history.assertExist(level: .critical, message: "critical 2", metadata: [:])
  36. testLogging.history.assertExist(level: .warning, message: "warning", metadata: metadata)
  37. }
  38. #endif
  39. }