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 0d3c0f7b76
commit 2f438b1d3a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

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.apphosting.api.ApiProxy;
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.BeforeEachCallback;
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");
private LocalServiceTestHelper helper;
private String taskQueueXml;
private File tmpDir;
public TaskQueueExtension() {
this.taskQueueXml = QUEUE_XML;
}
private Path queueFile;
@Override
public void beforeEach(ExtensionContext context) throws Exception {
File queueFile = new File(tmpDir, "queue.xml");
asCharSink(queueFile, UTF_8).write(taskQueueXml);
queueFile = Files.createTempFile("queue", ".xml");
asCharSink(queueFile.toFile(), UTF_8).write(QUEUE_XML);
helper =
new LocalServiceTestHelper(
new LocalTaskQueueTestConfig().setQueueXmlPath(queueFile.getAbsolutePath()));
new LocalTaskQueueTestConfig().setQueueXmlPath(queueFile.toAbsolutePath().toString()));
helper.setUp();
}
@Override
public void afterEach(ExtensionContext context) throws Exception {
helper.tearDown();
Files.delete(queueFile);
ApiProxy.setEnvironmentForCurrentThread(null);
}
}