Skip to content

Commit

Permalink
Add support of 'with' statement to ConfigDBConnector (#838)
Browse files Browse the repository at this point in the history
#### Why I did it
Add support of 'with' statement to ConfigDBConnector

#### How I did it
Add __enter__ and __exit__ method to ConfigDBConnector

##### Work item tracking
- Microsoft ADO: 24222755

#### How to verify it
Pass all UT and E2E test cases.
Add new UT to check ConfigDBConnector support 'with' statement

#### Which release branch to backport (provide reason below if selected)

<!--
- Note we only backport fixes to a release branch, *not* features!
- Please also provide a reason for the backporting below.
- e.g.
- [x] 202006
-->

- [ ] 201811
- [ ] 201911
- [ ] 202006
- [ ] 202012
- [ ] 202106
- [ ] 202111

#### Description for the changelog
Add support of 'with' statement to ConfigDBConnector

#### Link to config_db schema for YANG module changes
<!--
Provide a link to config_db schema for the table for which YANG model
is defined
Link should point to correct section on https://github.com/Azure/SONiC/wiki/Configuration.
-->

#### A picture of a cute animal (not mandatory but encouraged)
  • Loading branch information
liuh-80 authored Jan 19, 2024
1 parent 2711f6f commit ad4d386
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 1 deletion.
7 changes: 7 additions & 0 deletions common/configdb.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ class ConfigDBConnector_Native : public SonicV2Connector_Native
self.handlers = {}
self.fire_init_data = {}

def __enter__(self):
return self

def __exit__(self, exc_type, exc_value, exc_tb):
self.close()
pass

@property
def KEY_SEPARATOR(self):
return self.getKeySeparator()
Expand Down
5 changes: 5 additions & 0 deletions common/dbinterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ void DBInterface::close(const std::string& dbName)
m_redisClient.erase(dbName);
}

void DBInterface::close()
{
m_redisClient.clear();
}

int64_t DBInterface::del(const string& dbName, const std::string& key, bool blocking)
{
auto innerfunc = [&]
Expand Down
1 change: 1 addition & 0 deletions common/dbinterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class DBInterface
public:
void connect(int dbId, const std::string& dbName, bool retry = true);
void close(const std::string& dbName);
void close();
int64_t del(const std::string& dbName, const std::string& key, bool blocking = false);
// Delete all keys which match %pattern from DB
void delete_all_by_pattern(const std::string& dbName, const std::string& pattern);
Expand Down
5 changes: 5 additions & 0 deletions common/sonicv2connector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ void SonicV2Connector_Native::close(const std::string& db_name)
m_dbintf.close(db_name);
}

void SonicV2Connector_Native::close()
{
m_dbintf.close();
}

std::vector<std::string> SonicV2Connector_Native::get_db_list()
{
return SonicDBConfig::getDbList(m_netns);
Expand Down
2 changes: 2 additions & 0 deletions common/sonicv2connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class SonicV2Connector_Native

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

void close();

std::vector<std::string> get_db_list();

int get_dbid(const std::string& db_name);
Expand Down
1 change: 1 addition & 0 deletions tests/redis_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ TEST(DBConnector, DBInterface)
db.set("TEST_DB", "key0", "field1", "value2");
auto fvs = db.get_all("TEST_DB", "key0");
auto rc = fvs.find("field1");
db.close();
EXPECT_NE(rc, fvs.end());
EXPECT_EQ(rc->second, "value2");
}
Expand Down
29 changes: 28 additions & 1 deletion tests/test_redis_ut.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@
from swsscommon import swsscommon
from swsscommon.swsscommon import ConfigDBPipeConnector, DBInterface, SonicV2Connector, SonicDBConfig, ConfigDBConnector, SonicDBConfig, transpose_pops
import json
import gc

import sys
if sys.version_info.major == 3:
from unittest import mock
else:
# Expect the 'mock' package for python 2
# https://pypi.python.org/pypi/mock
import mock

def test_ProducerTable():
db = swsscommon.DBConnector("APPL_DB", 0, True)
Expand Down Expand Up @@ -801,4 +810,22 @@ def test_ConfigDBConnector():
allconfig["PORT_TABLE"] = None
config_db.mod_config(allconfig)
allconfig = config_db.get_config()
assert len(allconfig) == 0
assert len(allconfig) == 0


@mock.patch("swsscommon.swsscommon.ConfigDBConnector.close")
def test_ConfigDBConnector_with_statement(self):
# test ConfigDBConnector support 'with' statement
with ConfigDBConnector() as config_db:
assert config_db.db_name == ""
assert config_db.TABLE_NAME_SEPARATOR == "|"
config_db.connect(wait_for_init=False)
assert config_db.db_name == "CONFIG_DB"
assert config_db.TABLE_NAME_SEPARATOR == "|"
config_db.get_redis_client(config_db.CONFIG_DB).flushdb()
config_db.set_entry("TEST_PORT", "Ethernet111", {"alias": "etp1x"})
allconfig = config_db.get_config()
assert allconfig["TEST_PORT"]["Ethernet111"]["alias"] == "etp1x"

# check close() method called by with statement
ConfigDBConnector.close.assert_called_once_with()

0 comments on commit ad4d386

Please # to comment.