RxLibraryUnitTests.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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 =
  125. [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  126. handler.block(value, errorOrNil);
  127. [expectation fulfill];
  128. }];
  129. id anyValue = @7;
  130. // If:
  131. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  132. [pipe startWithWriteable:writeable];
  133. [pipe writeValue:anyValue];
  134. [pipe writesFinishedWithError:nil];
  135. // Then:
  136. [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
  137. XCTAssertEqual(handler.timesCalled, 1);
  138. XCTAssertEqualObjects(handler.value, anyValue);
  139. XCTAssertEqualObjects(handler.errorOrNil, nil);
  140. }
  141. - (void)testBufferedPipePropagatesError {
  142. __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
  143. // Given:
  144. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  145. id<GRXWriteable> writeable =
  146. [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  147. handler.block(value, errorOrNil);
  148. [expectation fulfill];
  149. }];
  150. NSError *anyError = [NSError errorWithDomain:@"domain" code:7 userInfo:nil];
  151. // If:
  152. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  153. [pipe startWithWriteable:writeable];
  154. [pipe writesFinishedWithError:anyError];
  155. // Then:
  156. [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
  157. XCTAssertEqual(handler.timesCalled, 1);
  158. XCTAssertEqualObjects(handler.value, nil);
  159. XCTAssertEqualObjects(handler.errorOrNil, anyError);
  160. }
  161. - (void)testBufferedPipeFinishWriteWhilePaused {
  162. __weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
  163. // Given:
  164. CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
  165. id<GRXWriteable> writeable =
  166. [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
  167. handler.block(value, errorOrNil);
  168. [expectation fulfill];
  169. }];
  170. id anyValue = @7;
  171. // If:
  172. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  173. // Write something, then finish
  174. [pipe writeValue:anyValue];
  175. [pipe writesFinishedWithError:nil];
  176. // then start the writeable
  177. [pipe startWithWriteable:writeable];
  178. // Then:
  179. [self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
  180. XCTAssertEqual(handler.timesCalled, 1);
  181. XCTAssertEqualObjects(handler.value, anyValue);
  182. XCTAssertEqualObjects(handler.errorOrNil, nil);
  183. }
  184. #define WRITE_ROUNDS (1000)
  185. - (void)testBufferedPipeResumeWhenDealloc {
  186. id anyValue = @7;
  187. id<GRXWriteable> writeable =
  188. [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil){
  189. }];
  190. // Release after alloc;
  191. GRXBufferedPipe *pipe = [GRXBufferedPipe pipe];
  192. pipe = nil;
  193. // Release after write but before start
  194. pipe = [GRXBufferedPipe pipe];
  195. for (int i = 0; i < WRITE_ROUNDS; i++) {
  196. [pipe writeValue:anyValue];
  197. }
  198. pipe = nil;
  199. // Release after start but not write
  200. pipe = [GRXBufferedPipe pipe];
  201. [pipe startWithWriteable:writeable];
  202. pipe = nil;
  203. // Release after start and write
  204. pipe = [GRXBufferedPipe pipe];
  205. for (int i = 0; i < WRITE_ROUNDS; i++) {
  206. [pipe writeValue:anyValue];
  207. }
  208. [pipe startWithWriteable:writeable];
  209. pipe = nil;
  210. // Release after start, write and pause
  211. pipe = [GRXBufferedPipe pipe];
  212. [pipe startWithWriteable:writeable];
  213. for (int i = 0; i < WRITE_ROUNDS; i++) {
  214. [pipe writeValue:anyValue];
  215. }
  216. pipe.state = GRXWriterStatePaused;
  217. for (int i = 0; i < WRITE_ROUNDS; i++) {
  218. [pipe writeValue:anyValue];
  219. }
  220. pipe = nil;
  221. // Release after start, write, pause and finish
  222. pipe = [GRXBufferedPipe pipe];
  223. [pipe startWithWriteable:writeable];
  224. for (int i = 0; i < WRITE_ROUNDS; i++) {
  225. [pipe writeValue:anyValue];
  226. }
  227. pipe.state = GRXWriterStatePaused;
  228. for (int i = 0; i < WRITE_ROUNDS; i++) {
  229. [pipe writeValue:anyValue];
  230. }
  231. [pipe finishWithError:nil];
  232. pipe = nil;
  233. // Release after start, write, pause, finish and resume
  234. pipe = [GRXBufferedPipe pipe];
  235. [pipe startWithWriteable:writeable];
  236. for (int i = 0; i < WRITE_ROUNDS; i++) {
  237. [pipe writeValue:anyValue];
  238. }
  239. pipe.state = GRXWriterStatePaused;
  240. for (int i = 0; i < WRITE_ROUNDS; i++) {
  241. [pipe writeValue:anyValue];
  242. }
  243. [pipe finishWithError:nil];
  244. pipe.state = GRXWriterStateStarted;
  245. pipe = nil;
  246. }
  247. @end