Skip to content

Commit 992f939

Browse files
committed
update unittest
1 parent feab23b commit 992f939

File tree

3 files changed

+30
-3
lines changed

3 files changed

+30
-3
lines changed

ext/include/opentelemetry/ext/http/client/curl/http_client_curl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ class HttpClient : public opentelemetry::ext::http::client::HttpClient
322322

323323
inline CURLM *GetMultiHandle() noexcept { return multi_handle_; }
324324

325-
void MaybeSpawnBackgroundThread();
325+
// return true if create background thread, false is already exist background thread
326+
bool MaybeSpawnBackgroundThread();
326327

327328
void ScheduleAddSession(uint64_t session_id);
328329
void ScheduleAbortSession(uint64_t session_id);

ext/src/http/client/curl/http_client_curl.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,12 +339,12 @@ void HttpClient::CleanupSession(uint64_t session_id)
339339
}
340340
}
341341

342-
void HttpClient::MaybeSpawnBackgroundThread()
342+
bool HttpClient::MaybeSpawnBackgroundThread()
343343
{
344344
std::lock_guard<std::mutex> lock_guard{background_thread_m_};
345345
if (background_thread_)
346346
{
347-
return;
347+
return false;
348348
}
349349

350350
background_thread_.reset(new std::thread(
@@ -488,6 +488,7 @@ void HttpClient::MaybeSpawnBackgroundThread()
488488
}
489489
},
490490
this));
491+
return true;
491492
}
492493

493494
void HttpClient::ScheduleAddSession(uint64_t session_id)

ext/test/http/curl_http_test.cc

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
#include <curl/curlver.h>
45
#include <gtest/gtest.h>
56
#include <string.h>
67
#include <atomic>
@@ -11,6 +12,7 @@
1112
#include <memory>
1213
#include <mutex>
1314
#include <string>
15+
#include <thread>
1416
#include <utility>
1517
#include <vector>
1618

@@ -547,3 +549,26 @@ TEST_F(BasicCurlHttpTests, ElegantQuitQuick)
547549
ASSERT_TRUE(handler->is_called_);
548550
ASSERT_TRUE(handler->got_response_);
549551
}
552+
553+
TEST_F(BasicCurlHttpTests, BackgroundThreadWaitMore)
554+
{
555+
{
556+
curl::HttpClient http_client;
557+
http_client.MaybeSpawnBackgroundThread();
558+
std::this_thread::sleep_for(std::chrono::milliseconds{10});
559+
#if LIBCURL_VERSION_NUM >= 0x074200
560+
ASSERT_FALSE(http_client.MaybeSpawnBackgroundThread());
561+
#else
562+
// low version curl do not support delay quit, so old background would quit
563+
ASSERT_TRUE(http_client.MaybeSpawnBackgroundThread());
564+
#endif
565+
}
566+
{
567+
curl::HttpClient http_client;
568+
http_client.SetBackgroundWaitFor(std::chrono::milliseconds::zero());
569+
http_client.MaybeSpawnBackgroundThread();
570+
std::this_thread::sleep_for(std::chrono::milliseconds{10});
571+
// we can disable delay quit by set wait for 0
572+
ASSERT_TRUE(http_client.MaybeSpawnBackgroundThread());
573+
}
574+
}

0 commit comments

Comments
 (0)