Skip to content

Commit

Permalink
Merge pull request #36 from naldikt/bug/listenerCalledMultipleTimes
Browse files Browse the repository at this point in the history
Listener block called multiple times for multiple instances
  • Loading branch information
cnstoll committed May 27, 2015
2 parents cf42b42 + 731ccfa commit 68d03c6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 15 deletions.
59 changes: 46 additions & 13 deletions Example/MMWormhole/MMWormholeTests/MMWormholeTests.m
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

#import "MMWormhole.h"

#define ApplicationGroupIdentifier @"group.com.mutualmobile.wormhole"

@interface MMWormhole (TextExtension)

- (NSString *)messagePassingDirectoryPath;
Expand All @@ -37,7 +39,7 @@ - (void)tearDown {
}

- (void)testMessagePassingDirectory {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];
NSString *messagePassingDirectoryPath = [wormhole messagePassingDirectoryPath];

Expand All @@ -47,7 +49,7 @@ - (void)testMessagePassingDirectory {
}

- (void)testFilePathForIdentifier {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];
NSString *filePathForIdentifier = [wormhole filePathForIdentifier:@"testIdentifier"];

Expand All @@ -57,7 +59,7 @@ - (void)testFilePathForIdentifier {
}

- (void)testFilePathForNilIdentifier {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];
NSString *filePathForIdentifier = [wormhole filePathForIdentifier:nil];

Expand All @@ -67,7 +69,7 @@ - (void)testFilePathForNilIdentifier {
}

- (void)testPassingMessageWithNilIdentifier {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole passMessageObject:@{} identifier:nil];
Expand All @@ -76,7 +78,7 @@ - (void)testPassingMessageWithNilIdentifier {
}

- (void)testValidMessagePassing {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole deleteFileForIdentifier:@"testIdentifier"];
Expand All @@ -95,7 +97,7 @@ - (void)testValidMessagePassing {
}

- (void)testFileWriting {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole deleteFileForIdentifier:@"testIdentifier"];
Expand All @@ -114,7 +116,7 @@ - (void)testFileWriting {
}

- (void)testClearingIndividualMessage {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole passMessageObject:@{} identifier:@"testIdentifier"];
Expand All @@ -137,7 +139,7 @@ - (void)testClearingIndividualMessage {
}

- (void)testClearingAllMessages {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole passMessageObject:@{} identifier:@"testIdentifier1"];
Expand All @@ -156,7 +158,7 @@ - (void)testClearingAllMessages {
}

- (void)testMessageListening {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

XCTestExpectation *expectation = [self expectationWithDescription:@"Listener should hear something"];
Expand All @@ -170,14 +172,45 @@ - (void)testMessageListening {
// Simulate a fake notification since Darwin notifications aren't delivered to the sender

[[NSNotificationCenter defaultCenter] postNotificationName:@"MMWormholeNotificationName"
object:nil
object:wormhole
userInfo:@{@"identifier" : @"testIdentifier"}];

[self waitForExpectationsWithTimeout:2 handler:nil];
}

- (void)testMessageListeningWithMultipleInstances {

__block int wormhole1ListenerCounter = 0;
__block int wormhole2ListenerCounter = 0;

// Instance 1
MMWormhole *wormhole1 = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole1 listenForMessageWithIdentifier:@"testIdentifier" listener:^(id messageObject) {
wormhole1ListenerCounter++;
}];


// Instance 2
MMWormhole *wormhole2 = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole2 listenForMessageWithIdentifier:@"testIdentifier" listener:^(id messageObject) {
wormhole2ListenerCounter++;
}];

// Simulate a fake notification since Darwin notifications aren't delivered to the sender

[[NSNotificationCenter defaultCenter] postNotificationName:@"MMWormholeNotificationName"
object:wormhole1
userInfo:@{@"identifier" : @"testIdentifier"}];

XCTAssertTrue( wormhole1ListenerCounter == 1 && wormhole2ListenerCounter == 0 , @"Valid multiple listener blocks with multiple instances should only be called once, on wormhole1." );
}

- (void)testStopMessageListening {
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

XCTestExpectation *expectation = [self expectationWithDescription:@"Listener should hear something"];
Expand All @@ -191,7 +224,7 @@ - (void)testStopMessageListening {
// Simulate a fake notification since Darwin notifications aren't delivered to the sender

[[NSNotificationCenter defaultCenter] postNotificationName:@"MMWormholeNotificationName"
object:nil
object:wormhole
userInfo:@{@"identifier" : @"testIdentifier"}];

[self waitForExpectationsWithTimeout:2 handler:^(NSError *error) {
Expand All @@ -209,7 +242,7 @@ - (void)testStopMessageListening {

- (void)testMessagePassingPerformance {
[self measureBlock:^{
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:@"group.com.mutualmobile.wormhole"
MMWormhole *wormhole = [[MMWormhole alloc] initWithApplicationGroupIdentifier:ApplicationGroupIdentifier
optionalDirectory:@"testDirectory"];

[wormhole passMessageObject:[self performanceSampleJSONObject] identifier:@"testPerformance"];
Expand Down
6 changes: 4 additions & 2 deletions Source/MMWormhole.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,11 @@ - (instancetype)initWithApplicationGroupIdentifier:(NSString *)identifier
_fileManager = [[NSFileManager alloc] init];
_listenerBlocks = [NSMutableDictionary dictionary];

// Only respects notification coming from self.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didReceiveMessageNotification:)
name:MMWormholeNotificationName
object:nil];
object:self];
}

return self;
Expand Down Expand Up @@ -195,8 +196,9 @@ void wormholeNotificationCallback(CFNotificationCenterRef center,
void const * object,
CFDictionaryRef userInfo) {
NSString *identifier = (__bridge NSString *)name;
NSObject *sender = (__bridge NSObject *)(observer);
[[NSNotificationCenter defaultCenter] postNotificationName:MMWormholeNotificationName
object:nil
object:sender
userInfo:@{@"identifier" : identifier}];
}

Expand Down

0 comments on commit 68d03c6

Please sign in to comment.