From 793f9bd15e326858611c33dff56ddacfead3e4f7 Mon Sep 17 00:00:00 2001 From: Danny Allen Date: Mon, 12 Oct 2020 17:04:48 -0700 Subject: [PATCH] [mock tests] Update MockDBConnector to match new swsscommon interface (#1465) Signed-off-by: Danny Allen --- tests/mock_tests/mock_dbconnector.cpp | 50 ++++++++++++--------------- 1 file changed, 22 insertions(+), 28 deletions(-) diff --git a/tests/mock_tests/mock_dbconnector.cpp b/tests/mock_tests/mock_dbconnector.cpp index 1377d2937d..362e1446d3 100644 --- a/tests/mock_tests/mock_dbconnector.cpp +++ b/tests/mock_tests/mock_dbconnector.cpp @@ -9,33 +9,25 @@ namespace swss { - DBConnector::~DBConnector() - { - close(m_conn->fd); - if (m_conn->connection_type == REDIS_CONN_TCP) - free(m_conn->tcp.host); - else - free(m_conn->unix_sock.path); - free(m_conn); - } - DBConnector::DBConnector(int dbId, const std::string &hostname, int port, unsigned int timeout) : m_dbId(dbId) { - m_conn = (redisContext *)calloc(1, sizeof(redisContext)); - m_conn->connection_type = REDIS_CONN_TCP; - m_conn->tcp.host = strdup(hostname.c_str()); - m_conn->tcp.port = port; - m_conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + auto conn = (redisContext *)calloc(1, sizeof(redisContext)); + conn->connection_type = REDIS_CONN_TCP; + conn->tcp.host = strdup(hostname.c_str()); + conn->tcp.port = port; + conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + setContext(conn); } DBConnector::DBConnector(int dbId, const std::string &unixPath, unsigned int timeout) : m_dbId(dbId) { - m_conn = (redisContext *)calloc(1, sizeof(redisContext)); - m_conn->connection_type = REDIS_CONN_UNIX; - m_conn->unix_sock.path = strdup(unixPath.c_str()); - m_conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + auto conn = (redisContext *)calloc(1, sizeof(redisContext)); + conn->connection_type = REDIS_CONN_UNIX; + conn->unix_sock.path = strdup(unixPath.c_str()); + conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + setContext(conn); } DBConnector::DBConnector(const std::string& dbName, unsigned int timeout, bool isTcpConn) @@ -46,18 +38,20 @@ namespace swss m_dbId = swss::SonicDBConfig::getDbId(dbName); if (isTcpConn) { - m_conn = (redisContext *)calloc(1, sizeof(redisContext)); - m_conn->connection_type = REDIS_CONN_TCP; - m_conn->tcp.host = strdup(swss::SonicDBConfig::getDbHostname(dbName).c_str()); - m_conn->tcp.port = swss::SonicDBConfig::getDbPort(dbName); - m_conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + auto conn = (redisContext *)calloc(1, sizeof(redisContext)); + conn->connection_type = REDIS_CONN_TCP; + conn->tcp.host = strdup(swss::SonicDBConfig::getDbHostname(dbName).c_str()); + conn->tcp.port = swss::SonicDBConfig::getDbPort(dbName); + conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + setContext(conn); } else { - m_conn = (redisContext *)calloc(1, sizeof(redisContext)); - m_conn->connection_type = REDIS_CONN_UNIX; - m_conn->unix_sock.path = strdup(swss::SonicDBConfig::getDbSock(dbName).c_str()); - m_conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + auto conn = (redisContext *)calloc(1, sizeof(redisContext)); + conn->connection_type = REDIS_CONN_UNIX; + conn->unix_sock.path = strdup(swss::SonicDBConfig::getDbSock(dbName).c_str()); + conn->fd = socket(AF_UNIX, SOCK_DGRAM, 0); + setContext(conn); } }