Back to Repositories

Testing Android Scheduler Implementation in RxAndroid

This test suite validates the functionality of Android schedulers in RxAndroid, focusing on main thread scheduling, looper handling, and message queue behavior. It ensures proper integration between RxJava’s scheduling mechanisms and Android’s threading model.

Test Coverage Overview

The test suite provides comprehensive coverage of AndroidSchedulers functionality:

  • Main thread scheduler hook verification
  • Null looper handling and validation
  • Message queue asynchronous behavior testing
  • API version compatibility checks
Tests cover both positive scenarios and edge cases including SDK version differences.

Implementation Analysis

The testing approach utilizes Robolectric for Android environment simulation and JUnit for test structure. The implementation employs shadow classes for Looper and MessageQueue manipulation, allowing precise control over Android’s threading mechanisms.

Key patterns include setup/teardown hooks, atomic operations for thread safety, and systematic message queue verification.

Technical Details

Testing infrastructure includes:

  • Robolectric TestRunner for Android environment simulation
  • JUnit 4 test framework
  • Shadow classes for Android components
  • RxAndroidPlugins for scheduler manipulation
  • Custom EmptyScheduler implementation

Best Practices Demonstrated

The test suite exemplifies high-quality testing practices:

  • Proper test isolation through setup/teardown methods
  • Comprehensive exception testing
  • Platform version compatibility verification
  • Clear test method naming and organization
  • Effective use of assertions for validation

reactivex/rxandroid

rxandroid/src/test/java/io/reactivex/rxjava3/android/schedulers/AndroidSchedulersTest.java

            
/*
 * 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 io.reactivex.rxjava3.android.schedulers;

import android.os.Build;
import android.os.Looper;
import android.os.Message;

import io.reactivex.rxjava3.android.plugins.RxAndroidPlugins;
import io.reactivex.rxjava3.android.testutil.EmptyScheduler;
import io.reactivex.rxjava3.core.Scheduler;
import io.reactivex.rxjava3.functions.Function;

import java.util.concurrent.atomic.AtomicInteger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowLooper;
import org.robolectric.shadows.ShadowMessageQueue;
import org.robolectric.util.ReflectionHelpers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.robolectric.Shadows.shadowOf;

@RunWith(RobolectricTestRunner.class)
@Config(manifest=Config.NONE)
public final class AndroidSchedulersTest {

    @Before @After
    public void setUpAndTearDown() {
        RxAndroidPlugins.reset();
    }

    @Test
    public void mainThreadCallsThroughToHook() {
        final AtomicInteger called = new AtomicInteger();
        final Scheduler newScheduler = new EmptyScheduler();
        RxAndroidPlugins.setMainThreadSchedulerHandler(new Function<Scheduler, Scheduler>() {
            @Override public Scheduler apply(Scheduler scheduler) {
                called.getAndIncrement();
                return newScheduler;
            }
        });

        assertSame(newScheduler, AndroidSchedulers.mainThread());
        assertEquals(1, called.get());

        assertSame(newScheduler, AndroidSchedulers.mainThread());
        assertEquals(2, called.get());
    }

    @Test
    public void fromNullThrows() {
        try {
            AndroidSchedulers.from(null);
            fail();
        } catch (NullPointerException e) {
            assertEquals("looper == null", e.getMessage());
        }
    }

    @Test
    public void fromNullThrowsTwoArg() {
        try {
            AndroidSchedulers.from(null, false);
            fail();
        } catch (NullPointerException e) {
            assertEquals("looper == null", e.getMessage());
        }
    }

    @Test
    public void fromReturnsUsableScheduler() {
        assertNotNull(AndroidSchedulers.from(Looper.getMainLooper()));
    }

    @Test
    public void mainThreadAsyncMessagesByDefault() {
        ShadowLooper mainLooper = shadowOf(Looper.getMainLooper());
        mainLooper.pause();
        ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue());

        Scheduler main = AndroidSchedulers.mainThread();
        main.scheduleDirect(new Runnable() {
            @Override public void run() {
            }
        });

        Message message = mainMessageQueue.getHead();
        assertTrue(message.isAsynchronous());
    }

    @Test
    public void fromAsyncMessagesByDefault() {
        ShadowLooper mainLooper = shadowOf(Looper.getMainLooper());
        mainLooper.pause();
        ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue());

        Scheduler main = AndroidSchedulers.from(Looper.getMainLooper());
        main.scheduleDirect(new Runnable() {
            @Override public void run() {
            }
        });

        Message message = mainMessageQueue.getHead();
        assertTrue(message.isAsynchronous());
    }

    @Test
    public void asyncIgnoredPre16() {
        int oldValue = Build.VERSION.SDK_INT;
        ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 14);
        try {
            ShadowLooper mainLooper = shadowOf(Looper.getMainLooper());
            mainLooper.pause();
            ShadowMessageQueue mainMessageQueue = shadowOf(Looper.getMainLooper().getQueue());

            Scheduler main = AndroidSchedulers.from(Looper.getMainLooper(), true);
            main.scheduleDirect(new Runnable() {
                @Override public void run() {
                }
            });

            Message message = mainMessageQueue.getHead();
            assertFalse(message.isAsynchronous());
        } finally {
            ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", oldValue);
        }
    }
}