RxLibraryUnitTests.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #import <UIKit/UIKit.h>
  19. #import <XCTest/XCTest.h>
  20. #import <RxLibrary/GRXBufferedPipe.h>
  21. #import <RxLibrary/GRXWriteable.h>
  22. #import <RxLibrary/GRXWriter.h>
  23. #define TEST_TIMEOUT 1
  24. // A mock of a GRXSingleValueHandler block that can be queried for how many times it was called and
  25. // what were the last values passed to it.
  26. //
  27. // TODO(jcanizales): Move this to a test util library, and add tests for it.
  28. @interface CapturingSingleValueHandler : NSObject
  29. @property (nonatomic, readonly) void (^block)(id value, NSError *errorOrNil);
  30. @property (nonatomic, readonly) NSUInteger timesCalled;
  31. @property (nonatomic, readonly) id value;
  32. @property (nonatomic, readonly) NSError *errorOrNil;
  33. + (instancetype)handler;
  34. @end
  35. @implementation CapturingSingleValueHandler
  36. + (instancetype)handler {
  37. return [[self alloc] init];
  38. }
  39. - (GRXSingleHandler)block {
  40. return ^(id value, NSError *errorOrNil) {
  41. ++_timesCalled;
  42. _value = value;
  43. _errorOrNil = errorOrNil;
  44. };
  45. }
  46. @end
  47. // TODO(jcanizales): Split into one file per tested class.
  48. @interface RxLibraryUnitTests : XCTestCase
  49. @end
  50. @implementation RxLibraryUnitTests
  51. + (void)setUp {
  52. NSLog(@"GRPCClientTests Started");
  53. }
  54. #pragma mark Writeable
  55. - (void)testWriteableSingleHandlerIsCalledForValue {
  56. // Given:
  57. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  58. id anyValue = @7;
  59. // If:
  60. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
  61. [writeable writeValue:anyValue];
  62. [writeable writesFinishedWithError:nil];
  63. // Then:
  64. XCTAssertEqual(handler.timesCalled, 1);
  65. XCTAssertEqualObjects(handler.value, anyValue);
  66. XCTAssertEqualObjects(handler.errorOrNil, nil);
  67. }
  68. - (void)testWriteableSingleHandlerIsCalledForError {
  69. // Given:
  70. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  71. NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
  72. // If:
  73. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
  74. [writeable writesFinishedWithError:anyError];
  75. // Then:
  76. XCTAssertEqual(handler.timesCalled, 1);
  77. XCTAssertEqualObjects(handler.value, nil);
  78. XCTAssertEqualObjects(handler.errorOrNil, anyError);
  79. }
  80. - (void)testWriteableSingleHandlerIsCalledOnlyOnce_ValueThenError {
  81. // Given:
  82. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  83. id anyValue = @7;
  84. NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
  85. // If:
  86. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
  87. [writeable writeValue:anyValue];
  88. [writeable writesFinishedWithError:anyError];
  89. // Then:
  90. XCTAssertEqual(handler.timesCalled, 1);
  91. XCTAssertEqualObjects(handler.value, anyValue);
  92. XCTAssertEqualObjects(handler.errorOrNil, nil);
  93. }
  94. - (void)testWriteableSingleHandlerIsCalledOnlyOnce_ValueThenValue {
  95. // Given:
  96. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  97. id anyValue = @7;
  98. // If:
  99. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
  100. [writeable writeValue:anyValue];
  101. [writeable writeValue:anyValue];
  102. [writeable writesFinishedWithError:nil];
  103. // Then:
  104. XCTAssertEqual(handler.timesCalled, 1);
  105. XCTAssertEqualObjects(handler.value, anyValue);
  106. XCTAssertEqualObjects(handler.errorOrNil, nil);
  107. }
  108. - (void)testWriteableSingleHandlerFailsOnEmptyWriter {
  109. // Given:
  110. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  111. // If:
  112. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block];
  113. [writeable writesFinishedWithError:nil];
  114. // Then:
  115. XCTAssertEqual(handler.timesCalled, 1);
  116. XCTAssertEqualObjects(handler.value, nil);
  117. XCTAssertNotNil(handler.errorOrNil);
  118. }
  119. #pragma mark BufferedPipe
  120. - (void)testBufferedPipePropagatesValue {
  121. __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
  122. // Given:
  123. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  124. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  125. handler.block(value, errorOrNil);
  126. [expectation fulfill];
  127. }];
  128. id anyValue = @7;
  129. // If:
  130. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  131. [pipe startWithWriteable:writeable];
  132. [pipe writeValue:anyValue];
  133. [pipe writesFinishedWithError:nil];
  134. // Then:
  135. [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
  136. XCTAssertEqual(handler.timesCalled, 1);
  137. XCTAssertEqualObjects(handler.value, anyValue);
  138. XCTAssertEqualObjects(handler.errorOrNil, nil);
  139. }
  140. - (void)testBufferedPipePropagatesError {
  141. __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
  142. // Given:
  143. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  144. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  145. handler.block(value, errorOrNil);
  146. [expectation fulfill];
  147. }];
  148. NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
  149. // If:
  150. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  151. [pipe startWithWriteable:writeable];
  152. [pipe writesFinishedWithError:anyError];
  153. // Then:
  154. [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
  155. XCTAssertEqual(handler.timesCalled, 1);
  156. XCTAssertEqualObjects(handler.value, nil);
  157. XCTAssertEqualObjects(handler.errorOrNil, anyError);
  158. }
  159. - (void)testBufferedPipeFinishWriteWhilePaused {
  160. __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
  161. // Given:
  162. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  163. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  164. handler.block(value, errorOrNil);
  165. [expectation fulfill];
  166. }];
  167. id anyValue = @7;
  168. // If:
  169. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  170. // Write something, then finish
  171. [pipe writeValue:anyValue];
  172. [pipe writesFinishedWithError:nil];
  173. // then start the writeable
  174. [pipe startWithWriteable:writeable];
  175. // Then:
  176. [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
  177. XCTAssertEqual(handler.timesCalled, 1);
  178. XCTAssertEqualObjects(handler.value, anyValue);
  179. XCTAssertEqualObjects(handler.errorOrNil, nil);
  180. }
  181. #define WRITE_ROUNDS (1000)
  182. - (void)testBufferedPipeResumeWhenDealloc {
  183. id anyValue = @7;
  184. id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  185. }];
  186. // Release after alloc;
  187. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  188. pipe = nil;
  189. // Release after write but before start
  190. pipe = [GRXBufferedPipe pipe];
  191. for (int i = 0; i < WRITE_ROUNDS; i++) {
  192. [pipe writeValue:anyValue];
  193. }
  194. pipe = nil;
  195. // Release after start but not write
  196. pipe = [GRXBufferedPipe pipe];
  197. [pipe startWithWriteable:writeable];
  198. pipe = nil;
  199. // Release after start and write
  200. pipe = [GRXBufferedPipe pipe];
  201. for (int i = 0; i < WRITE_ROUNDS; i++) {
  202. [pipe writeValue:anyValue];
  203. }
  204. [pipe startWithWriteable:writeable];
  205. pipe = nil;
  206. // Release after start, write and pause
  207. pipe = [GRXBufferedPipe pipe];
  208. [pipe startWithWriteable:writeable];
  209. for (int i = 0; i < WRITE_ROUNDS; i++) {
  210. [pipe writeValue:anyValue];
  211. }
  212. pipe.state = GRXWriterStatePaused;
  213. for (int i = 0; i < WRITE_ROUNDS; i++) {
  214. [pipe writeValue:anyValue];
  215. }
  216. pipe = nil;
  217. // Release after start, write, pause and finish
  218. pipe = [GRXBufferedPipe pipe];
  219. [pipe startWithWriteable:writeable];
  220. for (int i = 0; i < WRITE_ROUNDS; i++) {
  221. [pipe writeValue:anyValue];
  222. }
  223. pipe.state = GRXWriterStatePaused;
  224. for (int i = 0; i < WRITE_ROUNDS; i++) {
  225. [pipe writeValue:anyValue];
  226. }
  227. [pipe finishWithError:nil];
  228. pipe = nil;
  229. // Release after start, write, pause, finish and resume
  230. pipe = [GRXBufferedPipe pipe];
  231. [pipe startWithWriteable:writeable];
  232. for (int i = 0; i < WRITE_ROUNDS; i++) {
  233. [pipe writeValue:anyValue];
  234. }
  235. pipe.state = GRXWriterStatePaused;
  236. for (int i = 0; i < WRITE_ROUNDS; i++) {
  237. [pipe writeValue:anyValue];
  238. }
  239. [pipe finishWithError:nil];
  240. pipe.state = GRXWriterStateStarted;
  241. pipe = nil;
  242. }
  243. @end