summaryrefslogtreecommitdiffstats
path: root/documentapi
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@oath.com>2018-04-09 15:01:56 +0200
committerTor Brede Vekterli <vekterli@oath.com>2018-04-09 15:03:07 +0200
commit1f8d85a942add93368e9f87b782e22f70a524fc7 (patch)
treecda791813b6416fb52ed3e3f8a45ee7b284343bd /documentapi
parent313b273964320f7d8f7d8c319a7bee6af484b72d (diff)
Only write protocol files if contents have changed (Java)
Prevents races between reader and writer when tests across languages run in parallel.
Diffstat (limited to 'documentapi')
-rwxr-xr-xdocumentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/MessagesTestBase.java20
1 files changed, 18 insertions, 2 deletions
diff --git a/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/MessagesTestBase.java b/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/MessagesTestBase.java
index eb5de11bbc9..9e355fa3381 100755
--- a/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/MessagesTestBase.java
+++ b/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/MessagesTestBase.java
@@ -10,6 +10,8 @@ import com.yahoo.messagebus.Routable;
import org.junit.Test;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Paths;
import java.util.*;
import static org.junit.Assert.*;
@@ -87,6 +89,14 @@ public abstract class MessagesTestBase {
return TestFileUtil.getPath(filename);
}
+ private boolean fileContentIsUnchanged(String path, byte[] dataToWrite) throws IOException {
+ if (!Files.exists(Paths.get(path))) {
+ return false;
+ }
+ byte[] existingData = TestFileUtil.readFile(path);
+ return Arrays.equals(existingData, dataToWrite);
+ }
+
/**
* Writes the content of the given routable to the given file.
*
@@ -97,12 +107,18 @@ public abstract class MessagesTestBase {
public int serialize(String filename, Routable routable) {
Version version = version();
String path = getPath(version + "-java-" + filename + ".dat");
- System.out.println("Serializing to '" + path + "'..");
byte[] data = protocol.encode(version, routable);
assertNotNull(data);
assertTrue(data.length > 0);
try {
- TestFileUtil.writeToFile(path, data);
+ if (fileContentIsUnchanged(path, data)) {
+ System.out.println(String.format("Serialization for '%s' is unchanged; not overwriting it", path));
+ } else {
+ System.out.println(String.format("Serializing to '%s'..", path));
+ // This only happens when protocol encoding has changed and takes place
+ // during local development, not regular test runs.
+ TestFileUtil.writeToFile(path, data);
+ }
} catch (IOException e) {
throw new AssertionError(e);
}