Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/BufferingSubscriberErrorCompleteTest.java
41152 views
1
/*
2
* Copyright (c) 2017, 2018, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.nio.ByteBuffer;
25
import java.util.ArrayList;
26
import java.util.List;
27
import java.util.concurrent.CompletionStage;
28
import java.util.concurrent.ExecutorService;
29
import java.util.concurrent.Executors;
30
import java.util.concurrent.Flow.Subscription;
31
import java.util.concurrent.Phaser;
32
import java.util.concurrent.SubmissionPublisher;
33
import java.util.stream.IntStream;
34
import java.net.http.HttpResponse.BodySubscriber;
35
import org.testng.annotations.DataProvider;
36
import org.testng.annotations.Test;
37
import static java.lang.Long.MAX_VALUE;
38
import static java.lang.Long.MIN_VALUE;
39
import static java.nio.ByteBuffer.wrap;
40
import static java.net.http.HttpResponse.BodySubscribers.buffering;
41
import static org.testng.Assert.*;
42
43
/*
44
* @test
45
* @summary Test for HttpResponse.BodySubscriber.buffering() onError/onComplete
46
* @run testng/othervm BufferingSubscriberErrorCompleteTest
47
*/
48
49
public class BufferingSubscriberErrorCompleteTest {
50
51
@DataProvider(name = "illegalDemand")
52
public Object[][] illegalDemand() {
53
return new Object[][]{
54
{0L}, {-1L}, {-5L}, {-100L}, {-101L}, {-100_001L}, {MIN_VALUE}
55
};
56
}
57
58
@Test(dataProvider = "illegalDemand")
59
public void illegalRequest(long demand) throws Exception {
60
ExecutorService executor = Executors.newFixedThreadPool(1);
61
SubmissionPublisher<List<ByteBuffer>> publisher =
62
new SubmissionPublisher<>(executor, 1);
63
64
Phaser gate = new Phaser(2); // single onSubscribe and onError
65
ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
66
BodySubscriber subscriber = buffering(exposingSubscriber, 1);
67
publisher.subscribe(subscriber);
68
gate.arriveAndAwaitAdvance();
69
70
Subscription s = exposingSubscriber.subscription;
71
int previous = exposingSubscriber.onErrorInvocations;
72
s.request(demand);
73
gate.arriveAndAwaitAdvance();
74
75
assertEquals(previous + 1, exposingSubscriber.onErrorInvocations);
76
assertTrue(exposingSubscriber.throwable instanceof IllegalArgumentException,
77
"Expected IAE, got:" + exposingSubscriber.throwable);
78
79
furtherCancelsRequestsShouldBeNoOp(s);
80
assertEquals(exposingSubscriber.onErrorInvocations, 1);
81
executor.shutdown();
82
}
83
84
85
@DataProvider(name = "bufferAndItemSizes")
86
public Object[][] bufferAndItemSizes() {
87
List<Object[]> values = new ArrayList<>();
88
89
for (int bufferSize : new int[] { 1, 5, 10, 100, 1000 })
90
for (int items : new int[] { 0, 1, 2, 5, 9, 10, 11, 15, 29, 99 })
91
values.add(new Object[] { bufferSize, items });
92
93
return values.stream().toArray(Object[][]::new);
94
}
95
96
@Test(dataProvider = "bufferAndItemSizes")
97
public void onErrorFromPublisher(int bufferSize,
98
int numberOfItems)
99
throws Exception
100
{
101
ExecutorService executor = Executors.newFixedThreadPool(1);
102
SubmissionPublisher<List<ByteBuffer>> publisher =
103
new SubmissionPublisher<>(executor, 1);
104
105
// onSubscribe + onError + this thread
106
Phaser gate = new Phaser(3);
107
ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
108
BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
109
publisher.subscribe(subscriber);
110
111
List<ByteBuffer> item = List.of(wrap(new byte[] { 1 }));
112
IntStream.range(0, numberOfItems).forEach(x -> publisher.submit(item));
113
Throwable t = new Throwable("a message from me to me");
114
publisher.closeExceptionally(t);
115
116
gate.arriveAndAwaitAdvance();
117
118
Subscription s = exposingSubscriber.subscription;
119
120
assertEquals(exposingSubscriber.onErrorInvocations, 1);
121
assertEquals(exposingSubscriber.onCompleteInvocations, 0);
122
assertEquals(exposingSubscriber.throwable, t);
123
assertEquals(exposingSubscriber.throwable.getMessage(),
124
"a message from me to me");
125
126
furtherCancelsRequestsShouldBeNoOp(s);
127
assertEquals(exposingSubscriber.onErrorInvocations, 1);
128
assertEquals(exposingSubscriber.onCompleteInvocations, 0);
129
executor.shutdown();
130
}
131
132
@Test(dataProvider = "bufferAndItemSizes")
133
public void onCompleteFromPublisher(int bufferSize,
134
int numberOfItems)
135
throws Exception
136
{
137
ExecutorService executor = Executors.newFixedThreadPool(1);
138
SubmissionPublisher<List<ByteBuffer>> publisher =
139
new SubmissionPublisher<>(executor, 1);
140
141
// onSubscribe + onComplete + this thread
142
Phaser gate = new Phaser(3);
143
ExposingSubscriber exposingSubscriber = new ExposingSubscriber(gate);
144
BodySubscriber subscriber = buffering(exposingSubscriber, bufferSize);
145
publisher.subscribe(subscriber);
146
147
List<ByteBuffer> item = List.of(wrap(new byte[] { 1 }));
148
IntStream.range(0, numberOfItems).forEach(x -> publisher.submit(item));
149
publisher.close();
150
151
gate.arriveAndAwaitAdvance();
152
153
Subscription s = exposingSubscriber.subscription;
154
155
assertEquals(exposingSubscriber.onErrorInvocations, 0);
156
assertEquals(exposingSubscriber.onCompleteInvocations, 1);
157
assertEquals(exposingSubscriber.throwable, null);
158
159
furtherCancelsRequestsShouldBeNoOp(s);
160
assertEquals(exposingSubscriber.onErrorInvocations, 0);
161
assertEquals(exposingSubscriber.onCompleteInvocations, 1);
162
assertEquals(exposingSubscriber.throwable, null);
163
executor.shutdown();
164
}
165
166
static class ExposingSubscriber implements BodySubscriber<Void> {
167
final Phaser gate;
168
volatile Subscription subscription;
169
volatile int onNextInvocations;
170
volatile int onErrorInvocations;
171
volatile int onCompleteInvocations;
172
volatile Throwable throwable;
173
174
ExposingSubscriber(Phaser gate) {
175
this.gate = gate;
176
}
177
178
@Override
179
public void onSubscribe(Subscription subscription) {
180
//out.println("onSubscribe " + subscription);
181
this.subscription = subscription;
182
subscription.request(MAX_VALUE);
183
gate.arrive();
184
}
185
186
@Override
187
public void onNext(List<ByteBuffer> item) {
188
//out.println("onNext " + item);
189
onNextInvocations++;
190
}
191
192
@Override
193
public void onError(Throwable throwable) {
194
//out.println("onError " + throwable);
195
this.throwable = throwable;
196
onErrorInvocations++;
197
gate.arrive();
198
}
199
200
@Override
201
public void onComplete() {
202
//out.println("onComplete ");
203
onCompleteInvocations++;
204
gate.arrive();
205
}
206
207
@Override
208
public CompletionStage<Void> getBody() {
209
throw new UnsupportedOperationException("getBody is unsupported");
210
}
211
}
212
213
static void furtherCancelsRequestsShouldBeNoOp(Subscription s) {
214
s.cancel(); s.request(1);
215
s.cancel(); s.request(100); s.cancel();
216
s.cancel(); s.request(MAX_VALUE); s.cancel(); s.cancel();
217
s.cancel(); s.cancel(); s.cancel(); s.cancel();
218
s.request(MAX_VALUE); s.request(MAX_VALUE); s.request(MAX_VALUE);
219
s.request(-1); s.request(-100); s.request(MIN_VALUE);
220
}
221
}
222
223