Skip to content

Commit

Permalink
check for memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
gafferongames committed Dec 26, 2023
1 parent 3b3ba95 commit c78f4e4
Show file tree
Hide file tree
Showing 2 changed files with 190 additions and 1 deletion.
12 changes: 11 additions & 1 deletion soak.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,23 @@ int SoakMain()

Server server( GetDefaultAllocator(), privateKey, serverAddress, config, adapter, time );

server.SetLatency( 1000.0f );
server.SetJitter( 100.0f );
server.SetPacketLoss( 25.0f );
server.SetDuplicates( 25.0f );

server.Start( 1 );

uint64_t clientId = 0;
yojimbo_random_bytes( (uint8_t*) &clientId, 8 );

Client client( GetDefaultAllocator(), Address("0.0.0.0"), config, adapter, time );

client.SetLatency( 1000.0f );
client.SetJitter( 100.0f );
client.SetPacketLoss( 25.0f );
client.SetDuplicates( 25.0f );

client.InsecureConnect( privateKey, clientId, serverAddress );

uint64_t numMessagesSentToServer = 0;
Expand Down Expand Up @@ -104,7 +114,7 @@ int SoakMain()

if ( client.IsConnected() )
{
if ( ( rand() % 10000 ) == 0 )
if ( ( rand() % 100000 ) == 0 )
{
printf( "client reconnect\n" );
client.Disconnect();
Expand Down
179 changes: 179 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2233,6 +2233,183 @@ void test_single_message_type_unreliable()
check( numMessagesReceived == NumMessagesSent );
}


void SendClientToServerMessagesSample( Client & client, int numMessagesToSend, int channelIndex = 0 )
{
for ( int i = 0; i < numMessagesToSend; ++i )
{
if ( !client.CanSendMessage( channelIndex ) )
break;

TestMessage * message = (TestMessage*) client.CreateMessage( TEST_MESSAGE );
check( message );
message->sequence = i;
client.SendMessage( channelIndex, message );
}
}

void SendServerToClientMessagesSample( Server & server, int clientIndex, int numMessagesToSend, int channelIndex = 0 )
{
for ( int i = 0; i < numMessagesToSend; ++i )
{
if ( !server.CanSendMessage( clientIndex, channelIndex ) )
break;

TestMessage * message = (TestMessage*) server.CreateMessage( clientIndex, TEST_MESSAGE );
check( message );
message->sequence = i;
server.SendMessage( clientIndex, channelIndex, message );
}
}

void ProcessServerToClientMessagesSample( Client & client, int & numMessagesReceivedFromServer )
{
while ( true )
{
Message * message = client.ReceiveMessage( 0 );

if ( !message )
break;

switch ( message->GetType() )
{
case TEST_MESSAGE:
{
++numMessagesReceivedFromServer;
}
break;
}

client.ReleaseMessage( message );
}
}

void ProcessClientToServerMessagesSample( Server & server, int clientIndex, int & numMessagesReceivedFromClient )
{
while ( true )
{
Message * message = server.ReceiveMessage( clientIndex, 0 );

if ( !message )
break;

switch ( message->GetType() )
{
case TEST_MESSAGE:
{
check( !message->IsBlockMessage() );
++numMessagesReceivedFromClient;
}
break;
}

server.ReleaseMessage( clientIndex, message );
}
}

void test_client_server_messages_network_sim_leak()
{
const uint64_t clientId = 1;

Address clientAddress( "0.0.0.0", ClientPort );
Address serverAddress( "127.0.0.1", ServerPort );

double time = 100.0;

ClientServerConfig config;
config.networkSimulator = true;
config.channel[0].type = CHANNEL_TYPE_UNRELIABLE_UNORDERED;

Client client( GetDefaultAllocator(), clientAddress, config, adapter, time );

uint8_t privateKey[KeyBytes];
memset( privateKey, 0, KeyBytes );

Server server( GetDefaultAllocator(), privateKey, serverAddress, config, adapter, time );

server.Start( MaxClients );

server.SetLatency( 500 );
server.SetJitter( 100 );
server.SetPacketLoss( 5 );
server.SetDuplicates( 5 );

for ( int iteration = 0; iteration < 2; ++iteration )
{
client.InsecureConnect( privateKey, clientId, serverAddress );

client.SetLatency( 500 );
client.SetJitter( 100 );
client.SetPacketLoss( 5 );
client.SetDuplicates( 5 );

const int NumIterations = 10000;

for ( int i = 0; i < NumIterations; ++i )
{
Client * clients[] = { &client };
Server * servers[] = { &server };

PumpClientServerUpdate( time, clients, 1, servers, 1 );

if ( client.ConnectionFailed() )
break;

if ( !client.IsConnecting() && client.IsConnected() && server.GetNumConnectedClients() == 1 )
break;
}

check( !client.IsConnecting() );
check( client.IsConnected() );
check( server.GetNumConnectedClients() == 1 );
check( client.GetClientIndex() == 0 );
check( server.IsClientConnected(0) );

const int NumMessagesSent = 2000;

SendClientToServerMessagesSample( client, NumMessagesSent );

SendServerToClientMessagesSample( server, client.GetClientIndex(), NumMessagesSent );

int numMessagesReceivedFromClient = 0;
int numMessagesReceivedFromServer = 0;

for ( int i = 0; i < 100; ++i )
{
if ( !client.IsConnected() )
break;

Client * clients[] = { &client };
Server * servers[] = { &server };

PumpClientServerUpdate( time, clients, 1, servers, 1 );

ProcessServerToClientMessagesSample( client, numMessagesReceivedFromServer );
ProcessClientToServerMessagesSample( server, client.GetClientIndex(), numMessagesReceivedFromClient );
}

check( client.IsConnected() );
check( server.IsClientConnected( client.GetClientIndex() ) );

client.Disconnect();

for ( int i = 0; i < NumIterations; ++i )
{
Client * clients[] = { &client };
Server * servers[] = { &server };

PumpClientServerUpdate( time, clients, 1, servers, 1 );

if ( !client.IsConnected() && server.GetNumConnectedClients() == 0 )
break;
}

check( !client.IsConnected() && server.GetNumConnectedClients() == 0 );
}

server.Stop();
}

#define RUN_TEST( test_function ) \
do \
{ \
Expand Down Expand Up @@ -2334,6 +2511,8 @@ int main()
RUN_TEST( test_single_message_type_reliable_blocks );
RUN_TEST( test_single_message_type_unreliable );

RUN_TEST( test_client_server_messages_network_sim_leak );

#if SOAK
if ( quit )
break;
Expand Down

0 comments on commit c78f4e4

Please sign in to comment.