49765
|
1 |
/*
|
|
2 |
* Copyright (c) 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 org.testng.annotations.AfterTest;
|
|
25 |
import org.testng.annotations.DataProvider;
|
|
26 |
|
|
27 |
import java.io.IOException;
|
|
28 |
import java.net.http.WebSocket;
|
|
29 |
import java.util.concurrent.Callable;
|
|
30 |
import java.util.concurrent.CompletionStage;
|
|
31 |
import java.util.function.BooleanSupplier;
|
|
32 |
|
|
33 |
/* Common infrastructure for tests that check pending operations */
|
|
34 |
public class PendingOperations {
|
|
35 |
|
|
36 |
static final Class<IllegalStateException> ISE = IllegalStateException.class;
|
|
37 |
static final Class<IOException> IOE = IOException.class;
|
|
38 |
// Time after which we deem that the local send buffer and remote
|
|
39 |
// receive buffer must be full. This has been heuristically determined.
|
|
40 |
// At the time of writing, using anything <= 5s on Mac will make the
|
|
41 |
// tests fail intermittently.
|
|
42 |
static final long MAX_WAIT_SEC = 10; // seconds.
|
|
43 |
|
|
44 |
DummyWebSocketServer server;
|
|
45 |
WebSocket webSocket;
|
|
46 |
|
|
47 |
@AfterTest
|
|
48 |
public void cleanup() {
|
|
49 |
server.close();
|
|
50 |
webSocket.abort();
|
|
51 |
}
|
|
52 |
|
|
53 |
/* shortcut */
|
|
54 |
static void assertHangs(CompletionStage<?> stage) {
|
|
55 |
Support.assertHangs(stage);
|
|
56 |
}
|
|
57 |
|
|
58 |
/* shortcut */
|
|
59 |
static void assertFails(Class<? extends Throwable> clazz,
|
|
60 |
CompletionStage<?> stage) {
|
|
61 |
Support.assertCompletesExceptionally(clazz, stage);
|
|
62 |
}
|
|
63 |
|
|
64 |
@DataProvider(name = "booleans")
|
|
65 |
public Object[][] booleans() {
|
|
66 |
return new Object[][]{{Boolean.TRUE}, {Boolean.FALSE}};
|
|
67 |
}
|
|
68 |
|
|
69 |
static boolean isMacOS() {
|
|
70 |
return System.getProperty("os.name").contains("OS X");
|
|
71 |
}
|
|
72 |
|
|
73 |
private static final int ITERATIONS = 3;
|
|
74 |
|
|
75 |
static void repeatable(Callable<Void> callable,
|
|
76 |
BooleanSupplier repeatCondition)
|
|
77 |
throws Exception
|
|
78 |
{
|
|
79 |
int iterations = 0;
|
|
80 |
do {
|
|
81 |
iterations++;
|
|
82 |
System.out.println("--- iteration " + iterations + " ---");
|
|
83 |
try {
|
|
84 |
callable.call();
|
|
85 |
break;
|
|
86 |
} catch (AssertionError e) {
|
|
87 |
if (isMacOS() && repeatCondition.getAsBoolean()) {
|
|
88 |
// ## This is loathsome, but necessary because of observed
|
|
89 |
// ## automagic socket buffer resizing on recent macOS platforms
|
|
90 |
continue;
|
|
91 |
} else {
|
|
92 |
throw e;
|
|
93 |
}
|
|
94 |
}
|
|
95 |
} while (iterations <= ITERATIONS);
|
|
96 |
}
|
|
97 |
}
|