LazyEventLoopPromiseTests.swift 2.6 KB

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