Skip to content

Commit 2ff6e31

Browse files
add test
1 parent d249ba6 commit 2ff6e31

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

test/integ/basic.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@
1919
*/
2020

2121
#include <curl/curl.h>
22+
#include <atomic>
2223
#include <map>
2324
#include <memory>
2425
#include <numeric>
2526
#include <sstream>
2627
#include <string>
28+
#include <thread>
2729

2830
#include "./httpserver.hpp"
2931
#include "httpserver/string_utilities.hpp"
@@ -1462,6 +1464,46 @@ LT_BEGIN_AUTO_TEST(basic_suite, method_not_allowed_header)
14621464
curl_easy_cleanup(curl);
14631465
LT_END_AUTO_TEST(method_not_allowed_header)
14641466

1467+
LT_BEGIN_AUTO_TEST(basic_suite, thread_safety)
1468+
simple_resource resource;
1469+
1470+
std::atomic_bool done = false;
1471+
auto register_thread = std::thread([&]() {
1472+
int i = 0;
1473+
using namespace std::chrono;
1474+
while (!done) {
1475+
ws->register_resource(
1476+
std::string("/route") + std::to_string(++i), &resource);
1477+
}
1478+
});
1479+
1480+
auto get_thread = std::thread([&](){
1481+
while (!done) {
1482+
CURL *curl = curl_easy_init();
1483+
std::string s;
1484+
std::string url = "localhost:" PORT_STRING "/route" + std::to_string(
1485+
(int)((rand() * 10000000.0) / RAND_MAX));
1486+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
1487+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
1488+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
1489+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
1490+
curl_easy_perform(curl);
1491+
curl_easy_cleanup(curl);
1492+
}
1493+
});
1494+
1495+
using namespace std::chrono_literals;
1496+
std::this_thread::sleep_for(10s);
1497+
done = true;
1498+
if (register_thread.joinable()) {
1499+
register_thread.join();
1500+
}
1501+
if (get_thread.joinable()) {
1502+
get_thread.join();
1503+
}
1504+
LT_CHECK_EQ(1, 1);
1505+
LT_END_AUTO_TEST(thread_safety)
1506+
14651507
LT_BEGIN_AUTO_TEST_ENV()
14661508
AUTORUN_TESTS()
14671509
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)