Skip to content

Commit 147aa92

Browse files
authored
Merge pull request #265 from ursfassler/tmpfile
refactoring: programmatically generate temporary file name in test
2 parents 57faadb + 084c642 commit 147aa92

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

tests/integration/WireServerTest.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <filesystem>
66
#include <memory>
7+
#include <random>
78
#include <thread>
89
#include <chrono>
910

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

173174
virtual SocketServer* createListeningServer() {
174-
char filename[L_tmpnam];
175-
if (!std::tmpnam(filename)) {
176-
throw std::runtime_error("unable to create name for temporary file");
177-
}
175+
const std::string filename = std::filesystem::temp_directory_path() / randomString();
178176
server.reset(new UnixSocketServer(&protocolHandler));
179177
server->listen(filename);
180178
return server.get();
@@ -183,6 +181,18 @@ class UnixSocketServerTest : public SocketServerTest {
183181
virtual void destroyListeningServer() {
184182
server.reset();
185183
}
184+
185+
private:
186+
std::random_device rd{};
187+
std::mt19937 gen{rd()};
188+
std::uniform_int_distribution<> distrib{0, 15};
189+
190+
std::string randomString() {
191+
std::stringstream out{};
192+
for (std::size_t i = 0; i < 16; i++)
193+
out << std::hex << distrib(gen);
194+
return out.str();
195+
}
186196
};
187197

188198
/*

0 commit comments

Comments
 (0)