Connection+Equatable.swift 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright 2024, 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 GRPCHTTP2Core
  18. // Equatable conformance for these types is 'best effort', this is sufficient for testing but not
  19. // for general use. As such the conformance is added in the test module and must be declared
  20. // as a `@retroactive` conformance.
  21. #if compiler(>=6.0)
  22. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  23. extension Connection.Event: @retroactive Equatable {}
  24. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  25. extension Connection.CloseReason: @retroactive Equatable {}
  26. extension ClientConnectionEvent: @retroactive Equatable {}
  27. extension ClientConnectionEvent.CloseReason: @retroactive Equatable {}
  28. #else
  29. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  30. extension Connection.Event: Equatable {}
  31. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  32. extension Connection.CloseReason: Equatable {}
  33. extension ClientConnectionEvent: Equatable {}
  34. extension ClientConnectionEvent.CloseReason: Equatable {}
  35. #endif
  36. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  37. extension Connection.Event {
  38. package static func == (lhs: Connection.Event, rhs: Connection.Event) -> Bool {
  39. switch (lhs, rhs) {
  40. case (.connectSucceeded, .connectSucceeded),
  41. (.connectFailed, .connectFailed):
  42. return true
  43. case (.goingAway(let lhsCode, let lhsReason), .goingAway(let rhsCode, let rhsReason)):
  44. return lhsCode == rhsCode && lhsReason == rhsReason
  45. case (.closed(let lhsReason), .closed(let rhsReason)):
  46. return lhsReason == rhsReason
  47. default:
  48. return false
  49. }
  50. }
  51. }
  52. @available(macOS 14.0, iOS 17.0, watchOS 10.0, tvOS 17.0, *)
  53. extension Connection.CloseReason {
  54. package static func == (lhs: Connection.CloseReason, rhs: Connection.CloseReason) -> Bool {
  55. switch (lhs, rhs) {
  56. case (.idleTimeout, .idleTimeout),
  57. (.keepaliveTimeout, .keepaliveTimeout),
  58. (.initiatedLocally, .initiatedLocally),
  59. (.remote, .remote):
  60. return true
  61. case (.error(let lhsError, let lhsStreams), .error(let rhsError, let rhsStreams)):
  62. if let lhs = lhsError as? RPCError, let rhs = rhsError as? RPCError {
  63. return lhs == rhs && lhsStreams == rhsStreams
  64. } else {
  65. return lhsStreams == rhsStreams
  66. }
  67. default:
  68. return false
  69. }
  70. }
  71. }
  72. extension ClientConnectionEvent {
  73. package static func == (lhs: ClientConnectionEvent, rhs: ClientConnectionEvent) -> Bool {
  74. switch (lhs, rhs) {
  75. case (.ready, .ready):
  76. return true
  77. case (.closing(let lhsReason), .closing(let rhsReason)):
  78. return lhsReason == rhsReason
  79. default:
  80. return false
  81. }
  82. }
  83. }
  84. extension ClientConnectionEvent.CloseReason {
  85. package static func == (lhs: Self, rhs: Self) -> Bool {
  86. switch (lhs, rhs) {
  87. case (.goAway(let lhsCode, let lhsMessage), .goAway(let rhsCode, let rhsMessage)):
  88. return lhsCode == rhsCode && lhsMessage == rhsMessage
  89. case (.unexpected(let lhsError, let lhsIsIdle), .unexpected(let rhsError, let rhsIsIdle)):
  90. if let lhs = lhsError as? RPCError, let rhs = rhsError as? RPCError {
  91. return lhs == rhs && lhsIsIdle == rhsIsIdle
  92. } else {
  93. return lhsIsIdle == rhsIsIdle
  94. }
  95. case (.keepaliveExpired, .keepaliveExpired),
  96. (.idle, .idle),
  97. (.initiatedLocally, .initiatedLocally):
  98. return true
  99. default:
  100. return false
  101. }
  102. }
  103. }