Permalink
Browse files

proper fix for single message type bug with basic tests

  • Loading branch information...
FredGithub committed Oct 26, 2018
1 parent dddc9ce commit f8a564e0ead8951000b9295fb0eeed8d453e72c5
Showing with 241 additions and 1 deletion.
  1. +20 −0 shared.h
  2. +213 −0 test.cpp
  3. +8 −1 yojimbo.cpp
View
@@ -148,6 +148,26 @@ YOJIMBO_MESSAGE_FACTORY_START( TestMessageFactory, NUM_TEST_MESSAGE_TYPES );
YOJIMBO_DECLARE_MESSAGE_TYPE( TEST_EXHAUST_STREAM_ALLOCATOR_ON_READ_MESSAGE, TestExhaustStreamAllocatorOnReadMessage );
YOJIMBO_MESSAGE_FACTORY_FINISH();
enum SingleTestMessageType
{
SINGLE_TEST_MESSAGE,
NUM_SINGLE_TEST_MESSAGE_TYPES
};
YOJIMBO_MESSAGE_FACTORY_START( SingleTestMessageFactory, NUM_SINGLE_TEST_MESSAGE_TYPES );
YOJIMBO_DECLARE_MESSAGE_TYPE( SINGLE_TEST_MESSAGE, TestMessage );
YOJIMBO_MESSAGE_FACTORY_FINISH();
enum SingleBlockTestMessageType
{
SINGLE_BLOCK_TEST_MESSAGE,
NUM_SINGLE_BLOCK_TEST_MESSAGE_TYPES
};
YOJIMBO_MESSAGE_FACTORY_START( SingleBlockTestMessageFactory, NUM_SINGLE_BLOCK_TEST_MESSAGE_TYPES );
YOJIMBO_DECLARE_MESSAGE_TYPE( SINGLE_BLOCK_TEST_MESSAGE, TestBlockMessage );
YOJIMBO_MESSAGE_FACTORY_FINISH();
class TestAdapter : public Adapter
{
public:
View
213 test.cpp
@@ -2237,6 +2237,216 @@ void test_reliable_fragment_overflow_bug() {
server.Stop();
}
// Github Issue #77
void test_single_message_type_reliable()
{
SingleTestMessageFactory messageFactory( GetDefaultAllocator() );
double time = 100.0;
ConnectionConfig connectionConfig;
Connection sender( GetDefaultAllocator(), messageFactory, connectionConfig, time );
Connection receiver( GetDefaultAllocator(), messageFactory, connectionConfig, time );
const int NumMessagesSent = 64;
for ( int i = 0; i < NumMessagesSent; ++i )
{
TestMessage * message = (TestMessage*) messageFactory.CreateMessage( SINGLE_TEST_MESSAGE );
check( message );
message->sequence = i;
sender.SendMessage( 0, message );
}
const int SenderPort = 10000;
const int ReceiverPort = 10001;
Address senderAddress( "::1", SenderPort );
Address receiverAddress( "::1", ReceiverPort );
int numMessagesReceived = 0;
const int NumIterations = 1000;
uint16_t senderSequence = 0;
uint16_t receiverSequence = 0;
for ( int i = 0; i < NumIterations; ++i )
{
PumpConnectionUpdate( connectionConfig, time, sender, receiver, senderSequence, receiverSequence );
while ( true )
{
Message * message = receiver.ReceiveMessage( 0 );
if ( !message )
break;
check( message->GetId() == (int) numMessagesReceived );
check( message->GetType() == SINGLE_TEST_MESSAGE );
TestMessage * testMessage = (TestMessage*) message;
check( testMessage->sequence == numMessagesReceived );
++numMessagesReceived;
messageFactory.ReleaseMessage( message );
}
if ( numMessagesReceived == NumMessagesSent )
break;
}
check( numMessagesReceived == NumMessagesSent );
}
void test_single_message_type_reliable_blocks()
{
SingleBlockTestMessageFactory messageFactory( GetDefaultAllocator() );
double time = 100.0;
ConnectionConfig connectionConfig;
Connection sender( GetDefaultAllocator(), messageFactory, connectionConfig, time );
Connection receiver( GetDefaultAllocator(), messageFactory, connectionConfig, time );
const int NumMessagesSent = 32;
for ( int i = 0; i < NumMessagesSent; ++i )
{
TestBlockMessage * message = (TestBlockMessage*) messageFactory.CreateMessage( SINGLE_BLOCK_TEST_MESSAGE );
check( message );
message->sequence = i;
const int blockSize = 1 + ( ( i * 901 ) % 3333 );
uint8_t * blockData = (uint8_t*) YOJIMBO_ALLOCATE( messageFactory.GetAllocator(), blockSize );
for ( int j = 0; j < blockSize; ++j )
blockData[j] = i + j;
message->AttachBlock( messageFactory.GetAllocator(), blockData, blockSize );
sender.SendMessage( 0, message );
}
const int SenderPort = 10000;
const int ReceiverPort = 10001;
Address senderAddress( "::1", SenderPort );
Address receiverAddress( "::1", ReceiverPort );
int numMessagesReceived = 0;
uint16_t senderSequence = 0;
uint16_t receiverSequence = 0;
const int NumIterations = 10000;
for ( int i = 0; i < NumIterations; ++i )
{
PumpConnectionUpdate( connectionConfig, time, sender, receiver, senderSequence, receiverSequence );
while ( true )
{
Message * message = receiver.ReceiveMessage( 0 );
if ( !message )
break;
check( message->GetId() == (int) numMessagesReceived );
check( message->GetType() == SINGLE_BLOCK_TEST_MESSAGE );
TestBlockMessage * blockMessage = (TestBlockMessage*) message;
check( blockMessage->sequence == uint16_t( numMessagesReceived ) );
const int blockSize = blockMessage->GetBlockSize();
check( blockSize == 1 + ( ( numMessagesReceived * 901 ) % 3333 ) );
const uint8_t * blockData = blockMessage->GetBlockData();
check( blockData );
for ( int j = 0; j < blockSize; ++j )
{
check( blockData[j] == uint8_t( numMessagesReceived + j ) );
}
++numMessagesReceived;
messageFactory.ReleaseMessage( message );
}
if ( numMessagesReceived == NumMessagesSent )
break;
}
check( numMessagesReceived == NumMessagesSent );
}
void test_single_message_type_unreliable()
{
SingleTestMessageFactory messageFactory( GetDefaultAllocator() );
double time = 100.0;
ConnectionConfig connectionConfig;
connectionConfig.numChannels = 1;
connectionConfig.channel[0].type = CHANNEL_TYPE_UNRELIABLE_UNORDERED;
Connection sender( GetDefaultAllocator(), messageFactory, connectionConfig, time );
Connection receiver( GetDefaultAllocator(), messageFactory, connectionConfig, time );
const int SenderPort = 10000;
const int ReceiverPort = 10001;
Address senderAddress( "::1", SenderPort );
Address receiverAddress( "::1", ReceiverPort );
const int NumIterations = 256;
const int NumMessagesSent = 16;
for ( int j = 0; j < NumMessagesSent; ++j )
{
TestMessage * message = (TestMessage*) messageFactory.CreateMessage( SINGLE_TEST_MESSAGE );
check( message );
message->sequence = j;
sender.SendMessage( 0, message );
}
int numMessagesReceived = 0;
uint16_t senderSequence = 0;
uint16_t receiverSequence = 0;
for ( int i = 0; i < NumIterations; ++i )
{
PumpConnectionUpdate( connectionConfig, time, sender, receiver, senderSequence, receiverSequence, 0.1f, 0 );
while ( true )
{
Message * message = receiver.ReceiveMessage( 0 );
if ( !message )
break;
check( message->GetType() == SINGLE_TEST_MESSAGE );
TestMessage * testMessage = (TestMessage*) message;
check( testMessage->sequence == uint16_t( numMessagesReceived ) );
++numMessagesReceived;
messageFactory.ReleaseMessage( message );
}
if ( numMessagesReceived == NumMessagesSent )
break;
}
check( numMessagesReceived == NumMessagesSent );
}
#define RUN_TEST( test_function ) \
do \
{ \
@@ -2327,6 +2537,9 @@ int main()
RUN_TEST( test_client_server_message_exhaust_stream_allocator );
RUN_TEST( test_client_server_message_receive_queue_overflow );
RUN_TEST( test_reliable_fragment_overflow_bug );
RUN_TEST( test_single_message_type_reliable );
RUN_TEST( test_single_message_type_reliable_blocks );
RUN_TEST( test_single_message_type_unreliable );
#if SOAK
if ( quit )
View
@@ -1447,7 +1447,14 @@ namespace yojimbo
{
// block message
serialize_int( stream, block.messageType, 0, maxMessageType );
if ( maxMessageType > 0 )
{
serialize_int( stream, block.messageType, 0, maxMessageType );
}
else
{
block.messageType = 0;
}
if ( Stream::IsReading )
{

0 comments on commit f8a564e

Please sign in to comment.