Commit 7b5cf27c by Dima Bart

Add tests for polling.

parent 7542270b
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
#import <XCTest/XCTest.h> #import <XCTest/XCTest.h>
#import <OHHTTPStubs/OHHTTPStubs.h> #import <OHHTTPStubs/OHHTTPStubs.h>
#import "BUYRequestOperation.h" #import "BUYRequestOperation.h"
#import "BUYClient.h"
@interface BUYRequestOperationTests : XCTestCase @interface BUYRequestOperationTests : XCTestCase
...@@ -69,7 +70,7 @@ ...@@ -69,7 +70,7 @@
}; };
NSData *payloadData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil]; NSData *payloadData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil];
OHHTTPStubsResponse *response = [OHHTTPStubsResponse responseWithData:payloadData statusCode:200 headers:nil]; OHHTTPStubsResponse *response = [OHHTTPStubsResponse responseWithData:payloadData statusCode:BUYStatusComplete headers:nil];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES; return YES;
...@@ -85,7 +86,7 @@ ...@@ -85,7 +86,7 @@
XCTAssertNil(error); XCTAssertNil(error);
XCTAssertEqualObjects(json, payload); XCTAssertEqualObjects(json, payload);
XCTAssertEqual(response.statusCode, 200); XCTAssertEqual(response.statusCode, BUYStatusComplete);
}]; }];
[self.queue addOperation:operation]; [self.queue addOperation:operation];
...@@ -102,7 +103,7 @@ ...@@ -102,7 +103,7 @@
}; };
NSData *payloadData = [NSJSONSerialization dataWithJSONObject:errorPayload options:0 error:nil]; NSData *payloadData = [NSJSONSerialization dataWithJSONObject:errorPayload options:0 error:nil];
OHHTTPStubsResponse *response = [OHHTTPStubsResponse responseWithData:payloadData statusCode:400 headers:nil]; OHHTTPStubsResponse *response = [OHHTTPStubsResponse responseWithData:payloadData statusCode:BUYStatusFailed headers:nil];
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
return YES; return YES;
...@@ -118,7 +119,7 @@ ...@@ -118,7 +119,7 @@
XCTAssertNotNil(error); XCTAssertNotNil(error);
XCTAssertEqualObjects(error.userInfo, errorPayload); XCTAssertEqualObjects(error.userInfo, errorPayload);
XCTAssertEqual(response.statusCode, 400); XCTAssertEqual(response.statusCode, BUYStatusFailed);
}]; }];
[self.queue addOperation:operation]; [self.queue addOperation:operation];
...@@ -190,6 +191,44 @@ ...@@ -190,6 +191,44 @@
XCTAssertEqualObjects(container, @"1134"); XCTAssertEqualObjects(container, @"1134");
} }
- (void)testPollingActivatedWithHandler
{
[self stubRequestsWithDelay:0.1 status:BUYStatusProcessing];
XCTestExpectation *completion = [self expectationWithDescription:@"Should complete after polling"];
BUYRequestOperation *operation = [self operationFulfillingExpectation:nil completion:^{
[completion fulfill];
}];
__block int pollCount = 0;
XCTestExpectation *expectation = [self expectationWithDescription:@"Should stop polling at 10 iterations"];
operation.pollingHandler = ^BOOL (NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
[self stubRequestsWithDelay:0.1 status:BUYStatusProcessing];
XCTAssertNotNil(json);
XCTAssertNotNil(response);
XCTAssertNil(error);
if (response.statusCode == BUYStatusComplete) {
[expectation fulfill];
}
if (response.statusCode == BUYStatusProcessing) {
pollCount += 1;
if (pollCount == 10) {
[self stubRequestsWithDelay:0.1 status:BUYStatusComplete];
}
return YES;
}
return NO;
};
[self.queue addOperation:operation];
[self waitForExpectationsWithTimeout:5.0 handler:nil];
}
- (void)testCancellationBeforeExecution - (void)testCancellationBeforeExecution
{ {
[self stubRequests]; [self stubRequests];
...@@ -223,6 +262,32 @@ ...@@ -223,6 +262,32 @@
[self waitForExpectationsWithTimeout:4.0 handler:nil]; [self waitForExpectationsWithTimeout:4.0 handler:nil];
} }
- (void)testCancellationDuringPolling
{
[self stubRequestsWithDelay:0.1 status:BUYStatusProcessing];
BUYRequestOperation *operation = [self operationFulfillingExpectation:nil completion:^{
XCTAssert(NO, @"Operation should not call completion if cancelled.");
}];
__block int pollCount = 0;
operation.pollingHandler = ^BOOL (NSDictionary *json, NSHTTPURLResponse *response, NSError *error) {
pollCount += 1;
return YES;
};
[self.queue addOperation:operation];
[self after:0.5 block:^{
[operation cancel];
}];
[self createExpectationDelay:1.0 block:YES];
XCTAssertTrue(pollCount < 5);
}
#pragma mark - Convenience - #pragma mark - Convenience -
- (void)asyncMain:(dispatch_block_t)block - (void)asyncMain:(dispatch_block_t)block
...@@ -239,13 +304,22 @@ ...@@ -239,13 +304,22 @@
{ {
[self createExpectationDelay:1.0]; [self createExpectationDelay:1.0];
} }
- (void)createExpectationDelay:(NSTimeInterval)delay
{
[self createExpectationDelay:delay block:NO];
}
- (void)createExpectationDelay:(NSTimeInterval)delay - (void)createExpectationDelay:(NSTimeInterval)delay block:(BOOL)block
{ {
XCTestExpectation *expectation = [self expectationWithDescription:@"Delay"]; XCTestExpectation *expectation = [self expectationWithDescription:@"Delay"];
[self after:delay block:^{ [self after:delay block:^{
[expectation fulfill]; [expectation fulfill];
}]; }];
if (block) {
[self waitForExpectationsWithTimeout:delay + 0.1 handler:nil];
}
} }
- (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation completion:(dispatch_block_t)completion - (BUYRequestOperation *)operationFulfillingExpectation:(XCTestExpectation *)expectation completion:(dispatch_block_t)completion
...@@ -280,13 +354,18 @@ ...@@ -280,13 +354,18 @@
- (void)stubRequestsWithDelay:(NSTimeInterval)delay - (void)stubRequestsWithDelay:(NSTimeInterval)delay
{ {
[self stubRequestsWithDelay:delay status:BUYStatusProcessing];
}
- (void)stubRequestsWithDelay:(NSTimeInterval)delay status:(int)status
{
NSDictionary *payload = @{ NSDictionary *payload = @{
@"first_name" : @"John", @"first_name" : @"John",
@"last_name" : @"Smith", @"last_name" : @"Smith",
}; };
NSData *payloadData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil]; NSData *payloadData = [NSJSONSerialization dataWithJSONObject:payload options:0 error:nil];
OHHTTPStubsResponse *response = [OHHTTPStubsResponse responseWithData:payloadData statusCode:200 headers:nil]; OHHTTPStubsResponse *response = [OHHTTPStubsResponse responseWithData:payloadData statusCode:status headers:nil];
response.requestTime = delay; response.requestTime = delay;
[OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) { [OHHTTPStubs stubRequestsPassingTest:^BOOL(NSURLRequest *request) {
......
...@@ -29,14 +29,15 @@ NS_ASSUME_NONNULL_BEGIN ...@@ -29,14 +29,15 @@ NS_ASSUME_NONNULL_BEGIN
@protocol BUYSerializable; @protocol BUYSerializable;
typedef void (^BUYRequestOperationCompletion)(NSDictionary * _Nullable json, NSURLResponse * _Nullable response, NSError * _Nullable error); typedef void (^BUYRequestOperationCompletion)(NSDictionary * _Nullable json, NSHTTPURLResponse * _Nullable response, NSError * _Nullable error);
typedef BOOL (^BUYRequestOperationPollingHandler)(NSDictionary * _Nullable json, NSURLResponse * _Nullable response, NSError * _Nullable error); typedef BOOL (^BUYRequestOperationPollingHandler)(NSDictionary * _Nullable json, NSHTTPURLResponse * _Nullable response, NSError * _Nullable error);
@interface BUYRequestOperation : BUYOperation @interface BUYRequestOperation : BUYOperation
@property (strong, nonatomic, readonly, nonnull) NSURLSession *session; @property (strong, nonatomic, readonly, nonnull) NSURLSession *session;
@property (strong, nonatomic, readonly, nonnull) NSURLRequest *originalRequest; @property (strong, nonatomic, readonly, nonnull) NSURLRequest *originalRequest;
@property (strong, nonatomic, readonly, nullable) BUYRequestOperationPollingHandler pollingHandler;
@property (strong, nonatomic, nullable) BUYRequestOperationPollingHandler pollingHandler;
+ (instancetype)operationWithSession:(NSURLSession *)session request:(NSURLRequest *)request payload:(id<BUYSerializable> _Nullable)payload completion:(BUYRequestOperationCompletion)completion; + (instancetype)operationWithSession:(NSURLSession *)session request:(NSURLRequest *)request payload:(id<BUYSerializable> _Nullable)payload completion:(BUYRequestOperationCompletion)completion;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment