GRPCStatusTests.swift 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. @testable import GRPC
  17. import XCTest
  18. class GRPCStatusTests: GRPCTestCase {
  19. func testStatusDescriptionWithoutMessage() {
  20. XCTAssertEqual(
  21. "ok (0)",
  22. String(describing: GRPCStatus(code: .ok, message: nil))
  23. )
  24. XCTAssertEqual(
  25. "aborted (10)",
  26. String(describing: GRPCStatus(code: .aborted, message: nil))
  27. )
  28. XCTAssertEqual(
  29. "internal error (13)",
  30. String(describing: GRPCStatus(code: .internalError, message: nil))
  31. )
  32. }
  33. func testStatusDescriptionWithMessage() {
  34. XCTAssertEqual(
  35. "ok (0): OK",
  36. String(describing: GRPCStatus(code: .ok, message: "OK"))
  37. )
  38. XCTAssertEqual(
  39. "resource exhausted (8): a resource was exhausted",
  40. String(describing: GRPCStatus(code: .resourceExhausted, message: "a resource was exhausted"))
  41. )
  42. XCTAssertEqual(
  43. "failed precondition (9): invalid state",
  44. String(describing: GRPCStatus(code: .failedPrecondition, message: "invalid state"))
  45. )
  46. }
  47. func testCoWSemanticsModifyingMessage() {
  48. let nilStorageID = GRPCStatus.ok.testingOnly_storageObjectIdentifier
  49. var status = GRPCStatus(code: .resourceExhausted)
  50. // No message/cause, so uses the nil backing storage.
  51. XCTAssertEqual(status.testingOnly_storageObjectIdentifier, nilStorageID)
  52. status.message = "no longer using the nil backing storage"
  53. let storageID = status.testingOnly_storageObjectIdentifier
  54. XCTAssertNotEqual(storageID, nilStorageID)
  55. XCTAssertEqual(status.message, "no longer using the nil backing storage")
  56. // The storage of status should be uniquely ref'd, so setting message to nil should not change
  57. // the backing storage (even if the nil storage could now be used).
  58. status.message = nil
  59. XCTAssertEqual(status.testingOnly_storageObjectIdentifier, storageID)
  60. XCTAssertNil(status.message)
  61. }
  62. func testCoWSemanticsModifyingCause() {
  63. let nilStorageID = GRPCStatus.ok.testingOnly_storageObjectIdentifier
  64. var status = GRPCStatus(code: .cancelled)
  65. // No message/cause, so uses the nil backing storage.
  66. XCTAssertEqual(status.testingOnly_storageObjectIdentifier, nilStorageID)
  67. status.cause = ConnectionPoolError.tooManyWaiters(connectionError: nil)
  68. let storageID = status.testingOnly_storageObjectIdentifier
  69. XCTAssertNotEqual(storageID, nilStorageID)
  70. XCTAssert(status.cause is ConnectionPoolError)
  71. // The storage of status should be uniquely ref'd, so setting cause to nil should not change
  72. // the backing storage (even if the nil storage could now be used).
  73. status.cause = nil
  74. XCTAssertEqual(status.testingOnly_storageObjectIdentifier, storageID)
  75. XCTAssertNil(status.cause)
  76. }
  77. func testStatusesWithNoMessageOrCauseShareBackingStorage() {
  78. let validStatusCodes = (0 ... 16)
  79. let statuses: [GRPCStatus] = validStatusCodes.map { code in
  80. // 0...16 are all valid, '!' is fine.
  81. let code = GRPCStatus.Code(rawValue: code)!
  82. return GRPCStatus(code: code)
  83. }
  84. let storageIDs = Set(statuses.map { $0.testingOnly_storageObjectIdentifier })
  85. XCTAssertEqual(storageIDs.count, 1)
  86. }
  87. }