ErrorRecordingDelegate.swift 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * Copyright 2021, 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 GRPC
  17. import Logging
  18. import NIOConcurrencyHelpers
  19. import XCTest
  20. #if compiler(>=5.6)
  21. // Unchecked as all mutable state is accessed and modified behind a lock.
  22. extension ErrorRecordingDelegate: @unchecked Sendable {}
  23. #endif // compiler(>=5.6)
  24. final class ErrorRecordingDelegate: ClientErrorDelegate {
  25. private let lock: NIOLock
  26. private var _errors: [Error] = []
  27. internal var errors: [Error] {
  28. return self.lock.withLock {
  29. return self._errors
  30. }
  31. }
  32. var expectation: XCTestExpectation
  33. init(expectation: XCTestExpectation) {
  34. self.expectation = expectation
  35. self.lock = NIOLock()
  36. }
  37. func didCatchError(_ error: Error, logger: Logger, file: StaticString, line: Int) {
  38. self.lock.withLock {
  39. self._errors.append(error)
  40. }
  41. self.expectation.fulfill()
  42. }
  43. }
  44. class ServerErrorRecordingDelegate: ServerErrorDelegate {
  45. var errors: [Error] = []
  46. var expectation: XCTestExpectation
  47. init(expectation: XCTestExpectation) {
  48. self.expectation = expectation
  49. }
  50. func observeLibraryError(_ error: Error) {
  51. self.errors.append(error)
  52. self.expectation.fulfill()
  53. }
  54. }