36
36
using namespace testing ;
37
37
using namespace MAT ;
38
38
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
41
62
{
42
63
protected:
43
- std::list<HttpServer::Request> receivedRequests;
44
64
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
+
46
70
HttpServer server;
47
71
48
72
public:
@@ -54,8 +78,9 @@ class MultipleLogManagersTests : public ::testing::Test,
54
78
server.setServerName (os.str ());
55
79
serverAddress = " http://" + os.str ();
56
80
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);
59
84
60
85
server.start ();
61
86
@@ -77,9 +102,18 @@ class MultipleLogManagersTests : public ::testing::Test,
77
102
config2[" cacheFilePath" ] = " lm2.db" ;
78
103
::remove (config2[" cacheFilePath" ]);
79
104
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
+
83
117
}
84
118
85
119
virtual void TearDown () override
@@ -88,20 +122,29 @@ class MultipleLogManagersTests : public ::testing::Test,
88
122
server.stop ();
89
123
::remove (config1[" cacheFilePath" ]);
90
124
::remove (config2[" cacheFilePath" ]);
125
+ ::remove (config3[" cacheFilePath" ]);
126
+
91
127
}
92
128
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 )
94
131
{
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
+ }
98
141
}
99
142
100
- void waitForRequests (unsigned timeout, unsigned expectedCount = 1 )
143
+ void waitForRequestsSingleLogManager (unsigned timeout, unsigned expectedCount = 1 )
101
144
{
102
- auto sz = receivedRequests. size ();
145
+ auto sz = callback1. GetRequestCount ();
103
146
auto start = PAL::getUtcSystemTimeMs ();
104
- while (receivedRequests. size () - sz < expectedCount)
147
+ while (callback1. GetRequestCount () - sz < expectedCount)
105
148
{
106
149
if (PAL::getUtcSystemTimeMs () - start >= timeout)
107
150
{
@@ -124,40 +167,40 @@ class MultipleLogManagersTests : public ::testing::Test,
124
167
*/
125
168
};
126
169
127
- TEST_F (MultipleLogManagersTests, TwoInstancesCoexist )
170
+ TEST_F (MultipleLogManagersTests, ThreeInstancesCoexist )
128
171
{
129
172
std::unique_ptr<ILogManager> lm1 (LogManagerFactory::Create (config1));
130
173
std::unique_ptr<ILogManager> lm2 (LogManagerFactory::Create (config2));
174
+ std::unique_ptr<ILogManager> lm3 (LogManagerFactory::Create (config3));
131
175
132
176
lm1->SetContext (" test1" , " abc" );
133
-
134
177
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" );
135
182
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);
142
186
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 );
146
190
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);
151
194
152
195
lm1->GetLogController ()->UploadNow ();
153
196
lm2->GetLogController ()->UploadNow ();
197
+ lm3->GetLogController ()->UploadNow ();
154
198
155
- waitForRequests (5000 , 2 );
156
-
157
- // Add more tests
199
+ waitForRequestsMultipleLogManager (10000 , 1 , 1 , 1 );
158
200
159
201
lm1.reset ();
160
202
lm2.reset ();
203
+ lm3.reset ();
161
204
}
162
205
163
206
constexpr static unsigned max_iterations = 2000 ;
@@ -181,7 +224,7 @@ TEST_F(MultipleLogManagersTests, MultiProcessesLogManager)
181
224
CAPTURE_PERF_STATS (" Events Sent" );
182
225
lm->GetLogController ()->UploadNow ();
183
226
CAPTURE_PERF_STATS (" Events Uploaded" );
184
- waitForRequests (10000 , 2 );
227
+ waitForRequestsSingleLogManager (10000 , 2 );
185
228
lm.reset ();
186
229
CAPTURE_PERF_STATS (" Log Manager deleted" );
187
230
}
0 commit comments