-
Notifications
You must be signed in to change notification settings - Fork 42
/
sandesh_state_machine_test.cc
578 lines (517 loc) · 18.3 KB
/
sandesh_state_machine_test.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
/*
* Copyright (c) 2013 Juniper Networks, Inc. All rights reserved.
*/
//
// sandesh_state_machine_test.cc
//
#include <boost/assign.hpp>
#include <boost/asio.hpp>
#include <boost/foreach.hpp>
#include <boost/asio/ip/tcp.hpp>
#include "base/logging.h"
#include "base/test/task_test_util.h"
#include "io/event_manager.h"
#include "testing/gunit.h"
#include <sandesh/sandesh_types.h>
#include <sandesh/sandesh.h>
#include <sandesh/sandesh_server.h>
#include <sandesh/sandesh_session.h>
#include <sandesh/sandesh_statistics.h>
#include "sandesh_connection.h"
#include "sandesh_state_machine.h"
#include "sandesh_test_common.h"
using namespace std;
using namespace boost::assign;
using namespace boost::posix_time;
using boost::system::error_code;
typedef boost::asio::ip::tcp::endpoint Endpoint;
class SandeshSessionMock : public SandeshSession {
public:
enum State {
SERVER_INIT,
CLOSE,
};
enum Direction {
ACTIVE,
PASSIVE,
};
SandeshSessionMock(TcpServer *server, State state, Direction direction)
: SandeshSession(server, NULL, Task::kTaskInstanceAny,
TaskScheduler::GetInstance()->GetTaskId("sandesh::Test::StateMachine"),
TaskScheduler::GetInstance()->GetTaskId("io::ReaderTask")),
state_(state),
direction_(direction),
defer_reader_(false) {
}
~SandeshSessionMock() {
LOG(DEBUG, "Session delete");
}
virtual bool Send(const u_int8_t *data, size_t size, size_t *sent) {
return true;
}
void Close() {
state_ = SandeshSessionMock::CLOSE;
SandeshSession::Close();
}
virtual void SetDeferReader(bool defer_reader) {
defer_reader_ = defer_reader;
}
virtual bool IsReaderDeferred() const {
return defer_reader_;
}
State state() { return state_; }
Direction direction() { return direction_; }
private:
State state_;
Direction direction_;
bool defer_reader_;
};
class SandeshServerMock : public SandeshServer {
public:
SandeshServerMock(EventManager *evm) :
SandeshServer(evm),
session_(NULL),
old_session_(NULL) {
}
~SandeshServerMock() {
LOG(DEBUG, "Server delete");
}
virtual TcpSession *CreateSession() {
if (session_) {
assert(!old_session_);
old_session_ = session_;
session_ = NULL;
}
assert(!session_);
session_ = new SandeshSessionMock(this,
SandeshSessionMock::SERVER_INIT,
SandeshSessionMock::ACTIVE);
return session_;
}
virtual void DeleteSession(TcpSession *session) {
if (session_ == session) {
session_ = NULL;
} else if (old_session_ == session) {
old_session_ = NULL;
} else {
assert(false);
}
delete static_cast<SandeshSessionMock *>(session);
}
virtual bool ReceiveSandeshMsg(SandeshSession *session,
const SandeshMessage *msg, bool rsc) {
return true;
}
SandeshSessionMock *session() { return session_; }
SandeshSessionMock *old_session() { return old_session_; }
private:
SandeshSessionMock *session_;
SandeshSessionMock *old_session_;
};
class SandeshServerStateMachineTest : public ::testing::Test {
protected:
SandeshServerStateMachineTest() :
server_(new SandeshServerMock(&evm_)),
timer_(TimerManager::CreateTimer(*evm_.io_service(), "Dummy timer")),
connection_(new SandeshServerConnection(server_, dummy_,
Task::kTaskInstanceAny,
TaskScheduler::GetInstance()->GetTaskId("sandesh::Test::StateMachine"))) {
task_util::WaitForIdle();
sm_ = connection_->state_machine();
sm_->set_idle_hold_time(1);
sm_->SetGeneratorKey("Test");
}
~SandeshServerStateMachineTest() {
task_util::WaitForIdle();
connection_->Shutdown();
task_util::WaitForIdle();
delete connection_;
server_->Shutdown();
task_util::WaitForIdle();
TcpServerManager::DeleteServer(server_);
server_ = NULL;
TimerManager::DeleteTimer(timer_);
task_util::WaitForIdle();
evm_.Shutdown();
}
virtual void SetUp() {
}
virtual void TearDown() {
}
bool DummyTimerHandler() {
LOG(DEBUG, "Dummy handler called");
return false;
}
void RunToState(ssm::SsmState state) {
timer_->Start(15000,
boost::bind(&SandeshServerStateMachineTest::DummyTimerHandler, this));
task_util::WaitForIdle();
for (int count = 0; count < 100 && sm_->get_state() != state; count++) {
evm_.RunOnce();
task_util::WaitForIdle();
}
timer_->Cancel();
VerifyState(state);
}
void GetToState(ssm::SsmState state) {
switch (state) {
case ssm::IDLE: {
EvAdminDown();
VerifyState(state);
break;
}
case ssm::ACTIVE: {
GetToState(ssm::IDLE);
EvAdminUp();
RunToState(state);
break;
}
case ssm::SERVER_INIT: {
GetToState(ssm::ACTIVE);
EvTcpPassiveOpen();
VerifyState(ssm::SERVER_INIT);
break;
}
default: {
ASSERT_TRUE(false);
break;
}
}
}
void VerifyState(ssm::SsmState state) {
task_util::WaitForIdle();
TaskScheduler::GetInstance()->Stop();
LOG(DEBUG, "VerifyState " << state);
EXPECT_EQ(state, sm_->get_state());
switch (state) {
case ssm::IDLE:
EXPECT_TRUE(sm_->session() == NULL);
EXPECT_TRUE(connection_->session() == NULL);
break;
case ssm::ACTIVE:
EXPECT_TRUE(!IdleHoldTimerRunning());
EXPECT_TRUE(sm_->session() == NULL);
EXPECT_TRUE(connection_->session() == NULL);
break;
case ssm::SERVER_INIT:
EXPECT_TRUE(!IdleHoldTimerRunning());
EXPECT_TRUE(sm_->session() != NULL);
EXPECT_TRUE(connection_->session() != NULL);
break;
default:
ASSERT_TRUE(false);
break;
}
TaskScheduler::GetInstance()->Start();
}
void VerifyDirection(SandeshSessionMock::Direction direction) {
SandeshSession *session = sm_->connection()->session();
SandeshSessionMock *mock_session = dynamic_cast<SandeshSessionMock *>(session);
ASSERT_TRUE(mock_session != NULL);
EXPECT_TRUE(mock_session->direction() == direction);
}
SandeshSessionMock *GetSession(SandeshSessionMock *session) {
if (session) return session;
session = server_->session();
return session;
}
void EvStart() {
sm_->Initialize();
}
void EvStop() {
sm_->Shutdown();
}
void EvAdminUp() {
connection_->SetAdminState(false);
sm_->set_idle_hold_time(1);
}
void EvAdminDown() {
connection_->SetAdminState(true);
sm_->set_idle_hold_time(1);
}
void EvTcpPassiveOpen() {
TcpSession *session = server_->CreateSession();
SandeshSessionMock *mock_session = dynamic_cast<SandeshSessionMock *>(session);
ASSERT_TRUE(mock_session != NULL);
connection_->AcceptSession(mock_session);
}
void EvTcpClose(SandeshSessionMock *session = NULL) {
if (!session) session = server_->session();
sm_->OnSessionEvent(session, TcpSession::CLOSE);
}
static void CreateMalformedMsg(char *data, size_t &length, int error = 0) {
const char *data1;
if(error == 0) {
data1 = (char *)"<SandeshHeader><Namespace type=\"string\" identifier=\"1\">Test</Namespace><Timestamp type=\"i128\" identifier=\"2\">123456</Timestamp><Type type=\"i32\" identifier=\"8\">2</Type><Hints type=\"i32\" identifier=\"9\">2</Hints></SandeshHeader><FakeSandesh type=\"sandesh\"><str1 type=\"string\" identifier=\"1\">TEST</str1></FakeSandesh>";
} else if(error == 1) {
data1 = (char *)"<SandeshHeader><Namespace type=\"string\" identifier=\"1\">Test</Namespace><Timestamp type=\"i64\" identifier=\"2\">123456</Timestamp><Type type=\"i32\" identifier=\"8\">2</Type><Hints type=\"i32\" identifier=\"9\">2</Hints><open><</SandeshHeader><FakeSandesh type=\"sandesh\"><str1 type=\"string\" identifier=\"1\">TEST</str1><Open></Close></FakeSandesh>";
}
memset(data, 0, length);
memcpy(data, data1, strlen(data1));
length = strlen(data1);
}
void EvSandeshMessageRecv(SandeshSessionMock *session = NULL) {
session = GetSession(session);
uint8_t msg[1024];
contrail::sandesh::test::CreateFakeMessage(msg, sizeof(msg));
string xml((const char *)msg, sizeof(msg));
sm_->OnSandeshMessage(session, xml);
}
void EvInvalidTypeSandeshMessageRecv(SandeshSessionMock *session = NULL) {
session = GetSession(session);
char msg[1024];
size_t length = 1024;
CreateMalformedMsg(msg, length);
string xml((const char *)msg, length);
sm_->OnSandeshMessage(session, xml);
}
void EvMalformedXmlSandeshMessageRecv(SandeshSessionMock *session = NULL) {
session = GetSession(session);
char msg[1024];
size_t length = 1024;
CreateMalformedMsg(msg, length, 1);
string xml((const char *)msg, length);
sm_->OnSandeshMessage(session, xml);
}
SandeshLevel::type MessageDropLevel() const {
return sm_->message_drop_level_;
}
bool IdleHoldTimerRunning() { return sm_->idle_hold_timer_->running(); }
EventManager evm_;
SandeshServerMock *server_;
SandeshStateMachine *sm_;
Timer *timer_;
Endpoint dummy_;
SandeshConnection *connection_;
};
typedef boost::function<void(void)> EvGen;
struct EvGenComp {
bool operator()(const EvGen &lhs, const EvGen &rhs) const {
return &lhs < &rhs;
}
};
TEST_F(SandeshServerStateMachineTest, Matrix) {
boost::asio::io_service::work work(*evm_.io_service());
typedef std::map<EvGen, ssm::SsmState, EvGenComp> Transitions;
#define SERVER_SSM_TRANSITION(F, E) \
((EvGen) boost::bind(&SandeshServerStateMachineTest_Matrix_Test::F, this), E)
#define SERVER_SSM_TRANSITION2(F, E) \
((EvGen) boost::bind(&SandeshServerStateMachineTest_Matrix_Test::F, this, \
(SandeshSessionMock *) NULL), E)
Transitions idle = map_list_of
SERVER_SSM_TRANSITION(EvTcpPassiveOpen, ssm::IDLE)
SERVER_SSM_TRANSITION(EvAdminUp, ssm::ACTIVE);
Transitions active = map_list_of
SERVER_SSM_TRANSITION(EvStop, ssm::IDLE)
SERVER_SSM_TRANSITION(EvAdminDown, ssm::IDLE)
SERVER_SSM_TRANSITION(EvTcpPassiveOpen, ssm::SERVER_INIT)
SERVER_SSM_TRANSITION2(EvTcpClose, ssm::IDLE);
Transitions none = map_list_of
SERVER_SSM_TRANSITION(EvStop, ssm::IDLE);
Transitions server_init = map_list_of
SERVER_SSM_TRANSITION2(EvTcpClose, ssm::IDLE)
SERVER_SSM_TRANSITION2(EvSandeshMessageRecv, ssm::SERVER_INIT)
SERVER_SSM_TRANSITION2(EvTcpClose, ssm::IDLE);
Transitions matrix[] =
{ idle, active, none, server_init };
for (int k = ssm::IDLE; k <= ssm::SERVER_INIT; k++) {
ssm::SsmState i = static_cast<ssm::SsmState> (k);
// Ignore CONNECT, CLIENT_INIT and ESTABLISHED in SandeshServerStateMachine
if (i == ssm::ESTABLISHED) {
continue;
}
int count = 0;
for (Transitions::iterator j = matrix[i].begin();
j != matrix[i].end(); j++) {
LOG(DEBUG, "Starting test " << count++ << " in state " << i
<< " expecting " << j->second << " *********************");
GetToState(i);
j->first();
RunToState(j->second);
}
}
}
TEST_F(SandeshServerStateMachineTest, ReadInvalidTypeMessage) {
// Move to ssm::SERVER_INIT
GetToState(ssm::SERVER_INIT);
// Enqueue 1 message with type i128
EvInvalidTypeSandeshMessageRecv();
SandeshStateMachineStats sm_stats;
SandeshGeneratorStats msg_stats;
int error = 0;
if(sm_->GetStatistics(sm_stats, msg_stats)) {
std::vector<SandeshMessageTypeStats> mtype_stats;
mtype_stats = msg_stats.get_type_stats();
for(int i =0; i < mtype_stats.size(); i++) {
SandeshMessageTypeStats stat = mtype_stats[i];
if(stat.stats.messages_received_dropped_control_msg_failed) {
error = 1;
break;
}
}
}
EXPECT_EQ(1, error);
task_util::WaitForIdle();
}
TEST_F(SandeshServerStateMachineTest, ReadMalformedXmlMessage) {
// Move to ssm::SERVER_INIT
GetToState(ssm::SERVER_INIT);
// Enqueue 1 message with type i128
EvMalformedXmlSandeshMessageRecv();
SandeshStateMachineStats sm_stats;
SandeshGeneratorStats msg_stats;
int error = 0;
if(sm_->GetStatistics(sm_stats, msg_stats)) {
std::vector<SandeshMessageTypeStats> mtype_stats;
mtype_stats = msg_stats.get_type_stats();
for(int i =0; i < mtype_stats.size(); i++) {
SandeshMessageTypeStats stat = mtype_stats[i];
if(stat.stats.messages_received_dropped_decoding_failed) {
error = 1;
break;
}
}
}
EXPECT_EQ(1, error);
task_util::WaitForIdle();
}
TEST_F(SandeshServerStateMachineTest, WaterMark) {
// Set watermarks
std::vector<Sandesh::QueueWaterMarkInfo> wm_info =
boost::assign::tuple_list_of
(1*1024, SandeshLevel::SYS_EMERG, true, true)
(512, SandeshLevel::INVALID, false, true);
for (int i = 0; i < wm_info.size(); i++) {
sm_->SetQueueWaterMarkInfo(wm_info[i]);
}
// Verify initial message drop level and the session reader defer status
EXPECT_EQ(SandeshLevel::INVALID, MessageDropLevel());
EXPECT_TRUE(sm_->session() == NULL);
// Move to ssm::SERVER_INIT
GetToState(ssm::SERVER_INIT);
// Stop the task scheduler
TaskScheduler::GetInstance()->Stop();
// Enqueue 1 message 1024 byte
EvSandeshMessageRecv();
// Verify the message drop level and the session reader defer status
EXPECT_EQ(SandeshLevel::SYS_EMERG, MessageDropLevel());
EXPECT_TRUE(sm_->session()->IsReaderDeferred());
// Start the task scheduler
TaskScheduler::GetInstance()->Start();
task_util::WaitForIdle();
// Verify the message drop level and the session reader defer status
EXPECT_EQ(SandeshLevel::INVALID, MessageDropLevel());
EXPECT_FALSE(sm_->session()->IsReaderDeferred());
}
TEST_F(SandeshServerStateMachineTest, WaterMark2) {
// Set high and low watermarks with level set to INVALID
// and defer undefer also set to true. Verify that the
// session reader defer status is still set/unset
std::vector<Sandesh::QueueWaterMarkInfo> wm_info =
boost::assign::tuple_list_of
(1 * 1024, SandeshLevel::INVALID, true, true)
(512, SandeshLevel::INVALID, false, true);
for (int i = 0; i < wm_info.size(); i++) {
sm_->SetQueueWaterMarkInfo(wm_info[i]);
}
// Verify initial message drop level and the session reader defer status
EXPECT_EQ(SandeshLevel::INVALID, MessageDropLevel());
EXPECT_TRUE(sm_->session() == NULL);
// Move to ssm::SERVER_INIT
GetToState(ssm::SERVER_INIT);
// Stop the task scheduler
TaskScheduler::GetInstance()->Stop();
// Enqueue 1 message 1024 byte
EvSandeshMessageRecv();
// Verify the message drop level and the session reader defer status
EXPECT_EQ(SandeshLevel::INVALID, MessageDropLevel());
EXPECT_TRUE(sm_->session()->IsReaderDeferred());
// Start the task scheduler
TaskScheduler::GetInstance()->Start();
task_util::WaitForIdle();
// Verify the message drop level and the session reader defer status
EXPECT_EQ(SandeshLevel::INVALID, MessageDropLevel());
EXPECT_FALSE(sm_->session()->IsReaderDeferred());
}
TEST_F(SandeshServerStateMachineTest, DeferDequeue) {
// Move to ssm::SERVER_INIT
GetToState(ssm::SERVER_INIT);
// Verify the state machine queue is empty
uint64_t sm_queue_count;
ASSERT_TRUE(sm_->GetQueueCount(sm_queue_count));
EXPECT_EQ(0, sm_queue_count);
// Defer the dequeue
sm_->SetDeferDequeue(true);
// Enqueue 1 message
EvSandeshMessageRecv();
// Verify that state machine queue now has 1 entry
ASSERT_TRUE(sm_->GetQueueCount(sm_queue_count));
EXPECT_EQ(1024, sm_queue_count);
// Verify the state machine state
EXPECT_EQ(ssm::SERVER_INIT, sm_->get_state());
// Undefer the dequeue
sm_->SetDeferDequeue(false);
task_util::WaitForIdle();
// Verify the state machine queue is empty
ASSERT_TRUE(sm_->GetQueueCount(sm_queue_count));
EXPECT_EQ(0, sm_queue_count);
// Verify the state machine state
EXPECT_EQ(ssm::SERVER_INIT, sm_->get_state());
}
class SandeshServerStateMachineIdleTest : public SandeshServerStateMachineTest {
virtual void SetUp() {
GetToState(ssm::IDLE);
}
virtual void TearDown() {
}
};
// Old State: Idle
// Event: EvTcpPassiveOpen
// New State: Idle
TEST_F(SandeshServerStateMachineIdleTest, TcpPassiveOpen) {
TaskScheduler::GetInstance()->Stop();
EvTcpPassiveOpen();
TaskScheduler::GetInstance()->Start();
VerifyState(ssm::IDLE);
EXPECT_TRUE(sm_->session() == NULL);
VerifyState(ssm::IDLE);
// Also verify that we can proceed after this event
GetToState(ssm::ACTIVE);
}
class SandeshServerStateMachineActiveTest : public SandeshServerStateMachineTest {
virtual void SetUp() {
GetToState(ssm::ACTIVE);
}
virtual void TearDown() {
}
};
// Old State: Active
// Event: EvTcpPassiveOpen.
// New State: ServerInit
// Timers: None should be running.
// Messages: None.
TEST_F(SandeshServerStateMachineActiveTest, TcpPassiveOpen) {
EvTcpPassiveOpen();
RunToState(ssm::SERVER_INIT);
EXPECT_TRUE(!IdleHoldTimerRunning());
EXPECT_TRUE(sm_->session() != NULL);
}
// Old State: Active
// Event: EvTcpClose on the session.
// New State: Active
// Timers: None should be running.
// Messages: None.
TEST_F(SandeshServerStateMachineActiveTest, TcpClose) {
EvTcpPassiveOpen();
VerifyState(ssm::SERVER_INIT);
EvTcpClose(server_->session());
VerifyState(ssm::IDLE);
EXPECT_TRUE(sm_->session() == NULL);
}
int main(int argc, char **argv) {
LoggingInit();
::testing::InitGoogleTest(&argc, argv);
int result = RUN_ALL_TESTS();
TaskScheduler::GetInstance()->Terminate();
return result;
}