From 142431b0a88cc4055c3886ff438cc1d0a83e934a Mon Sep 17 00:00:00 2001 From: superjom Date: Mon, 18 Dec 2017 10:16:10 +0800 Subject: [PATCH] fix test unstable with try recursive make tmp some time should wait between read service and get item --- visualdl/logic/sdk_test.cc | 7 +++++-- visualdl/storage/storage.cc | 2 +- visualdl/utils/filesystem.h | 15 ++++++++++++++- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/visualdl/logic/sdk_test.cc b/visualdl/logic/sdk_test.cc index 5f204095..f800f31e 100644 --- a/visualdl/logic/sdk_test.cc +++ b/visualdl/logic/sdk_test.cc @@ -7,13 +7,16 @@ namespace visualdl { struct ScalarTestHelper { ImHelper rim; ImHelper wim; - const std::string dir = "./tmp/1.test"; + const std::string dir = "./tmp/sdk_test.test"; void operator()(std::function read, std::function write) { wim.StartWriteSerice(dir, 200); write(); - rim.StartReadService(dir, 200); + // should wait for the write service create log's path + std::this_thread::sleep_for(std::chrono::milliseconds(400)); + rim.StartReadService(dir, 100); + // should wait for the read service to load "tag0" tablet into memory std::this_thread::sleep_for(std::chrono::milliseconds(600)); read(); } diff --git a/visualdl/storage/storage.cc b/visualdl/storage/storage.cc index ec028647..d5b54166 100644 --- a/visualdl/storage/storage.cc +++ b/visualdl/storage/storage.cc @@ -44,7 +44,7 @@ void MemoryStorage::PersistToDisk(const std::string &dir) { CHECK(!dir.empty()); storage_.set_dir(dir); // make a directory if not exist - fs::TryMkdir(dir); + fs::TryRecurMkdir(dir); // write storage out LOG(INFO) << "to serize meta to dir " << dir; fs::SerializeToFile(storage_, meta_path(dir)); diff --git a/visualdl/utils/filesystem.h b/visualdl/utils/filesystem.h index 1a416749..ed97d03b 100644 --- a/visualdl/utils/filesystem.h +++ b/visualdl/utils/filesystem.h @@ -44,13 +44,26 @@ bool DeSerializeFromFile(T* proto, const std::string& path) { } void TryMkdir(const std::string& dir) { - VLOG(1) << "try to mkdir " << dir; + LOG(INFO) << "try to mkdir " << dir; struct stat st = {0}; if (stat(dir.c_str(), &st) == -1) { ::mkdir(dir.c_str(), 0700); } } +// Create a path by recursively create directries +void TryRecurMkdir(const std::string& path) { + // split path by '/' + for (int i = 1; i < path.size() - 1; i++) { + if (path[i] == '/') { + auto dir = path.substr(0, i + 1); + TryMkdir(dir); + } + } + // the last level + TryMkdir(path); +} + inline void Write(const std::string& path, const std::string& buffer, std::ios::openmode open_mode = std::ios::binary) { -- GitLab