From a6df220765bade15b005ba1d42bb1800b18ee759 Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Thu, 19 Nov 2026 07:59:11 -0800 Subject: [PATCH 1/3] Add RequestCancellationHelper and a samples query schema constant --- .../test/util/RequestCancellationHelper.java | 87 +++++++++++++++++++ .../test/util/exp/SampleTypeAPIHelper.java | 2 + 2 files changed, 89 insertions(+) create mode 100644 src/org/labkey/test/util/RequestCancellationHelper.java diff --git a/src/org/labkey/test/util/RequestCancellationHelper.java b/src/org/labkey/test/util/RequestCancellationHelper.java new file mode 100644 index 0000000000..81d8821de3 --- /dev/null +++ b/src/org/labkey/test/util/RequestCancellationHelper.java @@ -0,0 +1,87 @@ +/* + * Copyright (c) 2026 LabKey Corporation + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.labkey.test.util; + +import org.labkey.remoteapi.CommandException; +import org.labkey.remoteapi.query.ExecuteSqlCommand; +import org.labkey.test.WebDriverWrapper; +import org.labkey.test.util.core.admin.ServerUsageUtils; + +import java.io.IOException; +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +/** Helpers for tests that abandon slow requests and check that the server cancels them */ +public class RequestCancellationHelper +{ + /** The usage report gathers every module's metrics, which can take over a minute on a large database */ + public static final int METRICS_TIMEOUT = 180_000; + public static final String WS_MESSAGE = "wsMessage"; + public static final String WS_CLOSE = "wsClose"; + + private final WebDriverWrapper _test; + + public RequestCancellationHelper(WebDriverWrapper test) + { + _test = test; + } + + public void waitForWebSocket() + { + // The server ignores any message type but "cancel" + WebDriverWrapper.waitFor(() -> (Boolean) _test.executeScript("return LABKEY.WebSocket.send({ type: 'ping' });"), + "The notification WebSocket didn't open", WebDriverWrapper.WAIT_FOR_JAVASCRIPT); + } + + /** Server-wide count of requests canceled from one source (WS_MESSAGE, WS_CLOSE), from Core's usage metrics */ + @SuppressWarnings("unchecked") + public long getCancelCount(String metric) + { + try + { + Map core = ServerUsageUtils.getModuleMetrics(_test.createDefaultConnection().setTimeout(METRICS_TIMEOUT), "Core"); + Map counts = (Map) core.getOrDefault("simpleMetricCounts", Map.of()); + Map requestCancel = (Map) counts.getOrDefault("requestCancel", Map.of()); + return ((Number) requestCancel.getOrDefault(metric, 0)).longValue(); + } + catch (IOException | CommandException e) + { + throw new RuntimeException(e); + } + } + + /** At least, since the counter is server-wide and other cancels can land meanwhile */ + public void waitForCancelCount(String metric, long minimum) + { + WebDriverWrapper.waitFor(() -> getCancelCount(metric) >= minimum, "Requests canceled (" + metric + ") never reached " + minimum, METRICS_TIMEOUT); + } + + /** Names of the server's modules, sorted; the basis for slow cross joins whose size doesn't depend on the module count */ + public List getModuleNames(String containerPath) throws IOException, CommandException + { + return new ExecuteSqlCommand("core", "SELECT Name FROM core.Modules ORDER BY Name") + .execute(_test.createDefaultConnection(), containerPath) + .getRows().stream().map(row -> (String) row.get("Name")).toList(); + } + + /** "core.Modules m0, core.Modules m1, ..." with {@code joins} tables */ + public static String crossJoinModules(int joins) + { + return IntStream.range(0, joins).mapToObj(i -> "core.Modules m" + i).collect(Collectors.joining(", ")); + } +} diff --git a/src/org/labkey/test/util/exp/SampleTypeAPIHelper.java b/src/org/labkey/test/util/exp/SampleTypeAPIHelper.java index e008169a76..f153eb3947 100644 --- a/src/org/labkey/test/util/exp/SampleTypeAPIHelper.java +++ b/src/org/labkey/test/util/exp/SampleTypeAPIHelper.java @@ -46,6 +46,8 @@ public class SampleTypeAPIHelper { public static final String SCHEMA_NAME = "exp.materials"; + /** Query schema holding one table per sample type; SCHEMA_NAME is the domain schema */ + public static final String SAMPLES_SCHEMA_NAME = "samples"; // Global constants to ease migration from "Sample Set" to "Sample Type" public static final String SAMPLE_TYPE_DOMAIN_KIND = DomainKind.SampleSet.name(); From 330eca2607a4c37e2fda52067a45fa4a76e6990e Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Sun, 22 Nov 2026 17:25:01 -0800 Subject: [PATCH 2/3] Make RequestCancellationHelper polling resilient and use JUnit's ThrowingRunnable --- .../test/tests/SiteSettingsApiTest.java | 9 +--- .../test/util/RequestCancellationHelper.java | 48 ++++++++++++++----- 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/org/labkey/test/tests/SiteSettingsApiTest.java b/src/org/labkey/test/tests/SiteSettingsApiTest.java index e87dadc176..4404740f02 100644 --- a/src/org/labkey/test/tests/SiteSettingsApiTest.java +++ b/src/org/labkey/test/tests/SiteSettingsApiTest.java @@ -19,6 +19,7 @@ import org.junit.BeforeClass; import org.junit.Test; import org.junit.experimental.categories.Category; +import org.junit.function.ThrowingRunnable; import org.labkey.remoteapi.CommandException; import org.labkey.remoteapi.Connection; import org.labkey.test.BaseWebDriverTest; @@ -141,17 +142,11 @@ private Supplier impersonating(String email) throws IOException, Com private void assertForbidden(ThrowingRunnable call) { - Throwable thrown = assertThrows(Exception.class, call::run); + Throwable thrown = assertThrows(Exception.class, call); CommandException e = thrown instanceof CommandException ce ? ce : (CommandException) thrown.getCause(); assertEquals("Status for unauthorized request", 403, e.getStatusCode()); } - @FunctionalInterface - private interface ThrowingRunnable - { - void run() throws Exception; - } - @Test public void testPageSaveUsesApi() { diff --git a/src/org/labkey/test/util/RequestCancellationHelper.java b/src/org/labkey/test/util/RequestCancellationHelper.java index 81d8821de3..ba765f8bcb 100644 --- a/src/org/labkey/test/util/RequestCancellationHelper.java +++ b/src/org/labkey/test/util/RequestCancellationHelper.java @@ -15,6 +15,7 @@ */ package org.labkey.test.util; +import org.jetbrains.annotations.NotNull; import org.labkey.remoteapi.CommandException; import org.labkey.remoteapi.query.ExecuteSqlCommand; import org.labkey.test.WebDriverWrapper; @@ -33,6 +34,7 @@ public class RequestCancellationHelper public static final int METRICS_TIMEOUT = 180_000; public static final String WS_MESSAGE = "wsMessage"; public static final String WS_CLOSE = "wsClose"; + private static final int CANCEL_COUNT_POLL_INTERVAL = 500; private final WebDriverWrapper _test; @@ -44,20 +46,18 @@ public RequestCancellationHelper(WebDriverWrapper test) public void waitForWebSocket() { // The server ignores any message type but "cancel" - WebDriverWrapper.waitFor(() -> (Boolean) _test.executeScript("return LABKEY.WebSocket.send({ type: 'ping' });"), - "The notification WebSocket didn't open", WebDriverWrapper.WAIT_FOR_JAVASCRIPT); + WebDriverWrapper.waitFor(() -> (Boolean) _test.executeScript("return !!(window.LABKEY && LABKEY.WebSocket && LABKEY.WebSocket.send({ type: 'ping' }));"), + () -> "The notification WebSocket didn't open; it opens only for a signed-in user with the cancelAbandonedRequests experimental feature on (on this page: " + + _test.executeScript("return !!(window.LABKEY && LABKEY.experimental && LABKEY.experimental.cancelAbandonedRequests);") + ")", + WebDriverWrapper.WAIT_FOR_JAVASCRIPT); } /** Server-wide count of requests canceled from one source (WS_MESSAGE, WS_CLOSE), from Core's usage metrics */ - @SuppressWarnings("unchecked") - public long getCancelCount(String metric) + public long getCancelCount(@NotNull String metric) { try { - Map core = ServerUsageUtils.getModuleMetrics(_test.createDefaultConnection().setTimeout(METRICS_TIMEOUT), "Core"); - Map counts = (Map) core.getOrDefault("simpleMetricCounts", Map.of()); - Map requestCancel = (Map) counts.getOrDefault("requestCancel", Map.of()); - return ((Number) requestCancel.getOrDefault(metric, 0)).longValue(); + return fetchCancelCount(metric); } catch (IOException | CommandException e) { @@ -65,14 +65,36 @@ public long getCancelCount(String metric) } } + @SuppressWarnings("unchecked") + private long fetchCancelCount(String metric) throws IOException, CommandException + { + Map core = ServerUsageUtils.getModuleMetrics(_test.createDefaultConnection().setTimeout(METRICS_TIMEOUT), "Core"); + Map counts = (Map) core.getOrDefault("simpleMetricCounts", Map.of()); + Map requestCancel = (Map) counts.getOrDefault("requestCancel", Map.of()); + return ((Number) requestCancel.getOrDefault(metric, 0)).longValue(); + } + /** At least, since the counter is server-wide and other cancels can land meanwhile */ - public void waitForCancelCount(String metric, long minimum) + public void waitForCancelCount(@NotNull String metric, long minimum) { - WebDriverWrapper.waitFor(() -> getCancelCount(metric) >= minimum, "Requests canceled (" + metric + ") never reached " + minimum, METRICS_TIMEOUT); + // Each poll gathers every module's metrics, so poll slowly, retry failed polls, and outlast a poll that takes the full METRICS_TIMEOUT + WebDriverWrapper.waitFor(() -> { + try + { + if (fetchCancelCount(metric) >= minimum) + return true; + } + catch (IOException | CommandException e) + { + TestLogger.log("Retrying after failing to read cancel count: " + e.getMessage()); + } + WebDriverWrapper.sleep(CANCEL_COUNT_POLL_INTERVAL); + return false; + }, "Requests canceled (" + metric + ") never reached " + minimum, 3 * METRICS_TIMEOUT); } /** Names of the server's modules, sorted; the basis for slow cross joins whose size doesn't depend on the module count */ - public List getModuleNames(String containerPath) throws IOException, CommandException + public @NotNull List getModuleNames(@NotNull String containerPath) throws IOException, CommandException { return new ExecuteSqlCommand("core", "SELECT Name FROM core.Modules ORDER BY Name") .execute(_test.createDefaultConnection(), containerPath) @@ -80,8 +102,10 @@ public List getModuleNames(String containerPath) throws IOException, Com } /** "core.Modules m0, core.Modules m1, ..." with {@code joins} tables */ - public static String crossJoinModules(int joins) + public static @NotNull String crossJoinModules(int joins) { + if (joins < 1) + throw new IllegalArgumentException("joins must be at least 1: " + joins); return IntStream.range(0, joins).mapToObj(i -> "core.Modules m" + i).collect(Collectors.joining(", ")); } } From 7797fc0a175cd46a521cb68afb9ea0a6ef6247de Mon Sep 17 00:00:00 2001 From: labkey-jeckels Date: Tue, 24 Nov 2026 09:27:08 -0800 Subject: [PATCH 3/3] Code cleanup; remove unneeded Junit actions --- src/org/labkey/test/tests/list/ListTest.java | 32 +++++++++----------- 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/src/org/labkey/test/tests/list/ListTest.java b/src/org/labkey/test/tests/list/ListTest.java index b45875bd39..e08101ed2e 100644 --- a/src/org/labkey/test/tests/list/ListTest.java +++ b/src/org/labkey/test/tests/list/ListTest.java @@ -81,6 +81,7 @@ import java.io.File; import java.io.IOException; +import java.net.URI; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -93,6 +94,7 @@ import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; import static org.labkey.test.params.FieldDefinition.ColumnType; import static org.labkey.test.params.FieldDefinition.DOMAIN_TRICKY_CHARACTERS; @@ -2244,7 +2246,7 @@ FieldDefinition colURL(String name, ColumnType type, String url) List Acolumns = Arrays.asList( col("A", ColumnType.Integer), - colURL("title", ColumnType.String, "/junit/echoForm.view?key=${A}&title=${title}&table=A"), + colURL("title", ColumnType.String, "/project/begin.view?key=${A}&title=${title}&table=A"), col("Bfk", "B") ); String[][] Adata = new String[][] @@ -2254,7 +2256,7 @@ FieldDefinition colURL(String name, ColumnType type, String url) List Bcolumns = Arrays.asList( col("B", ColumnType.Integer), - colURL("title", ColumnType.String, "org.labkey.core.junit.JunitController$EchoFormAction.class?key=${B}&title=${title}&table=B"), + colURL("title", ColumnType.String, "org.labkey.core.portal.ProjectController$BeginAction.class?key=${B}&title=${title}&table=B"), col("Cfk", "C") ); String[][] Bdata = new String[][] @@ -2264,7 +2266,7 @@ FieldDefinition colURL(String name, ColumnType type, String url) List Ccolumns = Arrays.asList( col("C", ColumnType.Integer), - colURL("title", ColumnType.String, "/junit/echoForm.view?key=${C}&title=${title}&table=C") + colURL("title", ColumnType.String, "/project/begin.view?key=${C}&title=${title}&table=C") ); String[][] Cdata = new String[][] { @@ -2370,9 +2372,13 @@ private void setListImportAsTestDataField(String data, String... expectedErrors) } - Locator inputWithValue(String name, String value) + private void assertTitleUrl(Locator.XPathLocator link, String table, String title) { - return Locator.xpath("//input[@name='" + name + "' and @value='" + value + "']"); + String hrefValue = link.findElement(getDriver()).getDomProperty("href"); + assertNotNull("Link has no href: " + link, hrefValue); + URI href = URI.create(hrefValue); + assertTrue("Wrong URL target: " + href, href.getRawPath().endsWith(WebTestHelper.buildRelativeUrl("project", PROJECT_VERIFY, "begin"))); + assertEquals("Wrong URL parameters: " + href, Map.of("key", "1", "title", title, "table", table), WebTestHelper.parseUrlQueryString(href.getRawQuery())); } @LogMethod @@ -2387,14 +2393,7 @@ public void customizeURLTest() beginAt(WebTestHelper.buildURL("query", PROJECT_VERIFY, "executeQuery", Map.of("schemaName", "lists", "query.queryName", "A"))); - pushLocation(); - { - clickAndWait(Locator.linkWithText("one A")); - assertElementPresent(inputWithValue("table","A")); - assertElementPresent(inputWithValue("title","one A")); - assertElementPresent(inputWithValue("key","1")); - } - popLocation(); + assertTitleUrl(Locator.linkWithText("one A"), "A", "one A"); pushLocation(); { @@ -2413,11 +2412,8 @@ public void customizeURLTest() _customizeViewsHelper.addColumn("Bfk/Cfk/title"); _customizeViewsHelper.saveCustomView("allColumns"); - clickAndWait(Locator.linkWithText("one C").index(1)); - assertElementPresent(inputWithValue("key","1")); - assertElementPresent(inputWithValue("table","C")); - assertElementPresent(inputWithValue("title","one C")); - assertTrue(getCurrentRelativeURL().contains(WebTestHelper.buildRelativeUrl("junit", PROJECT_VERIFY, "echoForm"))); + assertTitleUrl(Locator.linkWithText("one B").index(1), "B", "one B"); + assertTitleUrl(Locator.linkWithText("one C").index(1), "C", "one C"); } popLocation(); }