Skip to content

Commit

Permalink
Add SonicDBConfig::reset method (sonic-net#843)
Browse files Browse the repository at this point in the history
#### Why I did it
sonic-gnmi will use swsscommon API to read database configuration, and sonic-gnmi unit test needs to reload database configuration in the same process.

#### How I did it
Add SonicDBConfig::reset method, and sonic-gnmi unit test will use this method to clear previous database configuration.

#### How to verify it
Pass all UT and E2E test cases.

### Description for the changelog
Add SonicDBConfig::reset method
  • Loading branch information
ganglyu authored Dec 15, 2023
1 parent ab3ce86 commit b2480ad
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
14 changes: 13 additions & 1 deletion common/dbconnector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,18 @@ void SonicDBConfig::initialize(const string &file)
m_init = true;
}

// This API is used to reset the SonicDBConfig class.
// And then user can call initialize with different config file.
void SonicDBConfig::reset()
{
std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex);
m_init = false;
m_global_init = false;
m_inst_info.clear();
m_db_info.clear();
m_db_separator.clear();
}

void SonicDBConfig::validateNamespace(const string &netns)
{
std::lock_guard<std::recursive_mutex> guard(m_db_info_mutex);
Expand Down Expand Up @@ -927,4 +939,4 @@ map<string, map<string, map<string, string>>> DBConnector::getall()
}
}
return data;
}
}
1 change: 1 addition & 0 deletions common/dbconnector.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ class SonicDBConfig
SonicDBConfig.initializeGlobalConfig(global_db_file_path)
%}
#endif
static void reset();

static void validateNamespace(const std::string &netns);
static std::string getDbInst(const std::string &dbName, const std::string &netns = EMPTY_NAMESPACE);
Expand Down
13 changes: 13 additions & 0 deletions tests/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ class SwsscommonEnvironment : public ::testing::Environment {
EXPECT_TRUE(strstr(e.what(), "Namespace invalid is not a valid namespace name in config file"));
}

// reset SonicDBConfig, init should be false
SonicDBConfig::reset();
cout<<"RESET: isInit = "<<SonicDBConfig::isInit()<<endl;
EXPECT_FALSE(SonicDBConfig::isInit());
EXPECT_FALSE(SonicDBConfig::isGlobalInit());

// reinitialize SonicDBConfig, init should be true
SonicDBConfig::initialize(existing_file);
cout<<"INIT: load local db config file, isInit = "<<SonicDBConfig::isInit()<<endl;
EXPECT_TRUE(SonicDBConfig::isInit());
SonicDBConfig::initializeGlobalConfig(global_existing_file);
cout<<"INIT: load global db config file, isInit = "<<SonicDBConfig::isGlobalInit()<<endl;
EXPECT_TRUE(SonicDBConfig::isGlobalInit());
}
};

Expand Down

0 comments on commit b2480ad

Please sign in to comment.