|
| 1 | +@import Foundation; |
| 2 | +@import PromiseKit; |
| 3 | +@import XCTest; |
| 4 | +#define PMKTestErrorDomain @"PMKTestErrorDomain" |
| 5 | + |
| 6 | +static inline NSError *dummyWithCode(NSInteger code) { |
| 7 | + return [NSError errorWithDomain:PMKTestErrorDomain code:rand() userInfo:@{NSLocalizedDescriptionKey: @(code).stringValue}]; |
| 8 | +} |
| 9 | + |
| 10 | +@interface RaceTests : XCTestCase @end @implementation RaceTests |
| 11 | + |
| 12 | +- (void)test_race { |
| 13 | + id ex = [self expectationWithDescription:@""]; |
| 14 | + id p = PMKAfter(0.1).then(^{ return @2; }); |
| 15 | + PMKRace(@[PMKAfter(10), PMKAfter(20), p]).then(^(id obj){ |
| 16 | + XCTAssertEqual(2, [obj integerValue]); |
| 17 | + [ex fulfill]; |
| 18 | + }); |
| 19 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 20 | +} |
| 21 | + |
| 22 | +- (void)test_race_empty { |
| 23 | + id ex = [self expectationWithDescription:@""]; |
| 24 | + PMKRace(@[]).then(^(NSArray* array){ |
| 25 | + XCTFail(); |
| 26 | + [ex fulfill]; |
| 27 | + }).catch(^(NSError *e){ |
| 28 | + XCTAssertEqual(e.domain, PMKErrorDomain); |
| 29 | + XCTAssertEqual(e.code, PMKInvalidUsageError); |
| 30 | + XCTAssertEqualObjects(e.userInfo[NSLocalizedDescriptionKey], @"PMKRace(nil)"); |
| 31 | + [ex fulfill]; |
| 32 | + }); |
| 33 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 34 | +} |
| 35 | + |
| 36 | +- (void)test_race_fullfilled { |
| 37 | + id ex = [self expectationWithDescription:@""]; |
| 38 | + NSArray* promises = @[ |
| 39 | + PMKAfter(1).then(^{ return dummyWithCode(1); }), |
| 40 | + PMKAfter(2).then(^{ return dummyWithCode(2); }), |
| 41 | + PMKAfter(5).then(^{ return @1; }), |
| 42 | + PMKAfter(4).then(^{ return @2; }), |
| 43 | + PMKAfter(3).then(^{ return dummyWithCode(3); }) |
| 44 | + ]; |
| 45 | + PMKRaceFulfilled(promises).then(^(id obj){ |
| 46 | + XCTAssertEqual(2, [obj integerValue]); |
| 47 | + [ex fulfill]; |
| 48 | + }).catch(^{ |
| 49 | + XCTFail(); |
| 50 | + [ex fulfill]; |
| 51 | + }); |
| 52 | + [self waitForExpectationsWithTimeout:10 handler:nil]; |
| 53 | +} |
| 54 | + |
| 55 | +- (void)test_race_fulfilled_empty { |
| 56 | + id ex = [self expectationWithDescription:@""]; |
| 57 | + PMKRaceFulfilled(@[]).then(^(NSArray* array){ |
| 58 | + XCTFail(); |
| 59 | + [ex fulfill]; |
| 60 | + }).catch(^(NSError *e){ |
| 61 | + XCTAssertEqual(e.domain, PMKErrorDomain); |
| 62 | + XCTAssertEqual(e.code, PMKInvalidUsageError); |
| 63 | + XCTAssertEqualObjects(e.userInfo[NSLocalizedDescriptionKey], @"PMKRaceFulfilled(nil)"); |
| 64 | + [ex fulfill]; |
| 65 | + }); |
| 66 | + [self waitForExpectationsWithTimeout:1 handler:nil]; |
| 67 | +} |
| 68 | + |
| 69 | +- (void)test_race_fullfilled_with_no_winner { |
| 70 | + id ex = [self expectationWithDescription:@""]; |
| 71 | + NSArray* promises = @[ |
| 72 | + PMKAfter(1).then(^{ return dummyWithCode(1); }), |
| 73 | + PMKAfter(2).then(^{ return dummyWithCode(2); }), |
| 74 | + PMKAfter(3).then(^{ return dummyWithCode(3); }) |
| 75 | + ]; |
| 76 | + PMKRaceFulfilled(promises).then(^(id obj){ |
| 77 | + XCTFail(); |
| 78 | + [ex fulfill]; |
| 79 | + }).catch(^(NSError *e){ |
| 80 | + XCTAssertEqual(e.domain, PMKErrorDomain); |
| 81 | + XCTAssertEqual(e.code, PMKNoWinnerError); |
| 82 | + XCTAssertEqualObjects(e.userInfo[NSLocalizedDescriptionKey], @"PMKRaceFulfilled(nil)"); |
| 83 | + [ex fulfill]; |
| 84 | + }); |
| 85 | + [self waitForExpectationsWithTimeout:10 handler:nil]; |
| 86 | +} |
| 87 | + |
| 88 | +@end |
0 commit comments