Skip to content

Commit

Permalink
prepare gtest for naming service test
Browse files Browse the repository at this point in the history
  • Loading branch information
zerotacg committed Jun 8, 2024
1 parent 4b6707e commit ddee279
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 164 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,11 @@ snowballs/build/*
ryzom/build/*
build/*
build-2010/*
build/*
install/*
build_*
install_*
nel/tools/build_gamedata/configuration/buildsite.py
cmake-build-*

# Linux nel compile
nel/build/nel-config
Expand Down
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,17 @@ ENDIF()
IF(WITH_NEL)
IF(WITH_NEL_TESTS)
FIND_PACKAGE(CppTest)

ENABLE_TESTING()
INCLUDE(FetchContent)
FetchContent_Declare(
googletest
URL https://github.com/google/googletest/archive/a7f443b80b105f940225332ed3c31f2790092f47.zip
EXCLUDE_FROM_ALL
)
SET(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)
INCLUDE(GoogleTest)
ENDIF()

IF(HUNTER_ENABLED)
Expand Down
3 changes: 3 additions & 0 deletions nelns/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ IF(WITH_NELNS_SERVER)
ADD_SUBDIRECTORY(admin_executor_service)
ADD_SUBDIRECTORY(admin_service)
ADD_SUBDIRECTORY(naming_service)
IF (WITH_NEL_TESTS)
ADD_SUBDIRECTORY(tests)
ENDIF ()
ADD_SUBDIRECTORY(login_service)
ADD_SUBDIRECTORY(welcome_service)
ENDIF()
Expand Down
6 changes: 3 additions & 3 deletions nelns/naming_service/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
FILE(GLOB SRC *.cpp *.h)

ADD_EXECUTABLE(naming_service WIN32 ${SRC})
ADD_EXECUTABLE(naming_service WIN32
naming_service.cpp
)

TARGET_LINK_LIBRARIES(naming_service
nelmisc
Expand Down
14 changes: 14 additions & 0 deletions nelns/tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
set(TEST_NAME "naming_service_test")

add_executable("${TEST_NAME}"
naming_service.test.cpp
)
target_link_libraries("${TEST_NAME}"
GTest::gtest_main
nelmisc
nelnet
)
gtest_discover_tests("${TEST_NAME}"
EXTRA_ARGS "--gtest_output=xml:${CMAKE_CURRENT_BINARY_DIR}/reports/junit-${TEST_NAME}.xml"
)

6 changes: 6 additions & 0 deletions nelns/tests/naming_service.test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <gtest/gtest.h>

TEST(CNamingService, BasicAssertions) {
EXPECT_STRNE("hello", "world");
EXPECT_EQ(7 * 6, 42);
}
161 changes: 1 addition & 160 deletions ryzom/server/src/ryzom_naming_service/ryzom_naming_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,167 +97,8 @@ void foo()
admin_modules_forceLink();
}

/**
* Manager for services instances
* (Moved from the TICKS to the NS)
* Implementable with layer 5, here implemented in NS (layer 3)
* \author Olivier Cado
* \author Nevrax France
* \date 2003
*/
class CServiceInstanceManager
{
public:

/// Constructor
CServiceInstanceManager();

/** Add the name of a service which must not be duplicated
* If uniqueOnShard is true, only one service is allowed.
* If uniqueOnShard is false, one service is allowed by physical machine.
*/
void addUniqueService( const std::string& serviceName, bool uniqueOnShard )
{
_UniqueServices.insert( std::make_pair( serviceName, uniqueOnShard ) );
}

/// Check if a service is allowed to start (if so, add it)
bool queryStartService( const std::string& serviceName, TServiceId serviceId, const std::vector<NLNET::CInetAddress> &addr, string& reason );

/// Release a service instance
void releaseService( NLNET::TServiceId serviceId );

/// Display information
void displayInfo( NLMISC::CLog *log = NLMISC::InfoLog ) const;

/// Make all controlled services quit
void killAllServices();

private:

/// List of restricted services
std::map< std::string, bool > _UniqueServices;

/// List of granted (online) services
std::set< TServiceId > _OnlineServices;
};


CServiceInstanceManager *SIMInstance = NULL;


/*
* Constructor
*/
CServiceInstanceManager::CServiceInstanceManager()
{
nlassert( ! SIMInstance );
SIMInstance = this;

// Note: addCallbackArray() done in CRangeMirrorManager::init()
}


/*
* Check if a service is allowed to start. Answer with a GSTS (Grant Start Service) message
*/
bool CServiceInstanceManager::queryStartService( const std::string& serviceName, TServiceId serviceId, const vector<CInetAddress> &addr, string& reason )
{
bool grantStarting = true;
std::map< std::string, bool >::iterator ius = _UniqueServices.find( serviceName );
if ( ius != _UniqueServices.end() )
{
// Service is restricted
set< TServiceId >::iterator ios;
bool uniqueOnShard = (*ius).second;
for ( ios=_OnlineServices.begin(); ios!=_OnlineServices.end(); ++ios )
{
string name = getServiceName( *ios );
if ( name == serviceName )
{
if ( uniqueOnShard )
{
// Only one service by shard is allowed => deny
grantStarting = false;
reason = toString( "Service %s already found as %hu, must be unique on shard", serviceName.c_str(), ios->get() );
nlinfo( reason.c_str() );
break;
}
else
{
// Only one service by physical machine is allowed

// Implementation for layer5
//TSockId hostid1, hostid2;
/*CCallbackNetBase *cnb1 = CUnifiedNetwork::getInstance()->getNetBase( serviceId, hostid1 );
CCallbackNetBase *cnb2 = CUnifiedNetwork::getInstance()->getNetBase( *ios, hostid2 );
if ( cnb1->hostAddress( hostid1 ).internalIPAddress() == cnb2->hostAddress( hostid2 ).internalIPAddress() )*/

// Implementation for NS
if ( addr[0].getAddress() == getHostAddress( *ios ).getAddress() )
{
grantStarting = false;
reason = toString( "Service %s already found as %hu on same machine", serviceName.c_str(), ios->get() );
nlinfo( reason.c_str() );
break;
}
}
}
}
}

if ( grantStarting )
{
_OnlineServices.insert( serviceId );
}
return grantStarting;
}


/*
* Release a service instance
*/
void CServiceInstanceManager::releaseService( NLNET::TServiceId serviceId )
{
_OnlineServices.erase( serviceId ); // not a problem if not found
}


/*
* Display information
*/
void CServiceInstanceManager::displayInfo( NLMISC::CLog *log ) const
{
log->displayNL( "Restricted services:" );
std::map< std::string, bool >::const_iterator ius;
for ( ius=_UniqueServices.begin(); ius!=_UniqueServices.end(); ++ius )
{
log->displayNL( "%s -> only one per %s", (*ius).first.c_str(), (*ius).second?"shard":"machine" );
}
log->displayNL( "Online registered services:" );
std::set< TServiceId >::const_iterator ios;
for ( ios=_OnlineServices.begin(); ios!=_OnlineServices.end(); ++ios )
{
log->displayNL( "%s", CUnifiedNetwork::getInstance()->getServiceUnifiedName( *ios ).c_str() );
}
}


/*
* Make all controlled services quit
*/
void CServiceInstanceManager::killAllServices()
{
// Send to all known online services
std::set< TServiceId >::const_iterator ios;
for ( ios=_OnlineServices.begin(); ios!=_OnlineServices.end(); ++ios )
{
doUnregisterService( (TServiceId)(*ios) );
}
}



//
// Variables
//
Expand All @@ -267,7 +108,7 @@ list<CServiceEntry> RegisteredServices; /// List of all registred services
uint16 MinBasePort = 30000; /// Ports begin at 51000
uint16 MaxBasePort = 30100; /// (note: in this implementation there can be no more than 100 services)

const TServiceId BaseSId(128); /// Allocated SIds begin at 128 (except for Agent Service)
/// Allocated SIds begin at 128 (except for Agent Service)

const TTime UnregisterTimeout = 10000; /// After 10s we remove an unregister service if every server didn't ACK the message

Expand Down

0 comments on commit ddee279

Please sign in to comment.