forked from samuraisam/DeferredKit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_DKDeferredTests.m
147 lines (123 loc) · 4.34 KB
/
_DKDeferredTests.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#import "DKDeferredTests.h"
@implementation DKDeferredTest
- (id)_testCallback2:(id)result {
NSLog(@"_testCallback2:%@", result);
return nil;
}
- (id)_testCallback3:(id)result {
NSLog(@"_testCallback3:%@", result);
return nil;
}
- (id)_testCallback:(id)result {
NSLog(@"_testCallback:%@", result);
DKDeferred *d = [DKDeferred deferred];
[d addCallbacks:functionTS(self, _testCallback2:)
:functionTS(self, _testErrback:)];
[d performSelector:@selector(callback:)
withObject:@"the second result bitch"
afterDelay:5.0f];
return d;
}
- (id)_testErrback:(id)result {
NSLog(@"_testErrback:%@", result);
return nil;
}
- (void)testDeferred {
DKDeferred *d = [DKDeferred deferred];
[d addCallbacks:functionTS(self, _testCallback:)
:functionTS(self, _testErrback:)];
sleep(5);
[d callback:@"tha result bitch"];
//[d errback:@"now error bitch"];
}
- (void)testDeferredList {
DKDeferred *d1 = [DKDeferred deferred];
[d1 addCallback:functionTS(self, _testCallback:)];
[d1 addErrback:functionTS(self, _testErrback:)];
DKDeferred *d2 = [DKDeferred deferred];
[d2 addCallbacks:functionTS(self, _testCallback2:)
:functionTS(self, _testErrback:)];
DKDeferredList *ll = [DKDeferredList deferredList:NSARRAY(d1, d2)];
[ll addBoth:functionTS(self, _testCallback3:)];
sleep(2);
[ll callback:@"hello mother chicken pluckerers!"];
}
- (id)waitCallback:(id)result {
NSLog(@"waitCallback");
return nil;
}
- (void)testWait {
DKDeferred *d = [DKDeferred wait:3 value:nil];
[d addBoth:functionTS(self, waitCallback:)];
}
- (id)callLaterFunc:(id)result {
NSLog(@"callLaterFunc");
return nil;
}
- (void)testCallLater {
[DKDeferred callLater:3 func:functionTS(self, callLaterFunc:)];
}
- (id)someBlockingOperation:(id)arg {
NSLog(@"Starting blocking op");
//sleep([arg intValue]);
sleep(2);
NSLog(@"Done op");
return @"Sonova bitch it works";
}
- (id)blockingCallback:(id)result {
NSLog(@"blockingCallback: %@", result);
return nil;
}
- (void)testThreadedDeferred {
DKDeferred *d = [DKDeferred deferInThread:functionTS(self, someBlockingOperation:)
withObject:[NSNumber numberWithInt:5]];
[d addBoth:functionTS(self, blockingCallback:)];
}
- (id)blockingListCallback:(id)result {
NSLog(@"blockingListCallback:%@", result);
return nil;
}
- (void)testThreadedDeferredList {
DKDeferred *d1 = [DKDeferred deferInThread:functionTS(self, someBlockingOperation:)
withObject:[NSNumber numberWithInt:2]];
DKDeferred *d2 = [DKDeferred deferInThread:functionTS(self, someBlockingOperation:)
withObject:[NSNumber numberWithInt:3]];
DKDeferred *ll = [DKDeferred gatherResults:NSARRAY(d1, d2)];
[ll addBoth:functionTS(self, blockingListCallback:)];
}
- (id)deferredURLCallback:(id)result {
NSLog(@"deferredURLCallback:%@", [NSString stringWithUTF8String:[(NSData *)result bytes]]);
return nil;
}
- (id)deferredJSONCallback:(id)result {
NSLog(@"deferredJSONCallback:%@", result);
return nil;
}
- (id)deferredJSONErrback:(NSError *)result {
NSLog(@"deferredJSONErrback:%@ %@", result, [result userInfo]);
return nil;
}
- (void)testDeferredURL {
DKDeferred *d = [DKDeferred loadURL:@"http://google.com/"];
[d addBoth:functionTS(self, deferredURLCallback:)];
}
- (void)testDeferredJSON {
DKDeferred *d = [DKDeferred loadJSONDoc:@"http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=pants"];
[d addBoth:functionTS(self, deferredJSONCallback:)];
}
- (void)testDeferredJSONProxy {
id service = [DKDeferred jsonService:@"http://devsam.com/json/" name:@"craigsfish"];
DKDeferred *d = [[service registerDeviceToken] sexyTime:NSARRAY(@"sam", @"sam", @"sam")];
[d addCallback:functionTS(self, deferredJSONCallback:)];
[d addErrback:functionTS(self, deferredJSONErrback:)];
}
@end
// [[[DKDeferredTest alloc] init] testDeferred];
// [[[DKDeferredTest alloc] init] testDeferredList];
// [[[DKDeferredTest alloc] init] testWait];
// [[[DKDeferredTest alloc] init] testCallLater];
// [[[DKDeferredTest alloc] init] testThreadedDeferred];
// [[[DKDeferredTest alloc] init] testThreadedDeferredList];
// [[[DKDeferredTest alloc] init] testDeferredURL];
// [[[DKDeferredTest alloc] init] testDeferredJSON];
// [[[DKDeferredTest alloc] init] testDeferredJSONProxy];