author | dfuchs |
Tue, 01 Oct 2019 12:10:33 +0100 | |
changeset 58423 | 54de0c861d32 |
parent 55546 | 3ae57bbf9585 |
permissions | -rw-r--r-- |
55546
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
1 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
2 |
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
4 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
5 |
* This code is free software; you can redistribute it and/or modify it |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
7 |
* published by the Free Software Foundation. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
8 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
13 |
* accompanied this code). |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
14 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
15 |
* You should have received a copy of the GNU General Public License version |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
18 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
21 |
* questions. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
22 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
23 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
24 |
import java.io.IOException; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
25 |
import java.io.InputStream; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
26 |
import java.nio.ByteBuffer; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
27 |
import java.nio.charset.StandardCharsets; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
28 |
import java.security.SecureRandom; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
29 |
import java.util.ArrayList; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
30 |
import java.util.Arrays; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
31 |
import java.util.Iterator; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
32 |
import java.util.List; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
33 |
import java.util.Objects; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
34 |
import java.util.Random; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
35 |
import java.util.concurrent.Flow.Publisher; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
36 |
import java.util.concurrent.Flow.Subscriber; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
37 |
import java.util.concurrent.Flow.Subscription; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
38 |
import java.util.stream.Stream; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
39 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
40 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
41 |
* S for Support. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
42 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
43 |
* Auxiliary methods for tests that check conformance with reactive streams |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
44 |
* specification. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
45 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
46 |
* Short name is for the sake of convenience calling this class' static methods. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
47 |
* It could've been called Support or TckSupport, but then we would need to |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
48 |
* place this class in its own package so as to use "import static". |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
49 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
50 |
public class S { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
51 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
52 |
private static final Random RANDOM = new SecureRandom(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
53 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
54 |
private S() { } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
55 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
56 |
public static List<ByteBuffer> listOfBuffersFromBufferOfNBytes(int nBytes) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
57 |
return scatterBuffer(bufferOfNRandomBytes(nBytes)); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
58 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
59 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
60 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
61 |
* Spreads the remaining contents of the given byte buffer across a number |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
62 |
* of buffers put into a list. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
63 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
64 |
public static List<ByteBuffer> scatterBuffer(ByteBuffer src) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
65 |
List<ByteBuffer> buffers = new ArrayList<>(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
66 |
while (src.hasRemaining()) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
67 |
// We do not allow empty buffers ~~~~~~~~~~~~~~~~v |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
68 |
int capacity = RANDOM.nextInt(src.remaining()) + 1; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
69 |
ByteBuffer b = ByteBuffer.allocate(capacity); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
70 |
for (int i = 0; i < capacity; i++) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
71 |
b.put(src.get()); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
72 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
73 |
b.flip(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
74 |
buffers.add(b); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
75 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
76 |
return List.copyOf(buffers); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
77 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
78 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
79 |
public static ByteBuffer bufferOfNRandomBytes(int capacity) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
80 |
return ByteBuffer.wrap(arrayOfNRandomBytes(capacity)); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
81 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
82 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
83 |
public static byte[] arrayOfNRandomBytes(int nBytes) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
84 |
byte[] contents = new byte[nBytes]; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
85 |
RANDOM.nextBytes(contents); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
86 |
return contents; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
87 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
88 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
89 |
public static InputStream inputStreamOfNReads(long n) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
90 |
return new NReadsInputStream(n); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
91 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
92 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
93 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
94 |
* Convenience method for testing publishers. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
95 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
96 |
public static byte[] arrayOfNRandomBytes(long nBytes) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
97 |
return arrayOfNRandomBytes((int) nBytes); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
98 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
99 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
100 |
public static ByteBuffer bufferOfNRandomASCIIBytes(int capacity) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
101 |
String alphaNumeric = "abcdefghijklmnopqrstuvwxyz1234567890"; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
102 |
StringBuilder builder = new StringBuilder(capacity); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
103 |
for (int i = 0; i < capacity; i++) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
104 |
int idx = RANDOM.nextInt(alphaNumeric.length()); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
105 |
builder.append(alphaNumeric.charAt(idx)); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
106 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
107 |
return ByteBuffer.wrap(builder.toString().getBytes( |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
108 |
StandardCharsets.US_ASCII)); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
109 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
110 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
111 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
112 |
* Returns a simple non-compliant Subscriber. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
113 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
114 |
* This Subscriber is useful for testing our adaptors and wrappers, to make |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
115 |
* sure they do not delegate RS compliance to the underlying (and foreign to |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
116 |
* java.net.http codebase) Subscribers, but rather comply themselves. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
117 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
118 |
* Here's an example: |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
119 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
120 |
* public void onSubscribe(Subscription s) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
121 |
* delegate.onSubscribe(s); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
122 |
* } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
123 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
124 |
* The snippet above cannot be considered a good implementation of a |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
125 |
* Subscriber if `delegate` is an unknown Subscriber. In this case the |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
126 |
* implementation should independently check all the rules from the RS spec |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
127 |
* related to subscribers. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
128 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
129 |
public static <T> Subscriber<T> nonCompliantSubscriber() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
130 |
return new Subscriber<>() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
131 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
132 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
133 |
public void onSubscribe(Subscription subscription) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
134 |
subscription.request(Long.MAX_VALUE); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
135 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
136 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
137 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
138 |
public void onNext(T item) { } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
139 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
140 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
141 |
public void onError(Throwable throwable) { } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
142 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
143 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
144 |
public void onComplete() { } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
145 |
}; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
146 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
147 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
148 |
public static int randomIntUpTo(int bound) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
149 |
return RANDOM.nextInt(bound); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
150 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
151 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
152 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
153 |
* Signals an error to its subscribers immediately after subscription. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
154 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
155 |
public static <T> Publisher<T> newErroredPublisher() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
156 |
return subscriber -> { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
157 |
subscriber.onSubscribe(new Subscription() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
158 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
159 |
public void request(long n) { } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
160 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
161 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
162 |
public void cancel() { } |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
163 |
}); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
164 |
subscriber.onError(new IOException()); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
165 |
}; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
166 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
167 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
168 |
/* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
169 |
* Publishes the elements obtained from the stream and signals completion. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
170 |
* Can be cancelled, but cannot signal an error. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
171 |
* |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
172 |
* This trivial ad-hoc implementation of Publisher was created so as to |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
173 |
* publish lists of byte buffers. We can publish ByteBuffer, but we can't |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
174 |
* seem to publish List<ByteBuffer> since there's no readily available |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
175 |
* publisher of those, nor there's a simple adaptor. |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
176 |
*/ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
177 |
public static <T> Publisher<T> publisherOfStream(Stream<? extends T> stream) |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
178 |
{ |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
179 |
if (stream == null) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
180 |
throw new NullPointerException(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
181 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
182 |
return new Publisher<T>() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
183 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
184 |
public void subscribe(Subscriber<? super T> subscriber) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
185 |
if (subscriber == null) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
186 |
throw new NullPointerException(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
187 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
188 |
Subscription subscription = new Subscription() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
189 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
190 |
boolean inOnNext; // recursion control |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
191 |
volatile boolean cancelled; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
192 |
long demand; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
193 |
final Iterator<? extends T> supply = stream.iterator(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
194 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
195 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
196 |
public void request(long n) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
197 |
demand = demand + n < 0 ? Long.MAX_VALUE : demand + n; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
198 |
if (inOnNext) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
199 |
return; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
200 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
201 |
if (cancelled) |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
202 |
return; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
203 |
if (n <= 0) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
204 |
cancelled = true; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
205 |
subscriber.onError(new IllegalArgumentException( |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
206 |
"non-positive subscription request")); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
207 |
return; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
208 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
209 |
while (supply.hasNext() && demand > 0 && !cancelled) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
210 |
demand--; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
211 |
inOnNext = true; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
212 |
try { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
213 |
T item = supply.next(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
214 |
subscriber.onNext(item); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
215 |
} finally { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
216 |
inOnNext = false; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
217 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
218 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
219 |
if (!supply.hasNext()) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
220 |
cancelled = true; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
221 |
subscriber.onComplete(); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
222 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
223 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
224 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
225 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
226 |
public void cancel() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
227 |
cancelled = true; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
228 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
229 |
}; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
230 |
subscriber.onSubscribe(subscription); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
231 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
232 |
}; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
233 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
234 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
235 |
static final class NReadsInputStream extends InputStream { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
236 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
237 |
private static final int EOF = -1; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
238 |
private long readsLeft; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
239 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
240 |
NReadsInputStream(long n) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
241 |
if (n < 0) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
242 |
throw new IllegalArgumentException(String.valueOf(n)); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
243 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
244 |
this.readsLeft = n; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
245 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
246 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
247 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
248 |
public int read() { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
249 |
if (readsLeft == 0L) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
250 |
return EOF; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
251 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
252 |
readsLeft--; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
253 |
return S.randomIntUpTo(256); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
254 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
255 |
|
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
256 |
@Override |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
257 |
public int read(byte[] b, int off, int len) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
258 |
Objects.checkFromIndexSize(off, len, b.length); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
259 |
// Must return 0 if len == 0, |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
260 |
// even if there are no more reads left |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
261 |
if (len == 0) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
262 |
return 0; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
263 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
264 |
if (readsLeft == 0L) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
265 |
return EOF; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
266 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
267 |
readsLeft--; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
268 |
// At least one byte MUST be read, but we can read |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
269 |
// less than `len` bytes |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
270 |
int r = RANDOM.nextInt(len) + 1; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
271 |
for (int i = 0; i < r; i++) { |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
272 |
b[i] = (byte) randomIntUpTo(256); |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
273 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
274 |
return r; |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
275 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
276 |
} |
3ae57bbf9585
8226602: Test convenience reactive primitives from java.net.http with RS TCK
prappo
parents:
diff
changeset
|
277 |
} |