Skip to content

Commit

Permalink
Update test before/after steps to avoid problems with locked files
Browse files Browse the repository at this point in the history
  • Loading branch information
tdrwenski committed Feb 22, 2024
1 parent f734885 commit 3188915
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package thredds.server.services;

import static thredds.server.util.AdminDebugUtils.*;
import java.nio.charset.StandardCharsets;
import org.apache.http.HttpStatus;
import org.apache.http.auth.Credentials;
Expand Down Expand Up @@ -28,8 +29,6 @@
public class TestAdminDebug {
private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

private static String urlPrefix = "https://localhost:8443/thredds/";
private static Credentials goodCred = new UsernamePasswordCredentials("tds", "secret666");
private static Credentials badCred = new UsernamePasswordCredentials("bad", "worse");

@Parameterized.Parameters(name = "{0}")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package thredds.server.util;

import javax.servlet.http.HttpServletResponse;
import org.apache.http.auth.Credentials;
import org.apache.http.auth.UsernamePasswordCredentials;
import thredds.test.util.TestOnLocalServer;
import thredds.util.ContentType;

public class AdminDebugUtils {
public static final String urlPrefix = "https://localhost:8443/thredds/";
public static final Credentials goodCred = new UsernamePasswordCredentials("tds", "secret666");

public static void disableRafCache() {
final String endpoint = urlPrefix + "admin/debug?Caches/disableRAFCache";
TestOnLocalServer.getContent(goodCred, endpoint, new int[] {HttpServletResponse.SC_OK}, ContentType.html);
}

public static void enableRafCache() {
final String endpoint = urlPrefix + "admin/debug?Caches/enableRAFCache";
TestOnLocalServer.getContent(goodCred, endpoint, new int[] {HttpServletResponse.SC_OK}, ContentType.html);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
import org.jdom2.input.SAXBuilder;
import org.jdom2.xpath.XPathExpression;
import org.jdom2.xpath.XPathFactory;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
Expand All @@ -29,6 +31,7 @@
import java.io.Reader;
import java.io.StringReader;
import java.lang.invoke.MethodHandles;
import thredds.server.util.AdminDebugUtils;
import thredds.test.util.TestOnLocalServer;
import thredds.util.ContentType;

Expand All @@ -47,6 +50,17 @@ public class TestUpdateWmsServer {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(new File(DIR));

@BeforeClass
public static void disableRafCache() {
// Avoid file locks which prevent files from being updated on windows
AdminDebugUtils.disableRafCache();
}

@AfterClass
public static void enableRafCache() {
AdminDebugUtils.enableRafCache();
}

@Test
public void testUpdateFile() throws IOException, JDOMException {
final String filePath = temporaryFolder.getRoot().getName() + "/" + FILENAME;
Expand Down
8 changes: 8 additions & 0 deletions tds/src/main/java/thredds/server/admin/DebugCommands.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ public void doAction(Event e) {
};
debugHandler.addAction(act);

act = new Action("enableRAFCache", "Enable RandomAccessFile Cache") {
public void doAction(Event e) {
RandomAccessFile.getGlobalFileCache().enable();
e.pw.println(" Enable RandomAccessFile Cache ok");
}
};
debugHandler.addAction(act);

act = new Action("forceRAFCache", "Force clear RandomAccessFile Cache") {
public void doAction(Event e) {
RandomAccessFile.getGlobalFileCache().clearCache(true);
Expand Down
18 changes: 18 additions & 0 deletions tds/src/test/java/thredds/server/wms/TestWmsCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import java.lang.invoke.MethodHandles;
import ucar.nc2.dataset.NetcdfDatasets;
import ucar.nc2.util.cache.FileCacheIF;
import ucar.unidata.io.RandomAccessFile;
import ucar.unidata.util.test.category.NeedsCdmUnitTest;

@RunWith(SpringJUnit4ClassRunner.class)
Expand All @@ -48,6 +49,23 @@ public class TestWmsCache {
@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder(new File(DIR));

@BeforeClass
public static void disableRafCache() {
// Avoid file locks which prevent files from being updated on windows
final FileCacheIF rafCache = RandomAccessFile.getGlobalFileCache();
if (rafCache != null) {
rafCache.disable();
}
}

@AfterClass
public static void enableRafCache() {
final FileCacheIF rafCache = RandomAccessFile.getGlobalFileCache();
if (rafCache != null) {
rafCache.enable();
}
}

@Before
public void createTestFiles() throws IOException {
File tempFile = temporaryFolder.newFile(FILENAME);
Expand Down

0 comments on commit 3188915

Please sign in to comment.