aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@oath.com>2018-04-06 15:33:21 +0200
committerTor Brede Vekterli <vekterli@oath.com>2018-04-06 15:33:21 +0200
commit3b6a6e3506af3b196715485e59514e91925d96b4 (patch)
treebb641e26dd60d8f1e0aebd71096439ecfe552382 /documentapi
parent91b873df42717710b48fd614a589e179d8bd4ab7 (diff)
Write to temp file and do atomic rename in Java DocumentAPI tests
Should avoid racing with concurrent reads by C++ DocumentAPI tests.
Diffstat (limited to 'documentapi')
-rw-r--r--documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/TestFileUtil.java13
1 files changed, 12 insertions, 1 deletions
diff --git a/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/TestFileUtil.java b/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/TestFileUtil.java
index 8bc6f56e574..50f8f82b180 100644
--- a/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/TestFileUtil.java
+++ b/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/TestFileUtil.java
@@ -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);
}
/**