Skip to content

Commit

Permalink
Fixed NoSubscriberEvent delivery after unregister
Browse files Browse the repository at this point in the history
  • Loading branch information
greenrobot committed Nov 15, 2013
1 parent 6688dac commit 86d14d2
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 64 deletions.
2 changes: 1 addition & 1 deletion EventBus/build.gradle
Expand Up @@ -3,7 +3,7 @@ apply plugin: 'maven'
apply plugin: 'signing'

group = 'de.greenrobot'
version = '2.1.0'
version = '2.1.1-SNAPSHOT'
sourceCompatibility = 1.6

def isSnapshot = version.endsWith('-SNAPSHOT')
Expand Down
2 changes: 1 addition & 1 deletion EventBus/src/de/greenrobot/event/EventBus.java
Expand Up @@ -397,7 +397,7 @@ private void postSingleEvent(Object event, boolean isMainThread) throws Error {
synchronized (this) {
subscriptions = subscriptionsByEventType.get(clazz);
}
if (subscriptions != null) {
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postToSubscription(subscription, event, isMainThread);
}
Expand Down
@@ -1,62 +1,73 @@
/*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.event.test;

import de.greenrobot.event.NoSubscriberEvent;
import de.greenrobot.event.SubscriberExceptionEvent;

/**
* @author Markus Junginger, greenrobot
*/
public class EventBusNoSubscriberEventTest extends AbstractEventBusTest {

public void testNoSubscriberEvent() {
eventBus.register(this);
eventBus.post("Foo");
assertEventCount(1);
assertEquals(NoSubscriberEvent.class, lastEvent.getClass());
NoSubscriberEvent noSub = (NoSubscriberEvent) lastEvent;
assertEquals("Foo", noSub.originalEvent);
assertSame(eventBus, noSub.eventBus);
}

public void testBadNoSubscriberSubscriber() {
eventBus.configureLogSubscriberExceptions(false);
eventBus.register(this);
eventBus.register(new BadNoSubscriberSubscriber());
eventBus.post("Foo");
assertEventCount(2);

assertEquals(SubscriberExceptionEvent.class, lastEvent.getClass());
NoSubscriberEvent noSub = (NoSubscriberEvent) ((SubscriberExceptionEvent) lastEvent).causingEvent;
assertEquals("Foo", noSub.originalEvent);
}

public void onEvent(NoSubscriberEvent event) {
trackEvent(event);
}

public void onEvent(SubscriberExceptionEvent event) {
trackEvent(event);
}

class BadNoSubscriberSubscriber {
public void onEvent(NoSubscriberEvent event) {
throw new RuntimeException("I'm bad");
}
}

}
/*
* Copyright (C) 2012 Markus Junginger, greenrobot (http://greenrobot.de)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.greenrobot.event.test;

import de.greenrobot.event.NoSubscriberEvent;
import de.greenrobot.event.SubscriberExceptionEvent;

/**
* @author Markus Junginger, greenrobot
*/
public class EventBusNoSubscriberEventTest extends AbstractEventBusTest {

public void testNoSubscriberEvent() {
eventBus.register(this);
eventBus.post("Foo");
assertEventCount(1);
assertEquals(NoSubscriberEvent.class, lastEvent.getClass());
NoSubscriberEvent noSub = (NoSubscriberEvent) lastEvent;
assertEquals("Foo", noSub.originalEvent);
assertSame(eventBus, noSub.eventBus);
}

public void testNoSubscriberEventAfterUnregister() {
Object subscriber = new Object() {
@SuppressWarnings("unused")
public void onEvent(String dummy) {
}
};
eventBus.register(subscriber);
eventBus.unregister(subscriber);
testNoSubscriberEvent();
}

public void testBadNoSubscriberSubscriber() {
eventBus.configureLogSubscriberExceptions(false);
eventBus.register(this);
eventBus.register(new BadNoSubscriberSubscriber());
eventBus.post("Foo");
assertEventCount(2);

assertEquals(SubscriberExceptionEvent.class, lastEvent.getClass());
NoSubscriberEvent noSub = (NoSubscriberEvent) ((SubscriberExceptionEvent) lastEvent).causingEvent;
assertEquals("Foo", noSub.originalEvent);
}

public void onEvent(NoSubscriberEvent event) {
trackEvent(event);
}

public void onEvent(SubscriberExceptionEvent event) {
trackEvent(event);
}

class BadNoSubscriberSubscriber {
public void onEvent(NoSubscriberEvent event) {
throw new RuntimeException("I'm bad");
}
}

}
3 changes: 3 additions & 0 deletions README.md
Expand Up @@ -180,6 +180,9 @@ FAQ

Release History
---------------
### V2.1.1 (future release) Bug fix release
* Fixed NoSubscriberEvent delivery after unregister

### V2.1.0 (2013-11-15) Features and bug fix release, util package
* AsyncExecutor executes RunnableEx and wraps exceptions into FailureEvents
* Experimental: exception to UI mapping (for now based on dialogs)
Expand Down

0 comments on commit 86d14d2

Please sign in to comment.