From a59fc7626d622d96f1d18c9a8708d06bb1b424b0 Mon Sep 17 00:00:00 2001 From: liuh-80 Date: Mon, 2 Sep 2024 06:41:30 +0000 Subject: [PATCH] Add test case --- tests/mock_tests/consumer_ut.cpp | 98 +++++++++----------- tests/mock_tests/mock_consumerstatetable.cpp | 33 ++++++- 2 files changed, 74 insertions(+), 57 deletions(-) diff --git a/tests/mock_tests/consumer_ut.cpp b/tests/mock_tests/consumer_ut.cpp index 500bf45879..a67b7235c7 100644 --- a/tests/mock_tests/consumer_ut.cpp +++ b/tests/mock_tests/consumer_ut.cpp @@ -1,21 +1,36 @@ + #include "ut_helper.h" #include "mock_orchagent_main.h" #include "mock_table.h" - #include - extern PortsOrch *gPortsOrch; - namespace consumer_test { using namespace std; + class TestOrch : public Orch + { + public: + TestOrch(swss::DBConnector *db, string tableName) + :Orch(db, tableName), + m_notification_count(0) + { + } + + void doTask(Consumer& consumer) + { + std::cout << "TestOrch::doTask " << consumer.m_toSync.size() << std::endl; + m_notification_count += consumer.m_toSync.size(); + consumer.m_toSync.clear(); + } + + long m_notification_count; + }; struct ConsumerTest : public ::testing::Test { shared_ptr m_app_db; shared_ptr m_config_db; shared_ptr m_state_db; - string key = "key"; string f1 = "field1"; string v1a = "value1_a"; @@ -26,10 +41,8 @@ namespace consumer_test string f3 = "field3"; string v3a = "value3_a"; KeyOpFieldsValuesTuple exp_kofv; - unique_ptr consumer; deque kofv_q; - ConsumerTest() { // FIXME: move out from constructor @@ -39,17 +52,14 @@ namespace consumer_test consumer = unique_ptr(new Consumer( new swss::ConsumerStateTable(m_config_db.get(), "CFG_TEST_TABLE", 1, 1), gPortsOrch, "CFG_TEST_TABLE")); } - virtual void SetUp() override { ::testing_db::reset(); } - virtual void TearDown() override { ::testing_db::reset(); } - void validate_syncmap(SyncMap &sync, uint16_t exp_sz, std::string exp_key, KeyOpFieldsValuesTuple exp_kofv) { // verify the content in syncMap @@ -58,7 +68,6 @@ namespace consumer_test while (it != sync.end()) { KeyOpFieldsValuesTuple t = it->second; - string itkey = kfvKey(t); if (itkey == exp_key) { ASSERT_EQ(t, exp_kofv); @@ -71,23 +80,19 @@ namespace consumer_test ASSERT_EQ(sync.size(), exp_sz-1); } }; - TEST_F(ConsumerTest, ConsumerAddToSync_Set) { - // Test case, one set_command auto entry = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - kofv_q.push_back(entry); consumer->addToSync(kofv_q); exp_kofv = entry; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } - TEST_F(ConsumerTest, ConsumerAddToSync_Del) { // Test case, one with del_command @@ -95,15 +100,11 @@ namespace consumer_test { key, DEL_COMMAND, { { } } }); - kofv_q.push_back(entry); consumer->addToSync(kofv_q); - exp_kofv = entry; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); - } - TEST_F(ConsumerTest, ConsumerAddToSync_Set_Del) { // Test case, add SET then DEL @@ -112,50 +113,41 @@ namespace consumer_test SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, DEL_COMMAND, { { } } }); - kofv_q.push_back(entrya); kofv_q.push_back(entryb); consumer->addToSync(kofv_q); - // expect only DEL exp_kofv = entryb; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } - TEST_F(ConsumerTest, ConsumerAddToSync_Del_Set) { auto entrya = KeyOpFieldsValuesTuple( { key, DEL_COMMAND, { { } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - // Test case, add DEL then SET, re-try 100 times, order should be kept for (auto x = 0; x < 100; x++) { kofv_q.push_back(entrya); kofv_q.push_back(entryb); consumer->addToSync(kofv_q); - // expect DEL then SET exp_kofv = entrya; validate_syncmap(consumer->m_toSync, 2, key, exp_kofv); - exp_kofv = entryb; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } } - TEST_F(ConsumerTest, ConsumerAddToSync_Set_Del_Set_Multi) { // Test5, add SET, DEL then SET, re-try 100 times , order should be kept @@ -164,34 +156,28 @@ namespace consumer_test SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, DEL_COMMAND, { { } } }); - auto entryc = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - for (auto x = 0; x < 100; x++) { kofv_q.push_back(entrya); kofv_q.push_back(entryb); kofv_q.push_back(entryc); consumer->addToSync(kofv_q); - // expect DEL then SET exp_kofv = entryb; validate_syncmap(consumer->m_toSync, 2, key, exp_kofv); - exp_kofv = entryc; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } } - TEST_F(ConsumerTest, ConsumerAddToSync_Set_Del_Set_Multi_In_Q) { // Test5, add SET, DEL then SET, repeat 100 times in queue, final result and order should be kept @@ -200,18 +186,15 @@ namespace consumer_test SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, DEL_COMMAND, { { } } }); - auto entryc = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - for (auto x = 0; x < 100; x++) { kofv_q.push_back(entrya); @@ -219,15 +202,12 @@ namespace consumer_test kofv_q.push_back(entryc); } consumer->addToSync(kofv_q); - // expect DEL then SET exp_kofv = entryb; validate_syncmap(consumer->m_toSync, 2, key, exp_kofv); - exp_kofv = entryc; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } - TEST_F(ConsumerTest, ConsumerAddToSync_Del_Set_Setnew) { // Test case, DEL, SET, then SET with different value @@ -235,32 +215,26 @@ namespace consumer_test { key, DEL_COMMAND, { { } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - auto entryc = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1b }, { f2, v2b } } }); - kofv_q.push_back(entrya); kofv_q.push_back(entryb); kofv_q.push_back(entryc); consumer->addToSync(kofv_q); - // expect DEL then SET with new values exp_kofv = entrya; validate_syncmap(consumer->m_toSync, 2, key, exp_kofv); - exp_kofv = entryc; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } - TEST_F(ConsumerTest, ConsumerAddToSync_Del_Set_Setnew1) { // Test case, DEL, SET, then SET with new values and new fields @@ -268,38 +242,31 @@ namespace consumer_test { key, DEL_COMMAND, { { } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - auto entryc = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f1, v1b }, { f3, v3a } } }); - kofv_q.push_back(entrya); kofv_q.push_back(entryb); kofv_q.push_back(entryc); consumer->addToSync(kofv_q); - // expect DEL then SET with new values and new fields exp_kofv = entrya; validate_syncmap(consumer->m_toSync, 2, key, exp_kofv); - exp_kofv = KeyOpFieldsValuesTuple( { key, SET_COMMAND, { { f2, v2a }, { f1, v1b }, { f3, v3a } } }); - validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); } - TEST_F(ConsumerTest, ConsumerAddToSync_Ind_Set_Del) { // Test case, Add individuals by addToSync, SET then DEL @@ -308,18 +275,37 @@ namespace consumer_test SET_COMMAND, { { f1, v1a }, { f2, v2a } } }); - auto entryb = KeyOpFieldsValuesTuple( { key, DEL_COMMAND, { { } } }); - consumer->addToSync(entrya); consumer->addToSync(entryb); - // expect only DEL exp_kofv = entryb; validate_syncmap(consumer->m_toSync, 1, key, exp_kofv); - + } + TEST_F(ConsumerTest, ConsumerPops_notification_count) + { + int consumer_pops_batch_size = 10; + TestOrch test_orch(m_config_db.get(), "CFG_TEST_TABLE"); + Consumer test_consumer( + new swss::ConsumerStateTable(m_config_db.get(), "CFG_TEST_TABLE", consumer_pops_batch_size, 1), &test_orch, "CFG_TEST_TABLE"); + swss::ProducerStateTable producer_table(m_config_db.get(), "CFG_TEST_TABLE"); + m_config_db->flushdb(); + for (int notification_count = 0; notification_count< consumer_pops_batch_size*2; notification_count++) + { + std::vector fields; + FieldValueTuple t("test_field", "test_value"); + fields.push_back(t); + producer_table.set(std::to_string(notification_count), fields); + + cout << "ConsumerPops_notification_count:: add key: " << notification_count << endl; + } + // consumer should pops consumer_pops_batch_size notifications + test_consumer.execute(); + ASSERT_EQ(test_orch.m_notification_count, consumer_pops_batch_size); + test_consumer.execute(); + ASSERT_EQ(test_orch.m_notification_count, consumer_pops_batch_size*2); } } diff --git a/tests/mock_tests/mock_consumerstatetable.cpp b/tests/mock_tests/mock_consumerstatetable.cpp index 822727929a..5f8f8fbdb7 100644 --- a/tests/mock_tests/mock_consumerstatetable.cpp +++ b/tests/mock_tests/mock_consumerstatetable.cpp @@ -1,5 +1,6 @@ -#include "consumerstatetable.h" +#include "consumerstatetable.h" +#include namespace swss { ConsumerStateTable::ConsumerStateTable(DBConnector *db, const std::string &tableName, int popBatchSize, int pri) : @@ -7,4 +8,34 @@ namespace swss TableName_KeySet(tableName) { } + + void ConsumerStateTable::pops(std::deque &vkco, const std::string& /*prefix*/) + { + int count = 0; + swss::Table table(getDbConnector(), getTableName()); + std::vector keys; + table.getKeys(keys); + for (const auto &key: keys) + { + // pop with batch size + if (count < POP_BATCH_SIZE) + { + count++; + } + else + { + break; + } + + KeyOpFieldsValuesTuple kco; + kfvKey(kco) = key; + kfvOp(kco) = SET_COMMAND; + if (!table.get(key, kfvFieldsValues(kco))) + { + continue; + } + table.del(key); + vkco.push_back(kco); + } + } }