Skip to content

Commit 8d3ff34

Browse files
authored
Merge branch 'main' into cmake-windows
2 parents a1aabdf + 38820bd commit 8d3ff34

File tree

3 files changed

+81
-38
lines changed

3 files changed

+81
-38
lines changed

lib/http/HttpClient_CAPI.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ namespace MAT_NS_BEGIN {
186186
void HttpClient_CAPI::CancelRequestAsync(const std::string& id)
187187
{
188188
LOG_TRACE("Cancelling CAPI HTTP request '%s'", id.c_str());
189-
std::shared_ptr<HttpClient_Operation> operation;
189+
std::shared_ptr<HttpClient_Operation> operation(nullptr);
190190
{
191191
// Only lock mutex while actually reading/writing pending operations collection to prevent potential recursive deadlock
192192
LOCKGUARD(s_operationsLock);
@@ -199,7 +199,7 @@ namespace MAT_NS_BEGIN {
199199

200200
if (operation != nullptr)
201201
{
202-
operation->Cancel();
202+
operation->Cancel();// CodeQL [cpp/uninitializedptrfield] operation is explicitly constructed with nullptr so it will never hold garbage value
203203
}
204204
}
205205

lib/include/mat/json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6842,7 +6842,7 @@ class lexer : public lexer_base<BasicJsonType>
68426842
locale's decimal point is used instead of `.` to work with the
68436843
locale-dependent converters.
68446844
*/
6845-
token_type scan_number() // lgtm [cpp/use-of-goto]
6845+
token_type scan_number() // CodeQL [cpp/use-of-goto] We explicitly allow the use of goto in this func
68466846
{
68476847
// reset token_buffer to store the number's bytes
68486848
reset();

tests/functests/MultipleLogManagersTests.cpp

Lines changed: 78 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,37 @@
3636
using namespace testing;
3737
using namespace MAT;
3838

39-
class MultipleLogManagersTests : public ::testing::Test,
40-
public HttpServer::Callback
39+
class RequestHandler : public HttpServer::Callback
40+
{
41+
public:
42+
RequestHandler(int id) : m_count(0), m_id(id){}
43+
44+
int onHttpRequest(HttpServer::Request const& request, HttpServer::Response& /*response*/) override
45+
{
46+
std::string expected_url = "/" + std::to_string(m_id) + "/";
47+
EXPECT_EQ(request.uri, expected_url);
48+
m_count++;
49+
return 200;
50+
}
51+
52+
size_t GetRequestCount() {
53+
return m_count;
54+
}
55+
56+
private:
57+
size_t m_count;
58+
int m_id ;
59+
};
60+
61+
class MultipleLogManagersTests : public ::testing::Test
4162
{
4263
protected:
43-
std::list<HttpServer::Request> receivedRequests;
4464
std::string serverAddress;
45-
ILogConfiguration config1, config2;
65+
ILogConfiguration config1, config2, config3;
66+
RequestHandler callback1 = RequestHandler(1);
67+
RequestHandler callback2 = RequestHandler(2);
68+
RequestHandler callback3 = RequestHandler(3);
69+
4670
HttpServer server;
4771

4872
public:
@@ -54,8 +78,9 @@ class MultipleLogManagersTests : public ::testing::Test,
5478
server.setServerName(os.str());
5579
serverAddress = "http://" + os.str();
5680

57-
server.addHandler("/1/", *this);
58-
server.addHandler("/2/", *this);
81+
server.addHandler("/1/", callback1);
82+
server.addHandler("/2/", callback2);
83+
server.addHandler("/3/", callback3);
5984

6085
server.start();
6186

@@ -77,9 +102,18 @@ class MultipleLogManagersTests : public ::testing::Test,
77102
config2["cacheFilePath"] = "lm2.db";
78103
::remove(config2["cacheFilePath"]);
79104
config2[CFG_STR_COLLECTOR_URL] = serverAddress + "/2/";
80-
config1["name"] = "Instance2";
81-
config1["version"] = "1.0.0";
82-
config1["config"]["host"] = "Instance2"; // host
105+
config2["name"] = "Instance2";
106+
config2["version"] = "1.0.0";
107+
config2["config"]["host"] = "Instance2"; // host
108+
109+
// Config for instance #3
110+
config3["cacheFilePath"] = "lm3.db";
111+
::remove(config3["cacheFilePath"]);
112+
config3[CFG_STR_COLLECTOR_URL] = serverAddress + "/3/";
113+
config3["name"] = "Instance3";
114+
config3["version"] = "1.0.0";
115+
config3["config"]["host"] = "Instance3"; // host
116+
83117
}
84118

85119
virtual void TearDown() override
@@ -88,20 +122,29 @@ class MultipleLogManagersTests : public ::testing::Test,
88122
server.stop();
89123
::remove(config1["cacheFilePath"]);
90124
::remove(config2["cacheFilePath"]);
125+
::remove(config3["cacheFilePath"]);
126+
91127
}
92128

93-
virtual int onHttpRequest(HttpServer::Request const& request, HttpServer::Response& response) override
129+
130+
void waitForRequestsMultipleLogManager(unsigned timeout, unsigned expectedCount1 = 1, unsigned expectedCount2 = 1, unsigned expectedCount3 = 1)
94131
{
95-
UNREFERENCED_PARAMETER(response);
96-
receivedRequests.push_back(request);
97-
return 200;
132+
auto start = PAL::getUtcSystemTimeMs();
133+
while (callback1.GetRequestCount() < expectedCount1 || callback2.GetRequestCount() < expectedCount2 || callback3.GetRequestCount() < expectedCount3)
134+
{
135+
if (PAL::getUtcSystemTimeMs() - start >= timeout)
136+
{
137+
GTEST_FATAL_FAILURE_("Didn't receive request within given timeout");
138+
}
139+
PAL::sleep(100);
140+
}
98141
}
99142

100-
void waitForRequests(unsigned timeout, unsigned expectedCount = 1)
143+
void waitForRequestsSingleLogManager(unsigned timeout, unsigned expectedCount = 1)
101144
{
102-
auto sz = receivedRequests.size();
145+
auto sz = callback1.GetRequestCount();
103146
auto start = PAL::getUtcSystemTimeMs();
104-
while (receivedRequests.size() - sz < expectedCount)
147+
while (callback1.GetRequestCount() - sz < expectedCount)
105148
{
106149
if (PAL::getUtcSystemTimeMs() - start >= timeout)
107150
{
@@ -124,40 +167,40 @@ class MultipleLogManagersTests : public ::testing::Test,
124167
*/
125168
};
126169

127-
TEST_F(MultipleLogManagersTests, TwoInstancesCoexist)
170+
TEST_F(MultipleLogManagersTests, ThreeInstancesCoexist)
128171
{
129172
std::unique_ptr<ILogManager> lm1(LogManagerFactory::Create(config1));
130173
std::unique_ptr<ILogManager> lm2(LogManagerFactory::Create(config2));
174+
std::unique_ptr<ILogManager> lm3(LogManagerFactory::Create(config3));
131175

132176
lm1->SetContext("test1", "abc");
133-
134177
lm2->GetSemanticContext().SetAppId("123");
178+
179+
ILogger* l1 = lm1->GetLogger("lm1_token1", "aaa-source");
180+
ILogger* l2 = lm2->GetLogger("lm2_token1", "bbb-source");
181+
ILogger* l3 = lm3->GetLogger("lm3_token1", "ccc-source");
135182

136-
ILogger* l1a = lm1->GetLogger("aaa");
137-
138-
ILogger* l2a = lm2->GetLogger("aaa", "aaa-source");
139-
EventProperties l2a1p("l2a1");
140-
l2a1p.SetProperty("x", "y");
141-
l2a->LogEvent(l2a1p);
183+
EventProperties l1_prop("l1a1");
184+
l1_prop.SetProperty("X", "Y");
185+
l1->LogEvent(l1_prop);
142186

143-
EventProperties l1a1p("l1a1");
144-
l1a1p.SetProperty("X", "Y");
145-
l1a->LogEvent(l1a1p);
187+
EventProperties l2_prop("l2a1");
188+
l2_prop.SetProperty("x", "y");
189+
l2->LogEvent(l2_prop);
146190

147-
ILogger* l1b = lm1->GetLogger("bbb");
148-
EventProperties l1b1p("l1b1");
149-
l1b1p.SetProperty("asdf", 1234);
150-
l1b->LogEvent(l1b1p);
191+
EventProperties l3_prop("l3a1");
192+
l3_prop.SetProperty("test", 1234);
193+
l3->LogEvent(l3_prop);
151194

152195
lm1->GetLogController()->UploadNow();
153196
lm2->GetLogController()->UploadNow();
197+
lm3->GetLogController()->UploadNow();
154198

155-
waitForRequests(5000, 2);
156-
157-
// Add more tests
199+
waitForRequestsMultipleLogManager(10000, 1, 1, 1);
158200

159201
lm1.reset();
160202
lm2.reset();
203+
lm3.reset();
161204
}
162205

163206
constexpr static unsigned max_iterations = 2000;
@@ -181,7 +224,7 @@ TEST_F(MultipleLogManagersTests, MultiProcessesLogManager)
181224
CAPTURE_PERF_STATS("Events Sent");
182225
lm->GetLogController()->UploadNow();
183226
CAPTURE_PERF_STATS("Events Uploaded");
184-
waitForRequests(10000, 2);
227+
waitForRequestsSingleLogManager(10000, 2);
185228
lm.reset();
186229
CAPTURE_PERF_STATS("Log Manager deleted");
187230
}

0 commit comments

Comments
 (0)