| 1 |
/* |
| 2 |
* Copyright (c) 2016-present, RxJava Contributors. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in |
| 5 |
* compliance with the License. You may obtain a copy of the License at |
| 6 |
* |
| 7 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
* |
| 9 |
* Unless required by applicable law or agreed to in writing, software distributed under the License is |
| 10 |
* distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See |
| 11 |
* the License for the specific language governing permissions and limitations under the License. |
| 12 |
*/ |
| 13 |
|
| 14 |
/* |
| 15 |
* The code was inspired by the similarly named JCTools class: |
| 16 |
* https://github.com/JCTools/JCTools/blob/master/jctools-core/src/main/java/org/jctools/queues/atomic |
| 17 |
*/ |
| 18 |
|
| 19 |
package io.reactivex.rxjava3.operators; |
| 20 |
|
| 21 |
import static org.junit.Assert.*; |
| 22 |
|
| 23 |
import java.util.concurrent.atomic.*; |
| 24 |
|
| 25 |
import org.junit.Test; |
| 26 |
|
| 27 |
import io.reactivex.rxjava3.core.RxJavaTest; |
| 28 |
import io.reactivex.rxjava3.internal.queue.MpscLinkedQueue; |
| 29 |
|
| 30 |
public class SimpleQueueTest extends RxJavaTest { |
| 31 |
|
| 32 |
@Test(expected = NullPointerException.class) |
| 33 |
public void spscArrayQueueNull() { |
| 34 |
SpscArrayQueue<Object> q = new SpscArrayQueue<>(16); |
| 35 |
q.offer(null); |
| 36 |
} |
| 37 |
|
| 38 |
@Test(expected = NullPointerException.class) |
| 39 |
public void spscLinkedArrayQueueNull() { |
| 40 |
SpscLinkedArrayQueue<Object> q = new SpscLinkedArrayQueue<>(16); |
| 41 |
q.offer(null); |
| 42 |
} |
| 43 |
|
| 44 |
@Test(expected = NullPointerException.class) |
| 45 |
public void mpscLinkedQueueNull() { |
| 46 |
MpscLinkedQueue<Object> q = new MpscLinkedQueue<>(); |
| 47 |
q.offer(null); |
| 48 |
} |
| 49 |
|
| 50 |
@Test |
| 51 |
public void spscArrayQueueBiOffer() { |
| 52 |
SpscArrayQueue<Object> q = new SpscArrayQueue<>(16); |
| 53 |
q.offer(1, 2); |
| 54 |
|
| 55 |
assertEquals(1, q.poll()); |
| 56 |
assertEquals(2, q.poll()); |
| 57 |
assertNull(q.poll()); |
| 58 |
} |
| 59 |
|
| 60 |
@Test |
| 61 |
public void spscLinkedArrayQueueBiOffer() { |
| 62 |
SpscLinkedArrayQueue<Object> q = new SpscLinkedArrayQueue<>(16); |
| 63 |
q.offer(1, 2); |
| 64 |
|
| 65 |
assertEquals(1, q.poll()); |
| 66 |
assertEquals(2, q.poll()); |
| 67 |
assertNull(q.poll()); |
| 68 |
} |
| 69 |
|
| 70 |
@Test |
| 71 |
public void mpscLinkedQueueBiOffer() { |
| 72 |
MpscLinkedQueue<Object> q = new MpscLinkedQueue<>(); |
| 73 |
q.offer(1, 2); |
| 74 |
|
| 75 |
assertEquals(1, q.poll()); |
| 76 |
assertEquals(2, q.poll()); |
| 77 |
assertNull(q.poll()); |
| 78 |
} |
| 79 |
|
| 80 |
@Test |
| 81 |
public void spscBiOfferCapacity() { |
| 82 |
SpscArrayQueue<Integer> q = new SpscArrayQueue<>(8); |
| 83 |
assertTrue(q.offer(1, 2)); |
| 84 |
assertTrue(q.offer(3, 4)); |
| 85 |
assertTrue(q.offer(5, 6)); |
| 86 |
assertTrue(q.offer(7)); |
| 87 |
|
| 88 |
assertFalse(q.offer(8, 9)); |
| 89 |
assertFalse(q.offer(9, 10)); |
| 90 |
} |
| 91 |
|
| 92 |
@Test |
| 93 |
public void spscLinkedNewBufferPeek() { |
| 94 |
SpscLinkedArrayQueue<Integer> q = new SpscLinkedArrayQueue<>(8); |
| 95 |
assertTrue(q.offer(1, 2)); |
| 96 |
assertTrue(q.offer(3, 4)); |
| 97 |
assertTrue(q.offer(5, 6)); |
| 98 |
assertTrue(q.offer(7, 8)); // this should trigger a new buffer |
| 99 |
|
| 100 |
for (int i = 0; i < 8; i++) { |
| 101 |
assertEquals(i + 1, q.peek().intValue()); |
| 102 |
assertEquals(i + 1, q.poll().intValue()); |
| 103 |
} |
| 104 |
|
| 105 |
assertNull(q.peek()); |
| 106 |
assertNull(q.poll()); |
| 107 |
} |
| 108 |
|
| 109 |
@Test |
| 110 |
public void mpscOfferPollRace() throws Exception { |
| 111 |
final MpscLinkedQueue<Integer> q = new MpscLinkedQueue<>(); |
| 112 |
|
| 113 |
final AtomicInteger c = new AtomicInteger(3); |
| 114 |
|
| 115 |
Thread t1 = new Thread(new Runnable() { |
| 116 |
int i; |
| 117 |
@Override |
| 118 |
public void run() { |
| 119 |
c.decrementAndGet(); |
| 120 |
while (c.get() != 0) { } |
| 121 |
|
| 122 |
while (i++ < 10000) { |
| 123 |
q.offer(i); |
| 124 |
} |
| 125 |
} |
| 126 |
}); |
| 127 |
t1.start(); |
| 128 |
|
| 129 |
Thread t2 = new Thread(new Runnable() { |
| 130 |
int i = 10000; |
| 131 |
@Override |
| 132 |
public void run() { |
| 133 |
c.decrementAndGet(); |
| 134 |
while (c.get() != 0) { } |
| 135 |
|
| 136 |
while (i++ < 10000) { |
| 137 |
q.offer(i); |
| 138 |
} |
| 139 |
} |
| 140 |
}); |
| 141 |
t2.start(); |
| 142 |
|
| 143 |
Runnable r3 = new Runnable() { |
| 144 |
int i = 20000; |
| 145 |
@Override |
| 146 |
public void run() { |
| 147 |
c.decrementAndGet(); |
| 148 |
while (c.get() != 0) { } |
| 149 |
|
| 150 |
while (--i > 0) { |
| 151 |
q.poll(); |
| 152 |
} |
| 153 |
} |
| 154 |
}; |
| 155 |
|
| 156 |
r3.run(); |
| 157 |
|
| 158 |
t1.join(); |
| 159 |
t2.join(); |
| 160 |
} |
| 161 |
|
| 162 |
@Test |
| 163 |
public void spscLinkedArrayQueueNoNepotism() { |
| 164 |
SpscLinkedArrayQueue<Integer> q = new SpscLinkedArrayQueue<>(16); |
| 165 |
|
| 166 |
AtomicReferenceArray<Object> ara = q.producerBuffer; |
| 167 |
|
| 168 |
for (int i = 0; i < 20; i++) { |
| 169 |
q.offer(i); |
| 170 |
} |
| 171 |
|
| 172 |
assertNotNull(ara.get(16)); |
| 173 |
|
| 174 |
for (int i = 0; i < 20; i++) { |
| 175 |
assertEquals(i, q.poll().intValue()); |
| 176 |
} |
| 177 |
|
| 178 |
assertNull(ara.get(16)); |
| 179 |
} |
| 180 |
} |