/
githubmirror
/
RxJava
Обзор
Документация
Войти
/
githubmirror
/
RxJava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
4.x
src/test/java/io/reactivex/rxjava4/internal/schedulers/TrampolineSchedulerInternalTest.java
200 строк
6 KB
David Karnok
4.x: Convert from JUnit 4 to JUnit 6 (#8202)
30 июн 2026, 12:01
Не верифицирован
30 июн 2026, 12:01
531388f
Код
Авторство
О чём код?
/* * Copyright (c) 2016-present, RxJava Contributors. * * 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.rxjava4.internal.schedulers; import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.*; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import org.junit.jupiter.api.Test; import io.reactivex.rxjava4.core.*; import io.reactivex.rxjava4.core.Scheduler.Worker; import io.reactivex.rxjava4.disposables.Disposable; import io.reactivex.rxjava4.internal.disposables.EmptyDisposable; import io.reactivex.rxjava4.internal.functions.Functions; import io.reactivex.rxjava4.internal.schedulers.TrampolineScheduler.*; import io.reactivex.rxjava4.schedulers.Schedulers; import io.reactivex.rxjava4.testsupport.*; public class TrampolineSchedulerInternalTest extends RxJavaTest { @Test @SuppressUndeliverable public void scheduleDirectInterrupt() { Thread.currentThread().interrupt(); final int[] calls = { 0 }; assertSame(EmptyDisposable.INSTANCE, Schedulers.trampoline().scheduleDirect(() -> calls[0]++, 1, TimeUnit.SECONDS)); assertTrue(Thread.interrupted()); assertEquals(0, calls[0]); } @Test public void dispose() { Worker w = Schedulers.trampoline().createWorker(); assertFalse(w.isDisposed()); w.dispose(); assertTrue(w.isDisposed()); assertEquals(EmptyDisposable.INSTANCE, w.schedule(Functions.EMPTY_RUNNABLE)); } @Test public void reentrantScheduleDispose() { final Worker w = Schedulers.trampoline().createWorker(); try { final int[] calls = { 0, 0 }; w.schedule(() -> { calls[0]++; w.schedule(() -> calls[1]++) .dispose(); }); assertEquals(1, calls[0]); assertEquals(0, calls[1]); } finally { w.dispose(); } } @Test public void reentrantScheduleShutdown() { final Worker w = Schedulers.trampoline().createWorker(); try { final int[] calls = { 0, 0 }; w.schedule(() -> { calls[0]++; w.schedule(() -> calls[1]++, 1, TimeUnit.MILLISECONDS); w.dispose(); }); assertEquals(1, calls[0]); assertEquals(0, calls[1]); } finally { w.dispose(); } } @Test public void reentrantScheduleShutdown2() { final Worker w = Schedulers.trampoline().createWorker(); try { final int[] calls = { 0, 0 }; w.schedule(() -> { calls[0]++; w.dispose(); assertSame(EmptyDisposable.INSTANCE, w.schedule(() -> calls[1]++, 1, TimeUnit.MILLISECONDS)); }); assertEquals(1, calls[0]); assertEquals(0, calls[1]); } finally { w.dispose(); } } @Test @SuppressUndeliverable public void reentrantScheduleInterrupt() { final Worker w = Schedulers.trampoline().createWorker(); try { final int[] calls = { 0 }; Thread.currentThread().interrupt(); w.schedule(() -> calls[0]++, 1, TimeUnit.DAYS); assertTrue(Thread.interrupted()); assertEquals(0, calls[0]); } finally { w.dispose(); } } @Test public void sleepingRunnableDisposedOnRun() { TrampolineWorker w = new TrampolineWorker(); Runnable r = mock(Runnable.class); SleepingRunnable run = new SleepingRunnable(r, w, 0); w.dispose(); run.run(); verify(r, never()).run(); } @Test public void sleepingRunnableNoDelayRun() { TrampolineWorker w = new TrampolineWorker(); Runnable r = mock(Runnable.class); SleepingRunnable run = new SleepingRunnable(r, w, 0); run.run(); verify(r).run(); } @Test public void sleepingRunnableDisposedOnDelayedRun() { final TrampolineWorker w = new TrampolineWorker(); Runnable r = mock(Runnable.class); SleepingRunnable run = new SleepingRunnable(r, w, System.currentTimeMillis() + 200); Schedulers.single().scheduleDirect(w::dispose, 100, TimeUnit.MILLISECONDS); run.run(); verify(r, never()).run(); } @Test public void submitAndDisposeNextTask() { Scheduler.Worker w = Schedulers.trampoline().createWorker(); for (int i = 0; i < TestHelper.RACE_DEFAULT_LOOPS; i++) { Runnable run = mock(Runnable.class); AtomicInteger sync = new AtomicInteger(2); w.schedule(() -> { Disposable d = w.schedule(run); Schedulers.single().scheduleDirect(() -> { if (sync.decrementAndGet() != 0) { while (sync.get() != 0) { } } d.dispose(); }); if (sync.decrementAndGet() != 0) { while (sync.get() != 0) { } } }); } } }