Skip to content
Merged
Show file tree
Hide file tree
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
9 changes: 2 additions & 7 deletions src/org/labkey/test/tests/SiteSettingsApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -141,17 +142,11 @@ private Supplier<Connection> 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()
{
Expand Down
32 changes: 14 additions & 18 deletions src/org/labkey/test/tests/list/ListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -2244,7 +2246,7 @@ FieldDefinition colURL(String name, ColumnType type, String url)

List<FieldDefinition> 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[][]
Expand All @@ -2254,7 +2256,7 @@ FieldDefinition colURL(String name, ColumnType type, String url)

List<FieldDefinition> 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[][]
Expand All @@ -2264,7 +2266,7 @@ FieldDefinition colURL(String name, ColumnType type, String url)

List<FieldDefinition> 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[][]
{
Expand Down Expand Up @@ -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
Expand All @@ -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();
{
Expand All @@ -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();
}
Expand Down
111 changes: 111 additions & 0 deletions src/org/labkey/test/util/RequestCancellationHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.jetbrains.annotations.NotNull;
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 static final int CANCEL_COUNT_POLL_INTERVAL = 500;

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 !!(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 */
public long getCancelCount(@NotNull String metric)
{
try
{
return fetchCancelCount(metric);
}
catch (IOException | CommandException e)
{
throw new RuntimeException(e);
}
}

@SuppressWarnings("unchecked")
private long fetchCancelCount(String metric) throws IOException, CommandException
{
Map<String, Object> core = ServerUsageUtils.getModuleMetrics(_test.createDefaultConnection().setTimeout(METRICS_TIMEOUT), "Core");
Map<String, Object> counts = (Map<String, Object>) core.getOrDefault("simpleMetricCounts", Map.of());
Map<String, Object> requestCancel = (Map<String, Object>) 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(@NotNull String metric, long minimum)
{
// 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 @NotNull List<String> getModuleNames(@NotNull 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 @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(", "));
}
}
2 changes: 2 additions & 0 deletions src/org/labkey/test/util/exp/SampleTypeAPIHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down