Back to Repositories

Testing EventBus Multithreaded Event Handling in greenrobot/EventBus

This test suite validates the multithreaded behavior of EventBus in Android environments, focusing on subscription, unsubscription, and event posting across different thread modes. It ensures reliable event handling in concurrent scenarios while maintaining thread safety.

Test Coverage Overview

The test suite provides comprehensive coverage of EventBus’s multithreaded capabilities in Android.

Key areas tested include:
  • Concurrent subscribe/unsubscribe operations
  • Mixed event type handling across different threads
  • Thread mode verification for event delivery
  • Main thread and background thread event handling

Implementation Analysis

The testing approach uses JUnit with AndroidJUnit4 runner for Android-specific thread handling verification.

Key patterns include:
  • Dynamic thread creation and management
  • Controlled concurrent operations using mathematical randomization
  • Thread mode annotations (@Subscribe) with different delivery options
  • Looper validation for proper thread context

Technical Details

Testing infrastructure includes:
  • AndroidJUnit4 test runner
  • Android Looper mechanisms for thread verification
  • Custom SubscribeUnsubscribeThread implementation
  • Thread management utilities
  • Assert methods for thread context validation

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper thread lifecycle management
  • Controlled concurrent testing scenarios
  • Clear separation of thread modes and contexts
  • Robust assertion patterns for thread safety
  • Efficient resource cleanup through shutdown mechanisms

greenrobot/eventbus

EventBusTest/src/org/greenrobot/eventbus/EventBusAndroidMultithreadedTest.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 org.junit.runner.RunWith;

import android.os.Looper;
import android.support.test.runner.AndroidJUnit4;

import java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertSame;

@RunWith(AndroidJUnit4.class)
public class EventBusAndroidMultithreadedTest extends EventBusMultithreadedTest {

    @Test
    public void testSubscribeUnSubscribeAndPostMixedEventType() throws InterruptedException {
        List<SubscribeUnsubscribeThread> threads = new ArrayList<SubscribeUnsubscribeThread>();

        // Debug.startMethodTracing("testSubscribeUnSubscribeAndPostMixedEventType");
        for (int i = 0; i < 5; i++) {
            SubscribeUnsubscribeThread thread = new SubscribeUnsubscribeThread();
            thread.start();
            threads.add(thread);
        }
        // This test takes a bit longer, so just use fraction the regular count
        runThreadsMixedEventType(COUNT / 4, 5);
        for (SubscribeUnsubscribeThread thread : threads) {
            thread.shutdown();
        }
        for (SubscribeUnsubscribeThread thread : threads) {
            thread.join();
        }
        // Debug.stopMethodTracing();
    }

    public class SubscribeUnsubscribeThread extends Thread {
        boolean running = true;

        public void shutdown() {
            running = false;
        }

        @Override
        public void run() {
            try {
                while (running) {
                    eventBus.register(this);
                    double random = Math.random();
                    if (random > 0.6d) {
                        Thread.sleep(0, (int) (1000000 * Math.random()));
                    } else if (random > 0.3d) {
                        Thread.yield();
                    }
                    eventBus.unregister(this);
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }

        @Subscribe(threadMode = ThreadMode.MAIN)
        public void onEventMainThread(String event) {
            assertSame(Looper.getMainLooper(), Looper.myLooper());
        }

        @Subscribe(threadMode = ThreadMode.BACKGROUND)
        public void onEventBackgroundThread(Integer event) {
            assertNotSame(Looper.getMainLooper(), Looper.myLooper());
        }

        @Subscribe
        public void onEvent(Object event) {
            assertNotSame(Looper.getMainLooper(), Looper.myLooper());
        }

        @Subscribe(threadMode = ThreadMode.ASYNC)
        public void onEventAsync(Object event) {
            assertNotSame(Looper.getMainLooper(), Looper.myLooper());
        }
    }

}