Skip to content

Commit

Permalink
refactoring: programmatically generate temporary file name in test
Browse files Browse the repository at this point in the history
Latest gcc throws errors when using tmpnam. Generate temporary file name
within the test to work around. It has the same shortcomings as using
tmpnam, but they are no risk as it is used only in the tests.
  • Loading branch information
ursfassler committed Dec 10, 2023
1 parent 57faadb commit 2a8465b
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions tests/integration/WireServerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <filesystem>
#include <memory>
#include <random>
#include <thread>
#include <chrono>

Expand Down Expand Up @@ -171,10 +172,7 @@ class UnixSocketServerTest : public SocketServerTest {
std::unique_ptr<UnixSocketServer> server;

virtual SocketServer* createListeningServer() {
char filename[L_tmpnam];
if (!std::tmpnam(filename)) {
throw std::runtime_error("unable to create name for temporary file");
}
const std::string filename = std::filesystem::temp_directory_path() / randomString();
server.reset(new UnixSocketServer(&protocolHandler));
server->listen(filename);
return server.get();
Expand All @@ -183,6 +181,20 @@ class UnixSocketServerTest : public SocketServerTest {
virtual void destroyListeningServer() {
server.reset();
}

private:
std::random_device rd{};
std::mt19937 gen{rd()};
std::uniform_int_distribution<> distrib{0, 15};

std::string randomString()
{
std::stringstream out{};
for (std::size_t i = 0; i < 16; i++) {
out << std::hex << distrib(gen);
}
return out.str();
}
};

/*
Expand Down

0 comments on commit 2a8465b

Please sign in to comment.