|
19 | 19 | */ |
20 | 20 |
|
21 | 21 | #include <curl/curl.h> |
| 22 | +#include <atomic> |
22 | 23 | #include <cstdio> |
23 | 24 | #include <cstring> |
24 | 25 | #include <iostream> |
|
27 | 28 | #include <numeric> |
28 | 29 | #include <sstream> |
29 | 30 | #include <string> |
| 31 | +#include <thread> |
30 | 32 | #include <utility> |
31 | 33 | #include <vector> |
32 | 34 |
|
@@ -1573,6 +1575,46 @@ LT_BEGIN_AUTO_TEST(basic_suite, method_not_allowed_header) |
1573 | 1575 | curl_easy_cleanup(curl); |
1574 | 1576 | LT_END_AUTO_TEST(method_not_allowed_header) |
1575 | 1577 |
|
| 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 | + |
1576 | 1618 | LT_BEGIN_AUTO_TEST_ENV() |
1577 | 1619 | AUTORUN_TESTS() |
1578 | 1620 | LT_END_AUTO_TEST_ENV() |
0 commit comments