Skip to content

Commit

Permalink
allow new snapshot json test to pass when using multiple threads
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonCalabrese committed Aug 31, 2022
1 parent 21cb19d commit 1061b94
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
2 changes: 1 addition & 1 deletion libraries/chain/snapshot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ istream_json_snapshot_reader::istream_json_snapshot_reader(const fc::path& p)
: impl{new istream_json_snapshot_reader_impl{0, 0, {}, {}}}
{
FILE* fp = fopen(p.string().c_str(), "rb");
EOS_ASSERT(fp, snapshot_exception, "Failed to JSON snapshot: ${file}", ("file", p));
EOS_ASSERT(fp, snapshot_exception, "Failed to open JSON snapshot: ${file}", ("file", p));
auto close = fc::make_scoped_exit( [&fp]() { fclose( fp ); } );
char readBuffer[65536];
eosio_rapidjson::FileReadStream is(fp, readBuffer, sizeof(readBuffer));
Expand Down
12 changes: 9 additions & 3 deletions libraries/testing/include/eosio/testing/snapshot_suites.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,18 @@ struct json_snapshot_suite {
std::shared_ptr<write_storage_t> storage;
};

static std::string temp_file() {
static fc::temp_directory temp_dir;

This comment has been minimized.

Copy link
@heifner

heifner Aug 31, 2022

Member

static are not thread specific storage, use thread_local for that. Instead of either, we should generate a unique file for each test run, regardless of thread or process. These tests are not run in separate threads, instead they are run in diff processes.

std::string temp_file = temp_dir.path().string() + "temp.bin.json";
return temp_file;
}

struct reader : public reader_t {
explicit reader(const fc::path& p)
:reader_t(p)
{}
~reader() {
remove("temp.bin.json");
remove(json_snapshot_suite::temp_file().c_str());
}
};

Expand All @@ -145,10 +151,10 @@ struct json_snapshot_suite {
}

static auto get_reader( const snapshot_t& buffer) {
std::ofstream fs("temp.bin.json");
std::ofstream fs(json_snapshot_suite::temp_file());
fs << buffer;
fs.close();
fc::path p("temp.bin.json");
fc::path p(json_snapshot_suite::temp_file());
return std::make_shared<reader>(p);
}

Expand Down

0 comments on commit 1061b94

Please sign in to comment.