Back to Repositories

Testing Event Delivery Cancellation Workflow in GreenRobot EventBus

This test suite validates the event delivery cancellation functionality in GreenRobot’s EventBus library. It examines the behavior of canceling event propagation between subscribers and ensures proper exception handling when cancellation is attempted under various conditions.

Test Coverage Overview

The test suite provides comprehensive coverage of EventBus’s event cancellation mechanisms.

  • Tests basic event cancellation between multiple subscribers
  • Verifies cancellation behavior with different priority levels
  • Validates exception handling for invalid cancellation attempts
  • Tests thread-specific cancellation scenarios

Implementation Analysis

The testing approach uses JUnit to systematically verify event cancellation functionality. The implementation employs a hierarchical structure with nested test classes and prioritized event handlers.

Key patterns include:
  • Priority-based subscriber testing using @Subscribe annotations
  • Exception validation for illegal cancellation scenarios
  • Thread-mode specific testing for main thread operations

Technical Details

Testing infrastructure includes:

  • JUnit test framework for assertion handling
  • CountDownLatch for thread synchronization
  • Custom Subscriber classes with priority configurations
  • ThreadMode specifications for execution context control

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific cancellation scenarios
  • Proper exception handling validation
  • Clear separation of concerns between test cases
  • Comprehensive edge case coverage
  • Well-structured test hierarchy

greenrobot/eventbus

EventBusTestJava/src/main/java/org/greenrobot/eventbus/EventBusCancelEventDeliveryTest.java

            
/*
 * Copyright (C) 2012-2016 Markus Junginger, greenrobot (http://greenrobot.org)
 *
 * 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 org.greenrobot.eventbus;

import org.junit.Test;

import java.util.concurrent.CountDownLatch;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.fail;

public class EventBusCancelEventDeliveryTest extends AbstractEventBusTest {

    Throwable failed;

    @Test
    public void testCancel() {
        Subscriber canceler = new Subscriber(1, true);
        eventBus.register(new Subscriber(0, false));
        eventBus.register(canceler);
        eventBus.register(new Subscriber(0, false));
        eventBus.post("42");
        assertEquals(1, eventCount.intValue());

        eventBus.unregister(canceler);
        eventBus.post("42");
        assertEquals(1 + 2, eventCount.intValue());
    }

    @Test
    public void testCancelInBetween() {
        eventBus.register(new Subscriber(2, true));
        eventBus.register(new Subscriber(1, false));
        eventBus.register(new Subscriber(3, false));
        eventBus.post("42");
        assertEquals(2, eventCount.intValue());
    }

    @Test
    public void testCancelOutsideEventHandler() {
        try {
            eventBus.cancelEventDelivery(this);
            fail("Should have thrown");
        } catch (EventBusException e) {
            // Expected
        }
    }

    @Test
    public void testCancelWrongEvent() {
        eventBus.register(new SubscriberCancelOtherEvent());
        eventBus.post("42");
        assertEquals(0, eventCount.intValue());
        assertNotNull(failed);
    }

    public class Subscriber {
        private final int prio;
        private final boolean cancel;

        public Subscriber(int prio, boolean cancel) {
            this.prio = prio;
            this.cancel = cancel;
        }

        @Subscribe
        public void onEvent(String event) {
            handleEvent(event, 0);
        }

        @Subscribe(priority = 1)
        public void onEvent1(String event) {
            handleEvent(event, 1);
        }

        @Subscribe(priority = 2)
        public void onEvent2(String event) {
            handleEvent(event, 2);
        }

        @Subscribe(priority = 3)
        public void onEvent3(String event) {
            handleEvent(event, 3);
        }

        private void handleEvent(String event, int prio) {
            if(this.prio == prio) {
                trackEvent(event);
                if (cancel) {
                    eventBus.cancelEventDelivery(event);
                }
            }
        }
    }

    public class SubscriberCancelOtherEvent {
        @Subscribe
        public void onEvent(String event) {
            try {
                eventBus.cancelEventDelivery(this);
            } catch (EventBusException e) {
                failed = e;
            }
        }
    }

    public class SubscriberMainThread {
        final CountDownLatch done = new CountDownLatch(1);

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEventMainThread(String event) {
            try {
                eventBus.cancelEventDelivery(event);
            } catch (EventBusException e) {
                failed = e;
            }
            done.countDown();
        }
    }

}