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

Edge case in EventBus#unregister #1630

Closed
gissuebot opened this issue Oct 31, 2014 · 3 comments
Closed

Edge case in EventBus#unregister #1630

gissuebot opened this issue Oct 31, 2014 · 3 comments
Assignees
Labels

Comments

@gissuebot
Copy link

Original issue created by stephan202 on 2014-01-06 at 10:22 AM


Scenario: in some unit tests, I'd like to test whether some object was (un)registered with an EventBus, similar to Issue 784. I use a construction akin to the following (I am aware of this method's side effect, that's fine for my purposes):

private void verifyRegistration(final EventBus eventBus, final Object object, final boolean expectedRegistered) {
    try {
        eventBus.unregister(object);
        assertTrue(expectedRegistered);
    } catch (final IllegalArgumentException e) {
        assertFalse(expectedRegistered);
    }   
}   

Now, it turns out that this does not work in case the object of interest does not define any @Subscribe methods. The documentation for EventBus#unregister states that an IllegalArgumentException is thrown "if the object was not previously registered", but in this case no Exception is thrown. The following two unit tests illustrate the issue more clearly (only the first one passes):

@Test(expected = IllegalArgumentException.class)
public void testUnregisterWithSubscriber() {
    new EventBus().unregister(new Object() {
        @Subscribe
        public void handleEvent(final Object event) { } 
    }); 
}   

@Test(expected = IllegalArgumentException.class)
public void testUnregisterWithNonSubscriber() {
    new EventBus().unregister(new Object());
}   

NB: I was torn between labeling this issue Type-Defect and Type-ApiDocs. Went with the former as I think a code fix is to be preferred over a documentation fix.

@gissuebot
Copy link
Author

Original comment posted by cgdecker@google.com on 2014-01-06 at 06:16 PM


Hmm... the problem with doing this is that we don't currently store anything at all when an object has no @Subscribe methods, because we only need to store event type -> subscriber mappings. Having to add a whole new Set<Object> for everything that's been passed to register just to make this work seems undesirable to me, though it wouldn't be the end of the world. It does seem slightly preferable for unregister to be throwing an exception if the object was never passed to register regardless of whether or not it has an @Subscribe method though.

@gissuebot
Copy link
Author

Original comment posted by Maaartinus on 2014-01-08 at 07:50 PM


Having to add a whole new Set<Object> for everything that's been passed to register

It would be enough to store the non-subscribers. However, they may constitute the majority as it's common to register all newly created objects without any thoughts.

There's an asymmetry in the behavior: While unregister seems to try to protect people from accidentally passing a wrong object, register accepts everything and even allows registering twice (which is a no-op). Assuming there's a good reason for un-registering throwing (which I personally doubt), the same reason should apply for re-registering.

@gissuebot gissuebot added type=defect Bug, not working as expected migrated package=eventbus labels Nov 1, 2014
@cgdecker cgdecker removed the migrated label Nov 1, 2014
@raghsriniv raghsriniv added the P3 no SLO label Jul 30, 2019
@cgdecker cgdecker added P4 no SLO and removed P3 no SLO labels Oct 30, 2019
@cgdecker cgdecker self-assigned this Apr 19, 2021
@cgdecker
Copy link
Member

I'm closing this issue because we are no longer going to be making changes to EventBus other than important bug fixes. The reasons we're discouraging it and some alternatives are listed in the EventBus javadoc.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants