aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/tests
diff options
context:
space:
mode:
authorTor Brede Vekterli <vekterli@oath.com>2018-04-09 13:12:53 +0000
committerTor Brede Vekterli <vekterli@oath.com>2018-04-09 13:12:53 +0000
commite2b69ec232f02e119b27ced1b1c5fca6766ea2b1 (patch)
tree504b7df2671d136762f34dbf7bf699abd0022c0c /documentapi/src/tests
parent1f8d85a942add93368e9f87b782e22f70a524fc7 (diff)
Only write protocol files if contents have changed (C++)
Prevents races between reader and writer when tests across languages run in parallel.
Diffstat (limited to 'documentapi/src/tests')
-rw-r--r--documentapi/src/tests/messages/testbase.cpp20
-rw-r--r--documentapi/src/tests/messages/testbase.h2
2 files changed, 18 insertions, 4 deletions
diff --git a/documentapi/src/tests/messages/testbase.cpp b/documentapi/src/tests/messages/testbase.cpp
index 02c180853d4..18902cb39a7 100644
--- a/documentapi/src/tests/messages/testbase.cpp
+++ b/documentapi/src/tests/messages/testbase.cpp
@@ -4,6 +4,7 @@
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/base/testdocrepo.h>
#include <vespa/vespalib/util/exception.h>
+#include <vespa/vespalib/io/fileutil.h>
#include <fcntl.h>
#include <unistd.h>
#include <algorithm>
@@ -109,6 +110,15 @@ TestBase::testCoverage(const std::vector<uint32_t> &expected, const std::vector<
return ret;
}
+bool TestBase::file_content_is_unchanged(const string& filename, const mbus::Blob& data_to_write) const {
+ if (!vespalib::fileExists(filename)) {
+ return false;
+ }
+ mbus::Blob existing = readFile(filename);
+ return ((existing.size() == data_to_write.size())
+ && (memcmp(existing.data(), data_to_write.data(), data_to_write.size()) == 0));
+}
+
uint32_t
TestBase::serialize(const string &filename, const mbus::Routable &routable)
{
@@ -117,7 +127,9 @@ TestBase::serialize(const string &filename, const mbus::Routable &routable)
LOG(info, "Serializing to '%s'..", path.c_str());
mbus::Blob blob = _protocol.encode(version, routable);
- if (!EXPECT_TRUE(writeFile(path, blob))) {
+ if (file_content_is_unchanged(path, blob)) {
+ LOG(info, "Serialization for '%s' is unchanged; not overwriting it", path.c_str());
+ } else if (!EXPECT_TRUE(writeFile(path, blob))) {
LOG(error, "Could not open file '%s' for writing.", path.c_str());
return 0;
}
@@ -180,7 +192,7 @@ TestBase::writeFile(const string &filename, const mbus::Blob& blob) const
return false;
}
if (write(file, blob.data(), blob.size()) != (ssize_t)blob.size()) {
- throw vespalib::Exception("write failed");
+ throw vespalib::Exception("write failed");
}
close(file);
return true;
@@ -195,8 +207,8 @@ TestBase::readFile(const string &filename) const
if (file != -1) {
lseek(file, 0, SEEK_SET);
if (read(file, blob.data(), len) != len) {
- throw vespalib::Exception("read failed");
- }
+ throw vespalib::Exception("read failed");
+ }
close(file);
}
diff --git a/documentapi/src/tests/messages/testbase.h b/documentapi/src/tests/messages/testbase.h
index e47858cda4b..e87f9104189 100644
--- a/documentapi/src/tests/messages/testbase.h
+++ b/documentapi/src/tests/messages/testbase.h
@@ -57,5 +57,7 @@ public:
string getPath(const string &filename) const { return _dataPath + "/" + filename; }
mbus::Blob encode(const mbus::Routable &obj) const { return _protocol.encode(getVersion(), obj); }
mbus::Routable::UP decode(mbus::BlobRef data) const { return _protocol.decode(getVersion(), data); }
+private:
+ bool file_content_is_unchanged(const string& filename, const mbus::Blob& data_to_write) const;
};