Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: attempt to make Vite websocket tests more reliable (#21078) (CP: 24.7) #21088

Merged
merged 2 commits into from
Mar 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,12 @@ public void tearDown() throws Exception {
}
}

@Test(timeout = 5000)
@Test(timeout = 7000)
public void waitForConnection_clientWebsocketAvailable_blocksUntilConnectionIsEstablished()
throws ExecutionException, InterruptedException {
CountDownLatch connectionLatch = new CountDownLatch(1);
CountDownLatch closeLatch = new CountDownLatch(1);
AtomicReference<Throwable> error = new AtomicReference<>();
handlerSupplier = (exchange) -> {
// Simulate connection delay
Thread.sleep(500);
Expand All @@ -78,27 +79,34 @@ public void waitForConnection_clientWebsocketAvailable_blocksUntilConnectionIsEs
long startTime = System.nanoTime();
ViteWebsocketConnection viteConnection = new ViteWebsocketConnection(
httpServer.getAddress().getPort(), "/VAADIN", "proto", x -> {
}, () -> {
closeLatch.countDown();
}, err -> {
}, closeLatch::countDown, err -> {
error.set(err);
connectionLatch.countDown();
}) {
@Override
public void onOpen(WebSocket webSocket) {
super.onOpen(webSocket);
connectionLatch.countDown();
}
};
connectionLatch.await(2, TimeUnit.SECONDS);
boolean established = connectionLatch.await(5, TimeUnit.SECONDS);
long elapsedTime = Duration.ofNanos(System.nanoTime() - startTime)
.toMillis();
if (error.get() != null) {
throw new AssertionError(
"Websocket connection failed: " + error.get().getMessage(),
error.get());
}
Assert.assertTrue("Connection NOT established. Elapsed time "
+ elapsedTime + " ms", established);
Assert.assertTrue(
"Should have waited for connection to be established (elapsed time: "
+ elapsedTime + ")",
elapsedTime > 500);
Assert.assertTrue(
"Should not have been blocked too long after connection (elapsed time: "
+ elapsedTime + ")",
elapsedTime < 1000);
elapsedTime < 2500);
if (!closeLatch.await(500, TimeUnit.MILLISECONDS)) {
viteConnection.close();
closeLatch.await(500, TimeUnit.MILLISECONDS);
Expand All @@ -115,10 +123,13 @@ public void waitForConnection_clientWebsocketNotAvailable_fails()
"/VAADIN", "proto", x -> {
}, () -> {
}, err -> errorLatch.countDown());
errorLatch.await(2, TimeUnit.SECONDS);
if (!errorLatch.await(5, TimeUnit.SECONDS)) {
Assert.fail(
"Expecting connection failure, but not happened in 5 seconds");
}
}

@Test(timeout = 2000)
@Test(timeout = 5000)
public void close_clientWebsocketNotAvailable_dontBlock()
throws ExecutionException, InterruptedException {
AtomicReference<Throwable> connectionError = new AtomicReference<>();
Expand All @@ -136,7 +147,7 @@ public void close_clientWebsocketNotAvailable_dontBlock()
}

@SuppressWarnings("unchecked")
@Test(timeout = 2000)
@Test(timeout = 3500)
public void close_clientWebsocketClose_dontBlockIndefinitely()
throws ExecutionException, InterruptedException,
NoSuchFieldException, InvocationTargetException,
Expand Down