testAsyncPacketQueue.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "unit/test.h"
  23. #include "platform/async/asyncPacketQueue.h"
  24. #include "console/console.h"
  25. #include "core/util/tVector.h"
  26. #ifndef TORQUE_SHIPPING
  27. using namespace UnitTesting;
  28. #define TEST( x ) test( ( x ), "FAIL: " #x )
  29. CreateUnitTest( TestAsyncPacketQueue, "Platform/AsyncPacketQueue" )
  30. {
  31. struct Packet
  32. {
  33. typedef void Parent;
  34. StringChar mChar;
  35. U32 mDuration;
  36. S32 mWriteIndex;
  37. U32 mWriteTime;
  38. Packet() {}
  39. Packet( StringChar ch, U32 duration )
  40. : mChar( ch ), mDuration( duration ), mWriteIndex( -2 ), mWriteTime( 0 ) {}
  41. };
  42. struct TimeSource
  43. {
  44. typedef void Parent;
  45. U32 mStartTime;
  46. TimeSource()
  47. : mStartTime( Platform::getRealMilliseconds() ) {}
  48. U32 getPosition()
  49. {
  50. return ( Platform::getRealMilliseconds() - mStartTime );
  51. }
  52. };
  53. struct Consumer : public IOutputStream< Packet* >
  54. {
  55. typedef IOutputStream< Packet* > Parent;
  56. U32 mIndex;
  57. Consumer()
  58. : mIndex( 0 ) {}
  59. virtual void write( Packet* const* packets, U32 num )
  60. {
  61. for( U32 i = 0; i < num; ++ i )
  62. {
  63. Packet* p = packets[ i ];
  64. Con::printf( "%c", p->mChar );
  65. p->mWriteTime = Platform::getRealMilliseconds();
  66. p->mWriteIndex = mIndex;
  67. mIndex ++;
  68. }
  69. }
  70. };
  71. void test1( bool dropPackets, U32 queueLength )
  72. {
  73. F32 factor = Con::getFloatVariable( "$testAsyncPacketQueue::timeFactor", 100.0f );
  74. String str = Con::getVariable( "$testAsyncPacketQueue::string" );
  75. if( str.isEmpty() )
  76. str = "This is a test string";
  77. Vector< Packet > packets;
  78. for( U32 i = 0; i < str.size(); ++ i )
  79. packets.push_back( Packet( str[ i ], U32( Platform::getRandom() * factor ) ) );
  80. U32 totalTime = 0;
  81. for( U32 i = 0; i < packets.size(); ++ i )
  82. totalTime += packets[ i ].mDuration;
  83. TimeSource timeSource;
  84. Consumer consumer;
  85. AsyncPacketQueue< Packet*, TimeSource* > queue( queueLength, &timeSource, &consumer, totalTime, dropPackets );
  86. U32 index = 0;
  87. while( !queue.isAtEnd() )
  88. {
  89. if( queue.needPacket()
  90. && index < packets.size() )
  91. {
  92. Packet* packet = &packets[ index ];
  93. index ++;
  94. queue.submitPacket( packet, packet->mDuration );
  95. }
  96. }
  97. U32 time = timeSource.mStartTime;
  98. S32 lastIndex = -1;
  99. for( U32 i = 0; i < packets.size(); ++ i )
  100. {
  101. TEST( ( packets[ i ].mWriteIndex == -2 && dropPackets ) // not written = dropped
  102. || packets[ i ].mWriteIndex == lastIndex + 1 );
  103. if( packets[ i ].mWriteIndex != -2 )
  104. lastIndex ++;
  105. if( queueLength == 1 )
  106. TEST( packets[ i ].mWriteTime >= time || dropPackets ); // start time okay
  107. time += packets[ i ].mDuration;
  108. if( dropPackets )
  109. TEST( packets[ i ].mWriteTime < time ); // end time okay (if not dropping)
  110. }
  111. }
  112. void run()
  113. {
  114. test1( false, 1 );
  115. test1( true, 1 );
  116. test1( false, 4 );
  117. test1( true, 4 );
  118. }
  119. };
  120. #endif // !TORQUE_SHIPPING