Skip to content

Commit ff66460

Browse files
committed
RaceTests.m
1 parent 00afea5 commit ff66460

File tree

4 files changed

+98
-48
lines changed

4 files changed

+98
-48
lines changed

PromiseKit.xcodeproj/project.pbxproj

+4
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
63CF6D80203CD19200EC8927 /* ThenableTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63CF6D7F203CD19200EC8927 /* ThenableTests.swift */; };
8181
63D9B2EF203385FD0075C00B /* race.m in Sources */ = {isa = PBXBuildFile; fileRef = 63D9B2EE203385FD0075C00B /* race.m */; };
8282
63D9B2F120338D5D0075C00B /* Deprecations.swift in Sources */ = {isa = PBXBuildFile; fileRef = 63D9B2F020338D5D0075C00B /* Deprecations.swift */; };
83+
9E66231626FE5A8C00FA25CB /* RaceTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 9E66231526FE5A8C00FA25CB /* RaceTests.m */; };
8384
C013F7382048E3B6006B57B1 /* MockNodeEnvironment.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */; };
8485
C013F73A2049076A006B57B1 /* JSPromise.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F7392049076A006B57B1 /* JSPromise.swift */; };
8586
C013F73C20494291006B57B1 /* JSAdapter.swift in Sources */ = {isa = PBXBuildFile; fileRef = C013F73B20494291006B57B1 /* JSAdapter.swift */; };
@@ -223,6 +224,7 @@
223224
63CF6D7F203CD19200EC8927 /* ThenableTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ThenableTests.swift; sourceTree = "<group>"; };
224225
63D9B2EE203385FD0075C00B /* race.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; name = race.m; path = Sources/race.m; sourceTree = "<group>"; };
225226
63D9B2F020338D5D0075C00B /* Deprecations.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = Deprecations.swift; path = Sources/Deprecations.swift; sourceTree = "<group>"; };
227+
9E66231526FE5A8C00FA25CB /* RaceTests.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = RaceTests.m; sourceTree = "<group>"; };
226228
C013F7372048E3B6006B57B1 /* MockNodeEnvironment.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = MockNodeEnvironment.swift; path = "Tests/JS-A+/MockNodeEnvironment.swift"; sourceTree = "<group>"; };
227229
C013F7392049076A006B57B1 /* JSPromise.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSPromise.swift; path = "Tests/JS-A+/JSPromise.swift"; sourceTree = "<group>"; };
228230
C013F73B20494291006B57B1 /* JSAdapter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; name = JSAdapter.swift; path = "Tests/JS-A+/JSAdapter.swift"; sourceTree = "<group>"; };
@@ -291,6 +293,7 @@
291293
630A8053203CEF6800D25F23 /* JoinTests.m */,
292294
630A8054203CEF6800D25F23 /* HangTests.m */,
293295
630A8055203CEF6800D25F23 /* WhenTests.m */,
296+
9E66231526FE5A8C00FA25CB /* RaceTests.m */,
294297
);
295298
name = CoreObjC;
296299
path = Tests/CoreObjC;
@@ -684,6 +687,7 @@
684687
630A805B203CF67800D25F23 /* DefaultDispatchQueueTests.swift in Sources */,
685688
635D641D1D59635300BC0AF5 /* PromiseTests.swift in Sources */,
686689
63CF6D7C203CCDAB00EC8927 /* GuaranteeTests.swift in Sources */,
690+
9E66231626FE5A8C00FA25CB /* RaceTests.m in Sources */,
687691
639BF757203DF03100FA577B /* Utilities.swift in Sources */,
688692
635D64261D59635300BC0AF5 /* WhenResolvedTests.swift in Sources */,
689693
635D64231D59635300BC0AF5 /* AfterTests.swift in Sources */,

Sources/race.m

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
// ^^ OSAtomicDecrement32 is deprecated on watchOS
77

88
AnyPromise *PMKRace(NSArray *promises) {
9+
if (promises == nil || promises.count == 0)
10+
return [AnyPromise promiseWithValue:[NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:@{NSLocalizedDescriptionKey: @"PMKRace(nil)"}]];
11+
912
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {
1013
for (AnyPromise *promise in promises) {
1114
[promise __pipe:resolve];
@@ -21,6 +24,9 @@
2124
@return The promise that was fulfilled first.
2225
*/
2326
AnyPromise *PMKRaceFulfilled(NSArray *promises) {
27+
if (promises == nil || promises.count == 0)
28+
return [AnyPromise promiseWithValue:[NSError errorWithDomain:PMKErrorDomain code:PMKInvalidUsageError userInfo:@{NSLocalizedDescriptionKey: @"PMKRaceFulfilled(nil)"}]];
29+
2430
__block int32_t countdown = (int32_t)[promises count];
2531

2632
return [AnyPromise promiseWithResolverBlock:^(PMKResolver resolve) {

Tests/CoreObjC/AnyPromiseTests.m

-48
Original file line numberDiff line numberDiff line change
@@ -789,54 +789,6 @@ - (void)test_promiseWithValue {
789789
XCTAssertEqual([AnyPromise promiseWithValue:[AnyPromise promiseWithValue:@1]].value, @1);
790790
}
791791

792-
- (void)test_race {
793-
id ex = [self expectationWithDescription:@""];
794-
id p = PMKAfter(0.1).then(^{ return @2; });
795-
PMKRace(@[PMKAfter(10), PMKAfter(20), p]).then(^(id obj){
796-
XCTAssertEqual(2, [obj integerValue]);
797-
[ex fulfill];
798-
});
799-
[self waitForExpectationsWithTimeout:1 handler:nil];
800-
}
801-
802-
- (void)test_race_fullfilled {
803-
id ex = [self expectationWithDescription:@""];
804-
NSArray* promises = @[
805-
PMKAfter(1).then(^{ return dummyWithCode(1); }),
806-
PMKAfter(2).then(^{ return dummyWithCode(2); }),
807-
PMKAfter(5).then(^{ return @1; }),
808-
PMKAfter(4).then(^{ return @2; }),
809-
PMKAfter(3).then(^{ return dummyWithCode(3); })
810-
];
811-
PMKRaceFulfilled(promises).then(^(id obj){
812-
XCTAssertEqual(2, [obj integerValue]);
813-
[ex fulfill];
814-
}).catch(^{
815-
XCTFail();
816-
[ex fulfill];
817-
});
818-
[self waitForExpectationsWithTimeout:10 handler:nil];
819-
}
820-
821-
- (void)test_race_fullfilled_with_no_winner {
822-
id ex = [self expectationWithDescription:@""];
823-
NSArray* promises = @[
824-
PMKAfter(1).then(^{ return dummyWithCode(1); }),
825-
PMKAfter(2).then(^{ return dummyWithCode(2); }),
826-
PMKAfter(3).then(^{ return dummyWithCode(3); })
827-
];
828-
PMKRaceFulfilled(promises).then(^(id obj){
829-
XCTFail();
830-
[ex fulfill];
831-
}).catch(^(NSError *e){
832-
XCTAssertEqual(e.domain, PMKErrorDomain);
833-
XCTAssertEqual(e.code, PMKNoWinnerError);
834-
XCTAssertEqualObjects(e.userInfo[NSLocalizedDescriptionKey], @"PMKRaceFulfilled(nil)");
835-
[ex fulfill];
836-
});
837-
[self waitForExpectationsWithTimeout:10 handler:nil];
838-
}
839-
840792
- (void)testInBackground {
841793
id ex = [self expectationWithDescription:@""];
842794
PMKAfter(0.1).thenInBackground(^{ [ex fulfill]; });

Tests/CoreObjC/RaceTests.m

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)