/
githubmirror
/
RxJava
Обзор
Документация
Войти
/
githubmirror
/
RxJava
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
4.x
src/test/java/io/reactivex/rxjava4/operators/SimpleQueueTest.java
186 строк
5 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. */ /* * The code was inspired by the similarly named JCTools class: * https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/queues/atomic */ package io.reactivex.rxjava4.operators; import static org.junit.jupiter.api.Assertions.*; import java.util.concurrent.atomic.*; import org.junit.jupiter.api.Test; import io.reactivex.rxjava4.core.RxJavaTest; import io.reactivex.rxjava4.internal.queue.MpscLinkedQueue; public class SimpleQueueTest extends RxJavaTest { @Test public void spscArrayQueueNull() { assertThrows(NullPointerException.class, () -> { SpscArrayQueue<Object> q = new SpscArrayQueue<>(16); q.offer(null); }); } @Test public void spscLinkedArrayQueueNull() { assertThrows(NullPointerException.class, () -> { SpscLinkedArrayQueue<Object> q = new SpscLinkedArrayQueue<>(16); q.offer(null); }); } @Test public void mpscLinkedQueueNull() { assertThrows(NullPointerException.class, () -> { MpscLinkedQueue<Object> q = new MpscLinkedQueue<>(); q.offer(null); }); } @Test public void spscArrayQueueBiOffer() { SpscArrayQueue<Object> q = new SpscArrayQueue<>(16); q.offer(1, 2); assertEquals(1, q.poll()); assertEquals(2, q.poll()); assertNull(q.poll()); } @Test public void spscLinkedArrayQueueBiOffer() { SpscLinkedArrayQueue<Object> q = new SpscLinkedArrayQueue<>(16); q.offer(1, 2); assertEquals(1, q.poll()); assertEquals(2, q.poll()); assertNull(q.poll()); } @Test public void mpscLinkedQueueBiOffer() { MpscLinkedQueue<Object> q = new MpscLinkedQueue<>(); q.offer(1, 2); assertEquals(1, q.poll()); assertEquals(2, q.poll()); assertNull(q.poll()); } @Test public void spscBiOfferCapacity() { SpscArrayQueue<Integer> q = new SpscArrayQueue<>(8); assertTrue(q.offer(1, 2)); assertTrue(q.offer(3, 4)); assertTrue(q.offer(5, 6)); assertTrue(q.offer(7)); assertFalse(q.offer(8, 9)); assertFalse(q.offer(9, 10)); } @Test public void spscLinkedNewBufferPeek() { SpscLinkedArrayQueue<Integer> q = new SpscLinkedArrayQueue<>(8); assertTrue(q.offer(1, 2)); assertTrue(q.offer(3, 4)); assertTrue(q.offer(5, 6)); assertTrue(q.offer(7, 8)); // this should trigger a new buffer for (int i = 0; i < 8; i++) { assertEquals(i + 1, q.peek().intValue()); assertEquals(i + 1, q.poll().intValue()); } assertNull(q.peek()); assertNull(q.poll()); } @Test public void mpscOfferPollRace() throws Exception { final MpscLinkedQueue<Integer> q = new MpscLinkedQueue<>(); final AtomicInteger c = new AtomicInteger(3); Thread t1 = new Thread(new Runnable() /* NFI */ { int i; @Override public void run() { c.decrementAndGet(); while (c.get() != 0) { } while (i++ < 10000) { q.offer(i); } } }); t1.start(); Thread t2 = new Thread(new Runnable() /* NFI */ { int i = 10000; @Override public void run() { c.decrementAndGet(); while (c.get() != 0) { } while (i++ < 10000) { q.offer(i); } } }); t2.start(); Runnable r3 = new Runnable() /* NFI */ { int i = 20000; @Override public void run() { c.decrementAndGet(); while (c.get() != 0) { } while (--i > 0) { q.poll(); } } }; r3.run(); t1.join(); t2.join(); } @Test public void spscLinkedArrayQueueNoNepotism() { SpscLinkedArrayQueue<Integer> q = new SpscLinkedArrayQueue<>(16); AtomicReferenceArray<Object> ara = q.producerBuffer; for (int i = 0; i < 20; i++) { q.offer(i); } assertNotNull(ara.get(16)); for (int i = 0; i < 20; i++) { assertEquals(i, q.poll().intValue()); } assertNull(ara.get(16)); } }