SstCloseTest() { plugins.load( "tcpsst" ); uint32 randport = 3000 + (uint32)(Sirikata::Task::LocalTime::now().raw() % 20000); mPort = boost::lexical_cast<std::string>(randport); mBytes=65536; mChunks=3; mOffset=1; mSendService = new IOServicePool(4); mSendStrand = mSendService->service()->createStrand(); mRecvService = new IOServicePool(4); mRecvStrand = mRecvService->service()->createStrand(); mListener = StreamListenerFactory::getSingleton().getDefaultConstructor()(mRecvStrand,StreamListenerFactory::getSingleton().getDefaultOptionParser()(String())); using std::tr1::placeholders::_1; using std::tr1::placeholders::_2; mListener->listen(Address("127.0.0.1",mPort),std::tr1::bind(&SstCloseTest::listenerNewStreamCallback,this,_1,_2)); mRecvService->run(); #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif }
~SstCloseTest() { delete mListener; mSendService->join(); mRecvService->join(); delete mSendStrand; delete mRecvStrand; delete mSendService; delete mRecvService; }
~SstTest() { { unique_mutex_lock lck(mMutex); for(std::vector<Stream*>::iterator i=mStreams.begin(),ie=mStreams.end();i!=ie;++i) { delete *i; } mStreams.resize(0); } delete mListener; // The other thread should finish up any outstanding handlers and stop mServicePool->join(); delete mServicePool; }
void listenerDataRecvCallback(Stream *s,int id, const Chunk&data, const Stream::PauseReceiveCallback& pauseReceive) { if (mSendReceiveMap[id]==-1) { mSendReceiveMap[id]=(unsigned char)data[0]; } static bool pause=true; if (rand()<RAND_MAX/5||pause) pause=!pause; if (pause) { mRecvService->service()->post(std::tr1::bind(&Stream::readyRead,s)); pauseReceive(); return; } mReceiversData[mSendReceiveMap[id]]+=(int)(data.size()*2); }
void testConnectSend (void ) { Stream*z=NULL; bool doSubstreams=true; { Stream *r=StreamFactory::getSingleton().getDefaultConstructor()(mServicePool->service(),StreamFactory::getSingleton().getDefaultOptionParser()(String())); simpleConnect(r,Address("127.0.0.1",mPort)); runRoutine(r); if (doSubstreams) { { Stream*zz=r->factory(); zz = r->clone(&SstTest::noopSubstream); if (zz) { runRoutine(zz); zz->close(); } delete zz; } using std::tr1::placeholders::_1; using std::tr1::placeholders::_2; z=r->clone(std::tr1::bind(&SstTest::testSubstream,this,_1,_2)); if (z) { runRoutine(z); }else { ++mDisconCount; } } //wait until done time_t last_time=time(NULL); int retry_count=20; while(mCount<(int)(mMessagesToSend.size()*(doSubstreams?5:2))&&!mAbortTest) { if (rand()<RAND_MAX/10) { r->readyRead(); z->readyRead(); { unique_mutex_lock lck(mMutex); for(std::vector<Stream*>::iterator i=mStreams.begin(),ie=mStreams.end();i!=ie;++i) { (*i)->readyRead(); } } } time_t this_time=time(NULL); if (this_time>last_time+5) { std::cerr<<"Message Receive Count == "<<mCount.read()<<'\n'; last_time=this_time; if (--retry_count<=0) { TS_FAIL("Timeout in receiving messages"); TS_ASSERT_EQUALS(mCount.read(),(int)(mMessagesToSend.size()*(doSubstreams?5:2))); break; } } } TS_ASSERT(mAbortTest==false); for (std::map<unsigned int,std::vector<Sirikata::Network::Chunk> >::iterator datamapiter=mDataMap.begin(); datamapiter!=mDataMap.end(); ++datamapiter) { validateVector(datamapiter->first,datamapiter->second,mMessagesToSend); } r->close(); delete r; } if( doSubstreams){ z->close(); time_t last_time=time(NULL); int retry_count=6; while(mDisconCount.read()<3){ if (rand()<RAND_MAX/10) { z->readyRead(); { unique_mutex_lock lck(mMutex); for (ptrdiff_t i=((ptrdiff_t)mStreams.size())-1;i>=0;--i) { mStreams[i]->readyRead(); } } } time_t this_time=time(NULL); if (this_time>last_time+5) { std::cerr<<"Close Receive Count == "<<mDisconCount.read()<<'\n'; last_time=this_time; if (--retry_count<=0) { TS_FAIL("Timeout in receiving close signals"); TS_ASSERT_EQUALS(mDisconCount.read(),3); break; } } } delete z; } time_t last_time=time(NULL); int retry_count=3; while(mEndCount.read()<1){//checking for that final call to newSubstream time_t this_time=time(NULL); if (rand()<RAND_MAX/10) { { unique_mutex_lock lck(mMutex); for (ptrdiff_t i=((ptrdiff_t)mStreams.size())-1;i>=0;--i) { mStreams[i]->readyRead(); } } } if (this_time>last_time+5) { std::cerr<<"SubStream End Receive Count == "<<mEndCount.read()<<'\n'; last_time=this_time; if (--retry_count<=0) { TS_FAIL("Timeout in receiving newSubstream functor deallocation request"); TS_ASSERT_EQUALS(mEndCount.read(),1); break; } } } }
SstTest():mCount(0),mDisconCount(0),mEndCount(0),ENDSTRING("T end"),mAbortTest(false) { Sirikata::PluginManager plugins; plugins.load( Sirikata::DynamicLibrary::filename("tcpsst") ); mPort="9142"; mServicePool = new IOServicePool(1); mListener = StreamListenerFactory::getSingleton().getDefaultConstructor()(mServicePool->service(),StreamListenerFactory::getSingleton().getDefaultOptionParser()(String())); using std::tr1::placeholders::_1; using std::tr1::placeholders::_2; mListener->listen(Address("127.0.0.1",mPort),std::tr1::bind(&SstTest::listenerNewStreamCallback,this,0,_1,_2)); mServicePool->run(); bool doUnorderedTest=true; bool doShortTest=false; if (doUnorderedTest){ mMessagesToSend.push_back("U:0"); mMessagesToSend.push_back("U:1"); mMessagesToSend.push_back("U:2"); } mMessagesToSend.push_back("T0th"); mMessagesToSend.push_back("T1st"); mMessagesToSend.push_back("T2nd"); mMessagesToSend.push_back("T3rd"); mMessagesToSend.push_back("T4th"); mMessagesToSend.push_back("T5th"); if (doUnorderedTest){ mMessagesToSend.push_back("U:3"); mMessagesToSend.push_back("U:4"); mMessagesToSend.push_back("U:5"); } mMessagesToSend.push_back("T6th"); mMessagesToSend.push_back("T7th"); mMessagesToSend.push_back("T8th"); mMessagesToSend.push_back("T9th"); if (!doShortTest) { std::string test("T"); for (unsigned int i=0;i<16385;++i) { test+=(char)((i+5)%128); } mMessagesToSend.push_back(test); if (doUnorderedTest){ test[0]='U'; mMessagesToSend.push_back(test); } for (unsigned int i=0;i<256*1024;++i) { test+=(char)((rand())%256); } if (doUnorderedTest){ mMessagesToSend.push_back(test); } test[0]='T'; mMessagesToSend.push_back(test); int pattern[40]={127,257,511,65537,129,16383,254,255,16384,256,16385,32767,1440,32768,32769,65535,65536,1401,1399,1280, 228,25215,5141,627,151,1,4,124240,3,296,114385,25,17,47,42,24222,655,1441,1439,1024}; for (size_t i=0;i<sizeof(pattern)/sizeof(pattern[0]);++i) { std::string pat="T"; for (int j=0;j<pattern[i];++j) { pat+='~'-(i%((pattern[i]%90)+3)); } mMessagesToSend.push_back(pat); pat[0]='U'; mMessagesToSend.push_back(pat); } mMessagesToSend.push_back("T_0th"); mMessagesToSend.push_back("T_1st"); mMessagesToSend.push_back("T_2nd"); mMessagesToSend.push_back("T_3rd"); mMessagesToSend.push_back("T_4th"); mMessagesToSend.push_back("T_5th"); if (doUnorderedTest){ mMessagesToSend.push_back("U:6"); mMessagesToSend.push_back("U:7"); mMessagesToSend.push_back("U:8"); mMessagesToSend.push_back("U:9"); mMessagesToSend.push_back("U:A"); } mMessagesToSend.push_back("T_6th"); mMessagesToSend.push_back("T_7th"); if (doUnorderedTest){ mMessagesToSend.push_back("U:B"); } mMessagesToSend.push_back("T_8th"); if (doUnorderedTest){ mMessagesToSend.push_back("U:C"); } mMessagesToSend.push_back("T_9th"); if (doUnorderedTest){ mMessagesToSend.push_back("U:D"); } mMessagesToSend.push_back("The green grasshopper fetched."); if (doUnorderedTest){ mMessagesToSend.push_back("U:E"); mMessagesToSend.push_back("U:F"); mMessagesToSend.push_back("U:G"); } mMessagesToSend.push_back("T A blade of grass."); mMessagesToSend.push_back("T From the playground ."); mMessagesToSend.push_back("T Grounds test test test this is a test test test this is a test test test this is a test test test test and the test is proceeding until it reaches signific length with a string that long however. this is not quite long enough to trigger the high water mark--well now it is I believe to the best of my abilities"); mMessagesToSend.push_back("T Grounds test test test this is a test test test this is a test test test this is a test test test test and the test is proceeding until it reaches signific length with a string that long however. this is not quite"); if (doUnorderedTest){ mMessagesToSend.push_back("U:H"); mMessagesToSend.push_back("U:I"); mMessagesToSend.push_back("U:J"); mMessagesToSend.push_back("U:K"); mMessagesToSend.push_back("U:L"); mMessagesToSend.push_back("U:M"); mMessagesToSend.push_back("U:N"); mMessagesToSend.push_back("U:O"); mMessagesToSend.push_back("U:P"); mMessagesToSend.push_back("U:Q"); } } mMessagesToSend.push_back(ENDSTRING); }
void closeStreamRun(bool fork, bool doSleep=false) { using std::tr1::placeholders::_1; using std::tr1::placeholders::_2; for(int i=0;i<NUM_TEST_STREAMS;++i) { mReceivers[i]=mSenders[i]=NULL; mSendReceiveMap[i]=-1; mReceiversData[i]=0; } for(int i=0;i<NUM_TEST_STREAMS;++i) { if (i==0||!fork) { mSenders[i]=StreamFactory::getSingleton().getDefaultConstructor()(mSendStrand,StreamFactory::getSingleton().getDefaultOptionParser()(String("--parallel-sockets=1"))); mSenders[i]->connect(Address("127.0.0.1",mPort), &Stream::ignoreSubstreamCallback, std::tr1::bind(&SstCloseTest::connectionCallback,this,i,_1,_2), &Stream::ignoreReceivedCallback, &Stream::ignoreReadySendCallback); }else { mSenders[i]=mSenders[0]->clone( std::tr1::bind(&SstCloseTest::connectionCallback,this,i,_1,_2), &Stream::ignoreReceivedCallback, &Stream::ignoreReadySendCallback); } } mSendService->reset(); mSendService->run(); #ifdef _WIN32 //Sleep(1000); #else sleep(1); #endif int sentSoFar=0; for (int c=0;c<mChunks;++c) { int willHaveSent=(c+1)*mBytes/mChunks; Chunk cur(willHaveSent-sentSoFar); for (size_t j=0;j<cur.size();++j) { cur[j]=j+sentSoFar; } for (int i=0;i<NUM_TEST_STREAMS;++i) { if (cur.size()) { cur[0]=i%256; if (cur.size()>1) { cur[1]=i/256%256; } if (cur.size()>2) { cur[2]=i/256/256%256; } if (cur.size()>3) { cur[3]=i/256/256/256%256; } } bool retval=mSenders[i]->send(MemoryReference(cur),ReliableUnordered); TS_ASSERT(retval); } if (doSleep) { #ifdef _WIN32 Sleep(1000); #else // sleep(1); #endif } sentSoFar=willHaveSent; } if (!doSleep) { #ifdef _WIN32 Sleep(1000); #else // sleep(1); #endif } //SILOG(tcpsst,error,"CLOSING"); for (int i=mOffset;i<NUM_TEST_STREAMS;++i) { mSenders[i]->close(); } //SILOG(tcpsst,error,"CLOSED"); bool done=true; int counter=0; do { done=true; for (int i=mOffset;i<NUM_TEST_STREAMS;++i) { if (mReceiversData[i].read()!=mBytes*2+1) { done=false; if (counter>4998) { SILOG(ssttest,error,"Data "<<i<<" only "<<mReceiversData[i].read()<<" != "<<mBytes*2+1); } } } if (counter>4997&&!done) { #ifdef _WIN32 Sleep(1000); #else sleep (1); #endif } ++counter; }while (counter<5000&&!done); TS_ASSERT(done); for (int i=0;i<NUM_TEST_STREAMS;++i) { delete mSenders[i]; mSenders[i]=NULL; } for (int i=0;i<NUM_TEST_STREAMS;++i) { delete mReceivers[i]; mReceivers[i]=NULL; } }