Write to temp file and do atomic rename in Java DocumentAPI tests
Should avoid racing with concurrent reads by C++ DocumentAPI tests.
@@ -4,16 +4,27 @@ package com.yahoo.documentapi.messagebus.protocol.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
+import java.nio.file.FileSystems;
import java.nio.file.Files;
+import java.nio.file.Path;
import java.nio.file.Paths;
+import static java.nio.file.StandardCopyOption.ATOMIC_MOVE;
+
public class TestFileUtil {
protected static final String DATA_PATH = "./test/crosslanguagefiles";
public static void writeToFile(String path, byte[] data) throws IOException {
- try (FileOutputStream stream = new FileOutputStream(path)) {
+ // Write to a temporary file to avoid racing with cross-language tests reading the
+ // exact same file we're trying to write.
+ String tmpPath = path + ".tmp";
+ try (FileOutputStream stream = new FileOutputStream(tmpPath)) {
stream.write(data);
+ stream.flush();
}
+ // We make the assumption that all file systems we run these tests on support some form
+ // of atomic moving rather than "move by content copy".
+ Files.move(FileSystems.getDefault().getPath(tmpPath), FileSystems.getDefault().getPath(path), ATOMIC_MOVE);
}
/**
