Fix flaky tests with TaskQueueExtension (#1909)

The temporary queue.xml file is not deleted in the afterEach() method,
likely causing some flaky tests that we saw due to overwriting of the
file by concurrent tests.
This commit is contained in:
Lai Jiang 2023-01-18 12:04:47 -05:00 committed by GitHub
parent 4067f7c3e4
commit f65b6ece5d

View file

@ -22,7 +22,8 @@ import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig; import com.google.appengine.tools.development.testing.LocalTaskQueueTestConfig;
import com.google.apphosting.api.ApiProxy; import com.google.apphosting.api.ApiProxy;
import google.registry.model.annotations.DeleteAfterMigration; import google.registry.model.annotations.DeleteAfterMigration;
import java.io.File; import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.extension.AfterEachCallback; import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext; import org.junit.jupiter.api.extension.ExtensionContext;
@ -39,26 +40,22 @@ public final class TaskQueueExtension implements BeforeEachCallback, AfterEachCa
readResourceUtf8("google/registry/env/common/default/WEB-INF/queue.xml"); readResourceUtf8("google/registry/env/common/default/WEB-INF/queue.xml");
private LocalServiceTestHelper helper; private LocalServiceTestHelper helper;
private String taskQueueXml; private Path queueFile;
private File tmpDir;
public TaskQueueExtension() {
this.taskQueueXml = QUEUE_XML;
}
@Override @Override
public void beforeEach(ExtensionContext context) throws Exception { public void beforeEach(ExtensionContext context) throws Exception {
File queueFile = new File(tmpDir, "queue.xml"); queueFile = Files.createTempFile("queue", ".xml");
asCharSink(queueFile, UTF_8).write(taskQueueXml); asCharSink(queueFile.toFile(), UTF_8).write(QUEUE_XML);
helper = helper =
new LocalServiceTestHelper( new LocalServiceTestHelper(
new LocalTaskQueueTestConfig().setQueueXmlPath(queueFile.getAbsolutePath())); new LocalTaskQueueTestConfig().setQueueXmlPath(queueFile.toAbsolutePath().toString()));
helper.setUp(); helper.setUp();
} }
@Override @Override
public void afterEach(ExtensionContext context) throws Exception { public void afterEach(ExtensionContext context) throws Exception {
helper.tearDown(); helper.tearDown();
Files.delete(queueFile);
ApiProxy.setEnvironmentForCurrentThread(null); ApiProxy.setEnvironmentForCurrentThread(null);
} }
} }