LazyEventLoopPromiseTests.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * Copyright 2020, 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 NIO
  18. import XCTest
  19. class LazyEventLoopPromiseTests: GRPCTestCase {
  20. func testGetFutureAfterSuccess() {
  21. let loop = EmbeddedEventLoop()
  22. var promise = loop.makeLazyPromise(of: String.self)
  23. promise.succeed("foo")
  24. XCTAssertEqual(try promise.getFutureResult().wait(), "foo")
  25. }
  26. func testGetFutureBeforeSuccess() {
  27. let loop = EmbeddedEventLoop()
  28. var promise = loop.makeLazyPromise(of: String.self)
  29. let future = promise.getFutureResult()
  30. promise.succeed("foo")
  31. XCTAssertEqual(try future.wait(), "foo")
  32. }
  33. func testGetFutureAfterError() {
  34. let loop = EmbeddedEventLoop()
  35. var promise = loop.makeLazyPromise(of: String.self)
  36. promise.fail(GRPCStatus.processingError)
  37. XCTAssertThrowsError(try promise.getFutureResult().wait()) { error in
  38. XCTAssertTrue(error is GRPCStatus)
  39. }
  40. }
  41. func testGetFutureBeforeError() {
  42. let loop = EmbeddedEventLoop()
  43. var promise = loop.makeLazyPromise(of: String.self)
  44. let future = promise.getFutureResult()
  45. promise.fail(GRPCStatus.processingError)
  46. XCTAssertThrowsError(try future.wait()) { error in
  47. XCTAssertTrue(error is GRPCStatus)
  48. }
  49. }
  50. func testGetFutureMultipleTimes() {
  51. let loop = EmbeddedEventLoop()
  52. var promise = loop.makeLazyPromise(of: String.self)
  53. let f1 = promise.getFutureResult()
  54. let f2 = promise.getFutureResult()
  55. promise.succeed("foo")
  56. XCTAssertEqual(try f1.wait(), try f2.wait())
  57. }
  58. func testMultipleResolutionsIgnored() {
  59. let loop = EmbeddedEventLoop()
  60. var promise = loop.makeLazyPromise(of: String.self)
  61. promise.succeed("foo")
  62. XCTAssertEqual(try promise.getFutureResult().wait(), "foo")
  63. promise.succeed("bar")
  64. XCTAssertEqual(try promise.getFutureResult().wait(), "foo")
  65. promise.fail(GRPCStatus.processingError)
  66. XCTAssertEqual(try promise.getFutureResult().wait(), "foo")
  67. }
  68. func testNoFuture() {
  69. let loop = EmbeddedEventLoop()
  70. var promise = loop.makeLazyPromise(of: String.self)
  71. promise.succeed("foo")
  72. }
  73. }