示例#1
0
int testConnecting() {
    std::vector<ProtocolPacket> out;
    Protocol p;
    p.outgoingDataPacket = std::tr1::shared_ptr<ProtocolPacket>(new ProtocolPacket(TYPE_DATA,5,NULL,0));
    out = p._connect(0);
    ASSERT(p.state == STATE_CONNECTING);
    ASSERT(! p.outgoingDataPacket);
    
    ASSERT(out.size() == 1);
    ASSERT(out[0].type == TYPE_CON);
    
    ProtocolPacket pp(TYPE_CONACK);
    
    out = p._packetEvent(pp,50).first;
    ASSERT(p.state == STATE_CONNECTED);
    ASSERT(out.size() == 0);
    
    return 0;
}
示例#2
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;
}
示例#3
0
int testConnect() {
    
    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::set<PacketType> packetTypes;
    
    for( int i = 0;  i < 1000 ; i++) {
        for (std::vector<ProtocolPacket>::iterator it = fora.begin() ; it != fora.end() ; it++ ) {
            packetTypes.insert(it->type);
            out = a._packetEvent(*it,t).first;
            forb.insert(forb.end(),out.begin(),out.end()); 
        }
        fora.clear();
        for (std::vector<ProtocolPacket>::iterator it = forb.begin() ; it != forb.end() ; it++ ) {
            packetTypes.insert(it->type);
            out = b._packetEvent(*it,t).first;
            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());
        
        t += 10;
    }
    
    ASSERT(a.state == STATE_CONNECTED);
    ASSERT(b.state == STATE_CONNECTED);
    
    ASSERT(packetTypes.find(TYPE_CON) != packetTypes.end());
    ASSERT(packetTypes.find(TYPE_CONACK) != packetTypes.end());
    ASSERT(packetTypes.find(TYPE_PING) != packetTypes.end());
    
    for ( int i = 0; i < 10000 ; i++) {
        a._timerEvent(t);
        b._timerEvent(t);
        t += 10;
    }
    ASSERT(a.state == STATE_UNINIT);
    ASSERT(b.state == STATE_UNINIT);
    
    b._connect(t);
    ASSERT(b.state == STATE_CONNECTING);
    b._timerEvent(t);
    ASSERT(b.state == STATE_CONNECTING);
    for(int i = 0; i < 10000 ; i++) {
        b._timerEvent(t);
        t += 10;
    }
    ASSERT(b.state == STATE_UNINIT);
    
    return 0;
    
}