Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #47 : RACMulticastConnection should reconnect after disposal. #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions ReactiveObjC/RACMulticastConnection.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ @interface RACMulticastConnection () {
}

@property (nonatomic, readonly, strong) RACSignal *sourceSignal;
@property (strong) RACSerialDisposable *serialDisposable;
@property (atomic, strong) RACSerialDisposable *serialDisposable;
@end

@implementation RACMulticastConnection
Expand All @@ -51,10 +51,19 @@ - (instancetype)initWithSourceSignal:(RACSignal *)source subject:(RACSubject *)s
#pragma mark Connecting

- (RACDisposable *)connect {
BOOL shouldConnect = OSAtomicCompareAndSwap32Barrier(0, 1, &_hasConnected);
int32_t volatile *hasConnected = &_hasConnected;
BOOL shouldConnect = OSAtomicCompareAndSwap32Barrier(0, 1, hasConnected);

if (shouldConnect) {
self.serialDisposable.disposable = [self.sourceSignal subscribe:_signal];
if ([self.serialDisposable isDisposed]) {
self.serialDisposable = [[RACSerialDisposable alloc] init];
}

RACDisposable *d = [self.sourceSignal subscribe:_signal];
self.serialDisposable.disposable = [RACDisposable disposableWithBlock:^{
OSAtomicCompareAndSwap32Barrier(1, 0, hasConnected);
[d dispose];
}];
}

return self.serialDisposable;
Expand Down
35 changes: 30 additions & 5 deletions ReactiveObjCTests/RACMulticastConnectionSpec.m
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,16 @@
expect(@(subscriptionCount)).to(equal(@1));
});

qck_it(@"shouldn't reconnect after disposal", ^{
qck_it(@"should reconnect after disposal", ^{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The fact that there's a test for this means that the behavior is intentional.

I'm not entirely sure why this is the case. Maybe one of the other maintainers does. But I'm not personally comfortable accepting this PR given how little I know about it.

Copy link
Author

@nverinaud nverinaud Dec 4, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it is intentional. But it should reconnect because it is the way it works in the original Rx (C#).

RACDisposable *disposable1 = [connection connect];
expect(@(subscriptionCount)).to(equal(@1));

[disposable1 dispose];

RACDisposable *disposable2 = [connection connect];
expect(@(subscriptionCount)).to(equal(@1));
expect(disposable1).to(equal(disposable2));
expect(@(subscriptionCount)).to(equal(@2));

[disposable2 dispose];
});

qck_it(@"shouldn't race when connecting", ^{
Expand Down Expand Up @@ -114,14 +115,38 @@
expect(@(disposed)).to(beTruthy());
});

qck_it(@"shouldn't reconnect after disposal", ^{
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(@1));
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", ^{
Expand Down