Skip to content

Commit b1662f7

Browse files
fchevassu-antidotetr
authored andcommitted
add test
1 parent f15a7b0 commit b1662f7

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,6 +19,7 @@
1919
*/
2020

2121
#include <curl/curl.h>
22+
#include <atomic>
2223
#include <cstdio>
2324
#include <cstring>
2425
#include <iostream>
@@ -27,6 +28,7 @@
2728
#include <numeric>
2829
#include <sstream>
2930
#include <string>
31+
#include <thread>
3032
#include <utility>
3133
#include <vector>
3234

@@ -1573,6 +1575,46 @@ LT_BEGIN_AUTO_TEST(basic_suite, method_not_allowed_header)
15731575
curl_easy_cleanup(curl);
15741576
LT_END_AUTO_TEST(method_not_allowed_header)
15751577

1578+
LT_BEGIN_AUTO_TEST(basic_suite, thread_safety)
1579+
simple_resource resource;
1580+
1581+
std::atomic_bool done = false;
1582+
auto register_thread = std::thread([&]() {
1583+
int i = 0;
1584+
using namespace std::chrono;
1585+
while (!done) {
1586+
ws->register_resource(
1587+
std::string("/route") + std::to_string(++i), &resource);
1588+
}
1589+
});
1590+
1591+
auto get_thread = std::thread([&](){
1592+
while (!done) {
1593+
CURL *curl = curl_easy_init();
1594+
std::string s;
1595+
std::string url = "localhost:" PORT_STRING "/route" + std::to_string(
1596+
(int)((rand() * 10000000.0) / RAND_MAX));
1597+
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
1598+
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L);
1599+
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc);
1600+
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
1601+
curl_easy_perform(curl);
1602+
curl_easy_cleanup(curl);
1603+
}
1604+
});
1605+
1606+
using namespace std::chrono_literals;
1607+
std::this_thread::sleep_for(10s);
1608+
done = true;
1609+
if (register_thread.joinable()) {
1610+
register_thread.join();
1611+
}
1612+
if (get_thread.joinable()) {
1613+
get_thread.join();
1614+
}
1615+
LT_CHECK_EQ(1, 1);
1616+
LT_END_AUTO_TEST(thread_safety)
1617+
15761618
LT_BEGIN_AUTO_TEST_ENV()
15771619
AUTORUN_TESTS()
15781620
LT_END_AUTO_TEST_ENV()

0 commit comments

Comments
 (0)