RuntimeErrorTests.swift 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. * Copyright 2023, 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 GRPCCore
  17. import XCTest
  18. @available(macOS 10.15, iOS 13, tvOS 13, watchOS 6, *)
  19. final class RuntimeErrorTests: XCTestCase {
  20. func testCopyOnWrite() {
  21. // RuntimeError has a heap based storage, so check CoW semantics are correctly implemented.
  22. let error1 = RuntimeError(code: .transportError, message: "Failed to start transport")
  23. var error2 = error1
  24. error2.code = .serverIsAlreadyRunning
  25. XCTAssertEqual(error1.code, .transportError)
  26. XCTAssertEqual(error2.code, .serverIsAlreadyRunning)
  27. var error3 = error1
  28. error3.message = "foo"
  29. XCTAssertEqual(error1.message, "Failed to start transport")
  30. XCTAssertEqual(error3.message, "foo")
  31. var error4 = error1
  32. error4.cause = CancellationError()
  33. XCTAssertNil(error1.cause)
  34. XCTAssert(error4.cause is CancellationError)
  35. }
  36. func testCustomStringConvertible() {
  37. let error1 = RuntimeError(code: .transportError, message: "Failed to start transport")
  38. XCTAssertDescription(error1, #"transportError: "Failed to start transport""#)
  39. let error2 = RuntimeError(
  40. code: .transportError,
  41. message: "Failed to start transport",
  42. cause: CancellationError()
  43. )
  44. XCTAssertDescription(
  45. error2,
  46. #"transportError: "Failed to start transport" (cause: "CancellationError()")"#
  47. )
  48. }
  49. }