Пример #1
0
int test_sendData(){
    Protocol p;
    p.state = STATE_CONNECTED;
    p.seqnum = 1337;
    ASSERT(p.readyForData());
    std::vector<uint8_t> emptydata;
    std::vector<ProtocolPacket> out = p._sendData(emptydata,5);
    ASSERT(out.size() == 1);
    ASSERT(out[0].seqnum == 1337);
    ASSERT(p.outgoingDataPacket->seqnum == 1337);
    ASSERT(p.lastSendAttempt == 5);
    return 0;
}
Пример #2
0
int testDataResending() {
    Protocol p;
    p.state = STATE_CONNECTED;
    p.seqnum = 1337;
    p.pingInterval = 9000; // suppress any pings
    ASSERT(p.readyForData() == true);
    std::vector<uint8_t> emptydata;
    std::vector<ProtocolPacket> out = p._sendData(emptydata,0);
    ASSERT(out.size() == 1);
    ASSERT(out[0].seqnum == 1337);
    ASSERT(p.outgoingDataPacket);
    ASSERT(p.outgoingDataPacket->seqnum == 1337);
    out = p._timerEvent(p.lastSendAttempt + p.sendAttemptInterval);
    ASSERT(out.size() == 0);
    out = p._timerEvent(p.lastSendAttempt + p.sendAttemptInterval + 1);
    ASSERT(out.size() == 1);
    ASSERT(p.outgoingDataPacket->seqnum == 1337);
    return 0; 
}
Пример #3
0
int testRecoverLost() {
    
    Protocol a;
    Protocol b;
    
    uint64_t t = 0;
    a.listen();
    std::vector<ProtocolPacket> fora = b._connect(t);
    std::vector<ProtocolPacket> forb;
    
    std::vector<ProtocolPacket> out;
    
    std::string arecieved;
    std::string brecieved;
    int asentCount = 0;
    int bsentCount = 0;
    int loopCounter = 0;
    while (loopCounter < 500000) {
        t += 10;
        if (asentCount < 500 && a.readyForData()) {
            out = a._sendData("foo",t);
            forb.insert(forb.end(),out.begin(),out.end());
            asentCount += 1;
        }
           
        if (bsentCount < 1000 && b.readyForData()) {
            out = b._sendData("bar",t);
            fora.insert(fora.end(),out.begin(),out.end());
            bsentCount += 1;
        }
        
        if (loopCounter > 10) { //start dropping packets after _connection is up
            if  (((loopCounter % 8) == 0) && fora.size() ) {
                fora.pop_back();
            }
            
            if (((loopCounter % 7) == 0) && forb.size()){
                forb.pop_back();
            }
        }
        
        // drop some more packets by not accepting data arbitrarily
        for (std::vector<ProtocolPacket>::iterator it = fora.begin() ; it != fora.end() ; it++ ) {
            std::pair<std::vector<ProtocolPacket>,std::vector<uint8_t> > eventResult;
            eventResult = a._packetEvent(*it,t,true);
            out = eventResult.first;
            std::string gotData(eventResult.second.begin(),eventResult.second.end());
            arecieved += gotData;
            forb.insert(forb.end(),out.begin(),out.end());
           
        }
        fora.clear();
        for (std::vector<ProtocolPacket>::iterator it = forb.begin() ; it != forb.end() ; it++ ) {
            std::pair<std::vector<ProtocolPacket>,std::vector<uint8_t> > eventResult;
            eventResult = b._packetEvent(*it,t,true);
            out = eventResult.first;
            std::string gotData(eventResult.second.begin(),eventResult.second.end());
            brecieved += gotData;
            fora.insert(fora.end(),out.begin(),out.end());
        }
        forb.clear();
        
        out = a._timerEvent(t);
        forb.insert(forb.end(),out.begin(),out.end());
        out = b._timerEvent(t);
        fora.insert(fora.end(),out.begin(),out.end());
        
        if (asentCount == 500 && bsentCount == 1000 && !a.outgoingDataPacket && !b.outgoingDataPacket) {
            break;
        }
        
        loopCounter += 1;
    }
    ASSERT(a.state == STATE_CONNECTED);
    ASSERT(b.state == STATE_CONNECTED);
    ASSERT(loopCounter != 500000);
    ASSERT(arecieved.size() == 1000*3);
    for(int i = 0 ; i < 1000; i++){
        ASSERT(arecieved.substr(i*3,3) == std::string("bar"));
    }
    for(int i = 0 ; i < 500; i++){
        ASSERT(brecieved.substr(i*3,3) == std::string("foo"));
    }    
    return 0;
}