Asynchronously wait for tests

pull/11070/head
Muxi Yan 8 years ago
parent 49b7f8349b
commit a8d40b5672
  1. 22
      src/objective-c/tests/RxLibraryUnitTests.m

@ -38,6 +38,8 @@
#import <RxLibrary/GRXWriteable.h> #import <RxLibrary/GRXWriteable.h>
#import <RxLibrary/GRXWriter.h> #import <RxLibrary/GRXWriter.h>
#define TEST_TIMEOUT 1
// A mock of a GRXSingleValueHandler block that can be queried for how many times it was called and // A mock of a GRXSingleValueHandler block that can be queried for how many times it was called and
// what were the last values passed to it. // what were the last values passed to it.
// //
@ -155,9 +157,14 @@
#pragma mark BufferedPipe #pragma mark BufferedPipe
- (void)testBufferedPipePropagatesValue { - (void)testBufferedPipePropagatesValue {
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
// Given: // Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id anyValue = @7; id anyValue = @7;
// If: // If:
@ -166,13 +173,12 @@
[pipe writeValue:anyValue]; [pipe writeValue:anyValue];
[pipe writesFinishedWithError:nil]; [pipe writesFinishedWithError:nil];
// Wait buffered pipe to be flushed.
sleep(1);
// Then: // Then:
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
XCTAssertEqual(handler.timesCalled, 1); XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, anyValue); XCTAssertEqualObjects(handler.value, anyValue);
XCTAssertEqualObjects(handler.errorOrNil, nil); XCTAssertEqualObjects(handler.errorOrNil, nil);
} }
- (void)testBufferedPipePropagatesError { - (void)testBufferedPipePropagatesError {
@ -193,9 +199,13 @@
} }
- (void)testBufferedPipeFinishWriteWhilePaused { - (void)testBufferedPipeFinishWriteWhilePaused {
__weak XCTestExpectation *expectation = [self expectationWithDescription:@"Response received"];
// Given: // Given:
CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler]; CapturingSingleValueHandler *handler = [CapturingSingleValueHandler handler];
id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:handler.block]; id<GRXWriteable> writeable = [GRXWriteable writeableWithSingleHandler:^(id value, NSError *errorOrNil) {
handler.block(value, errorOrNil);
[expectation fulfill];
}];
id anyValue = @7; id anyValue = @7;
// If: // If:
@ -205,9 +215,9 @@
[pipe writesFinishedWithError:nil]; [pipe writesFinishedWithError:nil];
// then start the writeable // then start the writeable
[pipe startWithWriteable:writeable]; [pipe startWithWriteable:writeable];
sleep(1);
// Then: // Then:
[self waitForExpectationsWithTimeout:TEST_TIMEOUT handler:nil];
XCTAssertEqual(handler.timesCalled, 1); XCTAssertEqual(handler.timesCalled, 1);
XCTAssertEqualObjects(handler.value, anyValue); XCTAssertEqualObjects(handler.value, anyValue);
XCTAssertEqualObjects(handler.errorOrNil, nil); XCTAssertEqualObjects(handler.errorOrNil, nil);

Loading…
Cancel
Save