1 #include <gtest/gtest.h>
10 #include <boost/filesystem.hpp>
12 #include "storage/sqlstorage.h"
13 #include "utilities/utils.h"
15 #include "logging/logging.h"
17 StorageType storage_test_type;
19 std::unique_ptr<INvStorage> Storage(
const StorageConfig& config) {
20 if (config.type == StorageType::kSqlite) {
21 return std::unique_ptr<INvStorage>(
new SQLStorage(config,
false));
23 throw std::runtime_error(
"Invalid config type");
27 StorageConfig MakeConfig(StorageType type,
const boost::filesystem::path& storage_dir) {
31 if (config.type == StorageType::kSqlite) {
32 config.sqldb_path = storage_dir /
"test.db";
34 throw std::runtime_error(
"Invalid config type");
46 StorageConfig config = MakeConfig(storage_test_type, t.storage_dir.Path());
47 std::unique_ptr<INvStorage> storage = Storage(config);
51 EXPECT_EQ(write(t.pipefd[1], &c, 1), 1);
53 storage->storePrimaryKeys(std::to_string(k), std::to_string(k));
58 static void check_consistent_state(
const boost::filesystem::path& storage_dir) {
59 StorageConfig config = MakeConfig(storage_test_type, storage_dir);
60 std::unique_ptr<INvStorage> storage = Storage(config);
61 std::string pub, priv;
63 EXPECT_TRUE(storage->loadPrimaryKeys(&pub, &priv));
69 unsigned int n_procs = 70;
70 std::list<TightProcess> procs;
72 for (
auto k = 0u; k < n_procs; k++) {
76 EXPECT_EQ(pipe(p.pipefd), 0);
90 for (
const auto& p : procs) {
93 EXPECT_EQ(read(p.pipefd[0], &c, 1), 1);
97 std::mt19937_64 eng{12};
98 std::uniform_int_distribution<> dist{1, 30};
99 while (!procs.empty()) {
100 std::this_thread::sleep_for(std::chrono::milliseconds{dist(eng)});
103 kill(p.pid, SIGKILL);
104 waitpid(p.pid, NULL, 0);
105 check_consistent_state(p.storage_dir.Path());
114 TEST(DISABLED_storage_atomic, sql) {
116 storage_test_type = StorageType::kSqlite;
121 int main(
int argc,
char** argv) {
122 ::testing::InitGoogleTest(&argc, argv);
124 logger_set_threshold(boost::log::trivial::trace);
125 return RUN_ALL_TESTS();