Skip to content

Commit bf7af35

Browse files
committed
Merge branch 'master' into smart_switch_db
Signed-off-by: Ze Gan <[email protected]>
2 parents a9a2602 + ad4d386 commit bf7af35

7 files changed

+49
-0
lines changed

common/configdb.h

+7
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,13 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
5959
self.handlers = {}
6060
self.fire_init_data = {}
6161

62+
def __enter__(self):
63+
return self
64+
65+
def __exit__(self, exc_type, exc_value, exc_tb):
66+
self.close()
67+
pass
68+
6269
@property
6370
def KEY_SEPARATOR(self):
6471
return self.getKeySeparator()

common/dbinterface.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,11 @@ void DBInterface::close(const std::string& dbName)
3232
m_redisClient.erase(dbName);
3333
}
3434

35+
void DBInterface::close()
36+
{
37+
m_redisClient.clear();
38+
}
39+
3540
int64_t DBInterface::del(const string& dbName, const std::string& key, bool blocking)
3641
{
3742
auto innerfunc = [&]

common/dbinterface.h

+1
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class DBInterface
3434
public:
3535
void connect(int dbId, const std::string& dbName, bool retry = true);
3636
void close(const std::string& dbName);
37+
void close();
3738
int64_t del(const std::string& dbName, const std::string& key, bool blocking = false);
3839
// Delete all keys which match %pattern from DB
3940
void delete_all_by_pattern(const std::string& dbName, const std::string& pattern);

common/sonicv2connector.cpp

+5
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ void SonicV2Connector_Native::close(const std::string& db_name)
3434
m_dbintf.close(db_name);
3535
}
3636

37+
void SonicV2Connector_Native::close()
38+
{
39+
m_dbintf.close();
40+
}
41+
3742
std::vector<std::string> SonicV2Connector_Native::get_db_list()
3843
{
3944
return SonicDBConfig::getDbList(m_netns);

common/sonicv2connector.h

+2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ class SonicV2Connector_Native
2020

2121
void close(const std::string& db_name);
2222

23+
void close();
24+
2325
std::vector<std::string> get_db_list();
2426

2527
int get_dbid(const std::string& db_name);

tests/redis_ut.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,7 @@ TEST(DBConnector, DBInterface)
326326
db.set("TEST_DB", "key0", "field1", "value2");
327327
auto fvs = db.get_all("TEST_DB", "key0");
328328
auto rc = fvs.find("field1");
329+
db.close();
329330
EXPECT_NE(rc, fvs.end());
330331
EXPECT_EQ(rc->second, "value2");
331332
}

tests/test_redis_ut.py

+28
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@
77
from swsscommon import swsscommon
88
from swsscommon.swsscommon import ConfigDBPipeConnector, DBInterface, SonicV2Connector, SonicDBConfig, ConfigDBConnector, SonicDBConfig, transpose_pops, SonicDBKey
99
import json
10+
import gc
11+
12+
import sys
13+
if sys.version_info.major == 3:
14+
from unittest import mock
15+
else:
16+
# Expect the 'mock' package for python 2
17+
# https://pypi.python.org/pypi/mock
18+
import mock
1019

1120
def test_ProducerTable():
1221
db = swsscommon.DBConnector("APPL_DB", 0, True)
@@ -804,6 +813,24 @@ def test_ConfigDBConnector():
804813
assert len(allconfig) == 0
805814

806815

816+
@mock.patch("swsscommon.swsscommon.ConfigDBConnector.close")
817+
def test_ConfigDBConnector_with_statement(self):
818+
# test ConfigDBConnector support 'with' statement
819+
with ConfigDBConnector() as config_db:
820+
assert config_db.db_name == ""
821+
assert config_db.TABLE_NAME_SEPARATOR == "|"
822+
config_db.connect(wait_for_init=False)
823+
assert config_db.db_name == "CONFIG_DB"
824+
assert config_db.TABLE_NAME_SEPARATOR == "|"
825+
config_db.get_redis_client(config_db.CONFIG_DB).flushdb()
826+
config_db.set_entry("TEST_PORT", "Ethernet111", {"alias": "etp1x"})
827+
allconfig = config_db.get_config()
828+
assert allconfig["TEST_PORT"]["Ethernet111"]["alias"] == "etp1x"
829+
830+
# check close() method called by with statement
831+
ConfigDBConnector.close.assert_called_once_with()
832+
833+
807834
def test_SmartSwitchDBConnector():
808835
test_dir = os.path.dirname(os.path.abspath(__file__))
809836
global_db_config = os.path.join(test_dir, 'redis_multi_db_ut_config', 'database_global.json')
@@ -821,3 +848,4 @@ def test_SmartSwitchDBConnector():
821848
assert "dputest2" in keys
822849
assert tbl.get("dputest1")[1][0] == ("dashfield1", "dashvalue1")
823850
assert tbl.get("dputest2")[1][1] == ("dashfield2", "dashvalue2")
851+

0 commit comments

Comments
 (0)