-
Notifications
You must be signed in to change notification settings - Fork 497
Expand file tree
/
Copy pathRACMulticastConnectionSpec.m
More file actions
175 lines (138 loc) · 4.78 KB
/
RACMulticastConnectionSpec.m
File metadata and controls
175 lines (138 loc) · 4.78 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//
// RACMulticastConnectionSpec.m
// ReactiveObjC
//
// Created by Josh Abernathy on 10/8/12.
// Copyright (c) 2012 GitHub, Inc. All rights reserved.
//
@import Quick;
@import Nimble;
#import "RACMulticastConnection.h"
#import "RACDisposable.h"
#import "RACSignal+Operations.h"
#import "RACSubscriber.h"
#import "RACReplaySubject.h"
#import "RACScheduler.h"
#import <libkern/OSAtomic.h>
QuickSpecBegin(RACMulticastConnectionSpec)
__block NSUInteger subscriptionCount = 0;
__block RACMulticastConnection *connection;
qck_beforeEach(^{
subscriptionCount = 0;
connection = [[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
subscriptionCount++;
return (RACDisposable *)nil;
}] publish];
expect(@(subscriptionCount)).to(equal(@0));
});
qck_describe(@"-connect", ^{
qck_it(@"should subscribe to the underlying signal", ^{
[connection connect];
expect(@(subscriptionCount)).to(equal(@1));
});
qck_it(@"should return the same disposable for each invocation", ^{
RACDisposable *d1 = [connection connect];
RACDisposable *d2 = [connection connect];
expect(d1).to(equal(d2));
expect(@(subscriptionCount)).to(equal(@1));
});
qck_it(@"should reconnect after disposal", ^{
RACDisposable *disposable1 = [connection connect];
expect(@(subscriptionCount)).to(equal(@1));
[disposable1 dispose];
RACDisposable *disposable2 = [connection connect];
expect(@(subscriptionCount)).to(equal(@2));
[disposable2 dispose];
});
qck_it(@"shouldn't race when connecting", ^{
dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
RACMulticastConnection *connection = [[RACSignal
defer:^ id {
dispatch_semaphore_wait(semaphore, DISPATCH_TIME_FOREVER);
return nil;
}]
publish];
__block RACDisposable *disposable;
[RACScheduler.scheduler schedule:^{
disposable = [connection connect];
dispatch_semaphore_signal(semaphore);
}];
expect([connection connect]).notTo(beNil());
dispatch_semaphore_signal(semaphore);
expect(disposable).toEventuallyNot(beNil());
});
});
qck_describe(@"-autoconnect", ^{
__block RACSignal *autoconnectedSignal;
qck_beforeEach(^{
autoconnectedSignal = [connection autoconnect];
});
qck_it(@"should subscribe to the multicasted signal on the first subscription", ^{
expect(@(subscriptionCount)).to(equal(@0));
[autoconnectedSignal subscribeNext:^(id x) {}];
expect(@(subscriptionCount)).to(equal(@1));
[autoconnectedSignal subscribeNext:^(id x) {}];
expect(@(subscriptionCount)).to(equal(@1));
});
qck_it(@"should dispose of the multicasted subscription when the signal has no subscribers", ^{
__block BOOL disposed = NO;
__block id<RACSubscriber> connectionSubscriber = nil;
RACSignal *signal = [[[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
// Keep the subscriber alive so it doesn't trigger disposal on dealloc
connectionSubscriber = subscriber;
subscriptionCount++;
return [RACDisposable disposableWithBlock:^{
disposed = YES;
}];
}] publish] autoconnect];
RACDisposable *disposable = [signal subscribeNext:^(id x) {}];
expect(@(disposed)).to(beFalsy());
[disposable dispose];
expect(@(disposed)).to(beTruthy());
});
qck_it(@"should reconnect after disposal", ^{
RACDisposable *disposable = [autoconnectedSignal subscribeNext:^(id x) {}];
expect(@(subscriptionCount)).to(equal(@1));
[disposable dispose];
disposable = [autoconnectedSignal subscribeNext:^(id x) {}];
expect(@(subscriptionCount)).to(equal(@2));
[disposable dispose];
});
qck_it(@"shouldn't dispose immediately when reconnecting", ^{
__block NSUInteger sub = 0;
__block NSUInteger unsub = 0;
RACSignal *signal = [[[RACSignal createSignal:^(id<RACSubscriber> subscriber) {
sub++;
return [RACDisposable disposableWithBlock:^{
unsub++;
}];
}] publish] autoconnect];
RACDisposable *disposable = [signal subscribeNext:^(id x) {}];
expect(@(unsub)).to(equal(@0));
[disposable dispose];
expect(@(unsub)).to(equal(@1));
disposable = [signal subscribeNext:^(id x) {}];
expect(@(sub)).to(equal(@2));
expect(@(unsub)).to(equal(@1));
[disposable dispose];
expect(@(unsub)).to(equal(@2));
});
qck_it(@"should replay values after disposal when multicasted to a replay subject", ^{
RACSubject *subject = [RACSubject subject];
RACSignal *signal = [[subject multicast:[RACReplaySubject subject]] autoconnect];
NSMutableArray *results1 = [NSMutableArray array];
RACDisposable *disposable = [signal subscribeNext:^(id x) {
[results1 addObject:x];
}];
[subject sendNext:@1];
[subject sendNext:@2];
expect(results1).to(equal((@[ @1, @2 ])));
[disposable dispose];
NSMutableArray *results2 = [NSMutableArray array];
[signal subscribeNext:^(id x) {
[results2 addObject:x];
}];
expect(results2).toEventually(equal((@[ @1, @2 ])));
});
});
QuickSpecEnd