porttest.cpp

来自「一个语言识别引擎」· C++ 代码 · 共 715 行 · 第 1/2 页

CPP
715
字号
        output.open("/out");
    
        input.setReader(provider);
    
        output.addOutput(Contact::byName("/in").addCarrier("tcp"));
        Time::delay(0.1);
        ServiceTester tester(*this);
        output.write(tester);
        Time::delay(0.1);
        tester.finalCheck();
        Time::delay(0.1);
        output.close();
        input.close();    
    }


    virtual void testBackground() {
        report(0,"test communication in background mode");

        Port input, output;
        input.open("/in");
        output.open("/out");
        output.enableBackgroundWrite(true);

        BinPortable<int> bin, bout;
        bout.content() = 42;
        bin.content() = 20;

        output.addOutput("/in");

        report(0,"writing...");
        output.write(bout);
        report(0,"reading...");
        input.read(bin);

        checkEqual(bout.content(),bin.content(),"successful transmit");

        while (output.isWriting()) {
            report(0,"waiting for port to stabilize");
            Time::delay(0.2);
        }

        bout.content() = 88;

        report(0,"writing...");
        output.write(bout);
        report(0,"reading...");
        input.read(bin);

        checkEqual(bout.content(),bin.content(),"successful transmit");

        output.close();
        input.close();
    }

    void testWriteBuffer() {
        report(0,"testing write buffering");

        Port input, output, altInput;
        input.open("/in");
        altInput.open("/in2");
        output.open("/out");
        output.addOutput("/in");

        report(0,"beginning...");

        BinPortable<int> bin;
        PortWriterBuffer<BinPortable<int> > binOut;
        binOut.attach(output);

        int val1 = 15;
        int val2 = 30;

        BinPortable<int>& active = binOut.get();
        active.content() = val1;
        binOut.write();

        output.addOutput("/in2");
        BinPortable<int>& active2 = binOut.get();
        active2.content() = val2;
        binOut.write();

        input.read(bin);
        checkEqual(val1,bin.content(),"successful transmit");

        altInput.read(bin);
        checkEqual(val2,bin.content(),"successful transmit");

        while (output.isWriting()) {
            report(0,"waiting for port to stabilize");
            Time::delay(0.2);
        }

        report(0,"port stabilized");
		output.close();
        report(0,"shut down output buffering");
    }

    void testBufferedPort() {
        report(0,"checking buffered port");
        BufferedPort<BinPortable<int> > output, input;
        output.open("/out");
        input.open("/in");

        report(0,"is write a no-op when no connection exists?...");
        BinPortable<int>& datum0 = output.prepare();
        datum0.content() = 123;
        report(0,"writing...");
        output.write();

        output.addOutput("/in");
        report(0,"now with a connection...");
        BinPortable<int>& datum = output.prepare();
        datum.content() = 999;
        report(0,"writing...");
        output.write();
        report(0,"reading...");
        BinPortable<int> *bin = input.read();
        checkEqual(bin->content(),999,"good send");
	}

    void testCloseOrder() {
        report(0,"check that port close order doesn't matter...");

        for (int i=0; i<4; i++) {
            // on OSX there is a problem only tickled upon repetition
            {
                Port input, output;
                input.open("/in");
                output.open("/out");
                output.addOutput("/in");
                
                report(0,"closing in");
                input.close();
                
                report(0,"closing out");
                output.close();
            }
            
            {
                Port input, output;
                input.open("/in");
                output.open("/out");
                output.addOutput("/in");
                
                report(0,"closing out");
                output.close();
                
                report(0,"closing in");
                input.close();
            }
        }
    }


    virtual void testDelegatedReadReply() {
        report(0,"check delegated read and reply...");
        DelegatedReader reader;
        DelegatedWriter writer;
        reader.start();
        writer.start();
        writer.stop();
        reader.stop();
        checkEqual(writer.total,6,"read/replies give right checksum");
    }

    virtual void testReaderHandler() {
        report(0,"check reader handler...");
        Port in;
        Port out;
        DelegatedCallback callback;
        out.open("/out");
        in.open("/in");
        Network::connect("/out","/in");
        PortReaderBuffer<Bottle> reader;
        reader.setStrict();
        reader.attach(in);
        reader.useCallback(callback);
        Bottle src("10 10 20");
        out.write(src);
        callback.produce.wait();
        checkEqual(callback.saved.size(),3,"object came through");
        out.close();
        in.close();
    }

    virtual void testReaderHandler2() {
        report(0,"check reader handler, bufferedport style...");
        BufferedPort<Bottle> in;
        Port out;
        DelegatedCallback callback;
        in.setStrict();
        out.open("/out");
        in.open("/in");
        Network::connect("/out","/in");
        in.useCallback(callback);
        Bottle src("10 10 20");
        out.write(src);
        callback.produce.wait();
        checkEqual(callback.saved.size(),3,"object came through");
    }


    virtual void testStrictWriter() {
        report(0,"check strict writer...");
        BufferedPort<Bottle> in;
        BufferedPort<Bottle> out;
        in.setStrict();
        in.open("/in");
        out.open("/out");
        
        Network::connect("/out","/in");
        
        Bottle& outBot1 = out.prepare();
        outBot1.fromString("hello world");
        printf("Writing bottle 1: %s\n", outBot1.toString().c_str());
        out.write(true);
        
        Bottle& outBot2 = out.prepare();
        outBot2.fromString("2 3 5 7 11");
        printf("Writing bottle 2: %s\n", outBot2.toString().c_str());
        out.write(true);
        
        Bottle *inBot1 = in.read();
        checkTrue(inBot1!=NULL,"got 1 of 2 items");
        if (inBot1!=NULL) {
            printf("Bottle 1 is: %s\n", inBot1->toString().c_str());
            checkEqual(inBot1->size(),2,"match for item 1");
        }
        Bottle *inBot2 = in.read();
        checkTrue(inBot2!=NULL,"got 2 of 2 items");
        if (inBot2!=NULL) {
            printf("Bottle 2 is: %s\n", inBot2->toString().c_str());
            checkEqual(inBot2->size(),5,"match for item 1");
        }
    }


    virtual void testRecentReader() {
        report(0,"check recent reader...");
        BufferedPort<Bottle> in;
        BufferedPort<Bottle> out;
        in.setStrict(false);
        in.open("/in");
        out.open("/out");
        
        Network::connect("/out","/in");
        
        Bottle& outBot1 = out.prepare();
        outBot1.fromString("hello world");
        printf("Writing bottle 1: %s\n", outBot1.toString().c_str());
        out.write(true);
        
        Bottle& outBot2 = out.prepare();
        outBot2.fromString("2 3 5 7 11");
        printf("Writing bottle 2: %s\n", outBot2.toString().c_str());
        out.write(true);

        Time::delay(0.25);

        Bottle *inBot2 = in.read();
        checkTrue(inBot2!=NULL,"got 2 of 2 items");
        if (inBot2!=NULL) {
            printf("Bottle 2 is: %s\n", inBot2->toString().c_str());
            checkEqual(inBot2->size(),5,"match for item 1");
        }
    }


    virtual void testUnbufferedClose() {
        report(0,"check that ports that receive data and do not read it can close...");
        BufferedPort<Bottle> sender;
        Port receiver;
        sender.open("/sender");
        receiver.open("/receiver");
        Network::connect("/sender","/receiver");
        Time::delay(0.25);
        Bottle& bot = sender.prepare();
        bot.clear();
        bot.addInt(1);
        sender.write();
        Time::delay(0.25);
        report(0,"if this hangs, PortTest::testUnbufferedClose is unhappy");
        receiver.close();
        sender.close();
    }

    virtual void testCloseOpenRepeats() {
        report(0,"check that opening-closing-opening etc is ok...");
        report(0,"non-buffered port:");
        Port p;
        p.open("/test1");
        p.open("/test2");
        p.open("/test3");
        p.close();
        p.open("/test4");
        p.close();
        report(0,"buffered port:");
        BufferedPort<Bottle> p2, p3;
        p2.open("/test1");
        p2.open("/test2");
        p2.open("/in");
        p3.open("/out");
        Network::connect("/out","/in");
        p3.prepare().fromString("10 20 30");
        p3.write();
        report(0,"wait for input...");
        p2.read(true);
        report(0,"... got it");
        p3.prepare().fromString("10 20 30");
        p3.write();
        p2.open("/test1");
        p3.open("/test2");
        Network::connect("/test2","/test1");
        p3.prepare().fromString("10 20 30");
        p3.write();
        report(0,"wait for input...");
        p2.read(true);
        report(0,"... got it");
    }


    virtual void runTests() {
        yarp::NameClient& nic = yarp::NameClient::getNameClient();
        nic.setFakeMode(true);

        testOpen();
        testReadBuffer();
        testPair();
        testReply();
        testUdp();
        //testHeavy();


        testBackground();
        testWriteBuffer();
        testBufferedPort();
        testCloseOrder();
        testDelegatedReadReply();
        testReaderHandler();
        testReaderHandler2();
        testStrictWriter();
        testRecentReader();
        testUnbufferedClose();
        //testCloseOpenRepeats(); //bring this back soon

        nic.setFakeMode(false);
    }
};

static PortTest thePortTest;

yarp::UnitTest& getPortTest() {
    return thePortTest;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?