| 1 |
/************************************************************************ |
| 2 |
* Licensed under Public Domain (CC0) * |
| 3 |
* * |
| 4 |
* To the extent possible under law, the person who associated CC0 with * |
| 5 |
* this code has waived all copyright and related or neighboring * |
| 6 |
* rights to this code. * |
| 7 |
* * |
| 8 |
* You should have received a copy of the CC0 legalcode along with this * |
| 9 |
* work. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.* |
| 10 |
************************************************************************/ |
| 11 |
|
| 12 |
package org.reactivestreams.tck.flow; |
| 13 |
|
| 14 |
import java.util.Map; |
| 15 |
import java.util.concurrent.ConcurrentHashMap; |
| 16 |
import java.util.concurrent.Flow; |
| 17 |
import java.util.concurrent.atomic.*; |
| 18 |
|
| 19 |
import org.reactivestreams.tck.TestEnvironment; |
| 20 |
import org.testng.annotations.*; |
| 21 |
|
| 22 |
@Test |
| 23 |
public class RangeFlowPublisherTest extends FlowPublisherVerification<Integer> { |
| 24 |
|
| 25 |
static final Map<Integer, StackTraceElement[]> stacks = new ConcurrentHashMap<Integer, StackTraceElement[]>(); |
| 26 |
|
| 27 |
static final Map<Integer, Boolean> states = new ConcurrentHashMap<Integer, Boolean>(); |
| 28 |
|
| 29 |
static final AtomicInteger id = new AtomicInteger(); |
| 30 |
|
| 31 |
@AfterClass |
| 32 |
public static void afterClass() { |
| 33 |
boolean fail = false; |
| 34 |
StringBuilder b = new StringBuilder(); |
| 35 |
for (Map.Entry<Integer, Boolean> t : states.entrySet()) { |
| 36 |
if (!t.getValue()) { |
| 37 |
b.append("\r\n-------------------------------"); |
| 38 |
for (Object o : stacks.get(t.getKey())) { |
| 39 |
b.append("\r\nat ").append(o); |
| 40 |
} |
| 41 |
fail = true; |
| 42 |
} |
| 43 |
} |
| 44 |
if (fail) { |
| 45 |
throw new AssertionError("Cancellations were missing:" + b); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
public RangeFlowPublisherTest() { |
| 50 |
super(new TestEnvironment()); |
| 51 |
} |
| 52 |
|
| 53 |
@Override |
| 54 |
public Flow.Publisher<Integer> createFlowPublisher(long elements) { |
| 55 |
return new RangeFlowPublisher(1, elements); |
| 56 |
} |
| 57 |
|
| 58 |
@Override |
| 59 |
public Flow.Publisher<Integer> createFailedFlowPublisher() { |
| 60 |
return null; |
| 61 |
} |
| 62 |
|
| 63 |
static final class RangeFlowPublisher |
| 64 |
implements Flow.Publisher<Integer> { |
| 65 |
|
| 66 |
final StackTraceElement[] stacktrace; |
| 67 |
|
| 68 |
final long start; |
| 69 |
|
| 70 |
final long count; |
| 71 |
|
| 72 |
RangeFlowPublisher(long start, long count) { |
| 73 |
this.stacktrace = Thread.currentThread().getStackTrace(); |
| 74 |
this.start = start; |
| 75 |
this.count = count; |
| 76 |
} |
| 77 |
|
| 78 |
@Override |
| 79 |
public void subscribe(Flow.Subscriber<? super Integer> s) { |
| 80 |
if (s == null) { |
| 81 |
throw new NullPointerException(); |
| 82 |
} |
| 83 |
|
| 84 |
int ids = id.incrementAndGet(); |
| 85 |
|
| 86 |
RangeFlowSubscription parent = new RangeFlowSubscription(s, ids, start, start + count); |
| 87 |
stacks.put(ids, stacktrace); |
| 88 |
states.put(ids, false); |
| 89 |
s.onSubscribe(parent); |
| 90 |
} |
| 91 |
|
| 92 |
static final class RangeFlowSubscription extends AtomicLong implements Flow.Subscription { |
| 93 |
|
| 94 |
private static final long serialVersionUID = 9066221863682220604L; |
| 95 |
|
| 96 |
final Flow.Subscriber<? super Integer> actual; |
| 97 |
|
| 98 |
final int ids; |
| 99 |
|
| 100 |
final long end; |
| 101 |
|
| 102 |
long index; |
| 103 |
|
| 104 |
volatile boolean cancelled; |
| 105 |
|
| 106 |
RangeFlowSubscription(Flow.Subscriber<? super Integer> actual, int ids, long start, long end) { |
| 107 |
this.actual = actual; |
| 108 |
this.ids = ids; |
| 109 |
this.index = start; |
| 110 |
this.end = end; |
| 111 |
} |
| 112 |
|
| 113 |
@Override |
| 114 |
public void request(long n) { |
| 115 |
if (!cancelled) { |
| 116 |
if (n <= 0L) { |
| 117 |
cancelled = true; |
| 118 |
states.put(ids, true); |
| 119 |
actual.onError(new IllegalArgumentException("ยง3.9 violated")); |
| 120 |
return; |
| 121 |
} |
| 122 |
|
| 123 |
for (;;) { |
| 124 |
long r = get(); |
| 125 |
long u = r + n; |
| 126 |
if (u < 0L) { |
| 127 |
u = Long.MAX_VALUE; |
| 128 |
} |
| 129 |
if (compareAndSet(r, u)) { |
| 130 |
if (r == 0) { |
| 131 |
break; |
| 132 |
} |
| 133 |
return; |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
long idx = index; |
| 138 |
long f = end; |
| 139 |
|
| 140 |
for (;;) { |
| 141 |
long e = 0; |
| 142 |
while (e != n && idx != f) { |
| 143 |
if (cancelled) { |
| 144 |
return; |
| 145 |
} |
| 146 |
|
| 147 |
actual.onNext((int)idx); |
| 148 |
|
| 149 |
idx++; |
| 150 |
e++; |
| 151 |
} |
| 152 |
|
| 153 |
if (idx == f) { |
| 154 |
if (!cancelled) { |
| 155 |
states.put(ids, true); |
| 156 |
actual.onComplete(); |
| 157 |
} |
| 158 |
return; |
| 159 |
} |
| 160 |
|
| 161 |
index = idx; |
| 162 |
n = addAndGet(-n); |
| 163 |
if (n == 0) { |
| 164 |
break; |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
@Override |
| 171 |
public void cancel() { |
| 172 |
cancelled = true; |
| 173 |
states.put(ids, true); |
| 174 |
} |
| 175 |
} |
| 176 |
} |
| 177 |
} |