void TCPConnectionManagerTest::test_send()
{
    // Use Case:
    // client requests a connection
    // expect client to be registered for any data
    StreamDataRequest req;
    DataSpec require;
    require.addStreamData("testData");
    req.addDataOption(require);

    QTcpSocket* client = _createClient();
    _sendRequest( client, req );
    QCoreApplication::processEvents();
    CPPUNIT_ASSERT_EQUAL( 1, _server->clientsForStream("testData") );
    TestDataBlob blob;
    blob.setData("sometestData");
    _server->send("testData",&blob);
    sleep(1);

    CPPUNIT_ASSERT( client->state() == QAbstractSocket::ConnectedState );
    boost::shared_ptr<ServerResponse> r = _clientProtocol->receive(*client);
    CPPUNIT_ASSERT( r->type() == ServerResponse::Blob );
    DataBlobResponse* res = static_cast<DataBlobResponse*>(r.get());
    CPPUNIT_ASSERT( res->dataName() == "testData" );
    CPPUNIT_ASSERT( res->blobClass() == "TestDataBlob" );
    TestDataBlob recvBlob;
    recvBlob.deserialise(*client, res->byteOrder());
    CPPUNIT_ASSERT(recvBlob == blob);

}
Esempio n. 2
0
/**
 * @details
 * Deserialises the data from the given QIODevice into a
 * TestDataBlob structure.
 *
 * @param[in] in    A pointer to the input device.
 */
void TestServiceAdapter::deserialise(QIODevice* in)
{
    TestDataBlob* blob = static_cast<TestDataBlob*>(_data);
    blob->resize(_chunkSize);
    // Ensure there is enough data to read from the device.
    while (in->bytesAvailable() < (qint64)_chunkSize)
        in->waitForReadyRead(-1);
    in->read( blob->data().data(), (quint64)_chunkSize);
}
void TCPConnectionManagerTest::test_dataSupportedRequest()
{
    // Use Case:
    // Send a dataSupport request with no data
    // Expect: stream to return a DataSupportResponse
    // and add us to the datasupport stream
    QString streamInfo("__streamInfo__");
    DataSupportRequest req;
    QTcpSocket* client = _createClient();
    CPPUNIT_ASSERT_EQUAL( 0, _server->clientsForStream(streamInfo) );
    _sendRequest( client, req );
    QCoreApplication::processEvents();
    CPPUNIT_ASSERT_EQUAL( 1, _server->clientsForStream(streamInfo) );

    sleep(1);
    QCoreApplication::processEvents();
    CPPUNIT_ASSERT( client->state() == QAbstractSocket::ConnectedState );
    boost::shared_ptr<ServerResponse> r = _clientProtocol->receive( *client );
    CPPUNIT_ASSERT( r->type() == ServerResponse::DataSupport );

    // Use case:
    // New stream type arrives
    // Expect:
    // to receive a new DataRequest with the new data
    QString stream1("stream1");
    TestDataBlob blob;
    blob.setData("stream1Data");
    _server->send(stream1,&blob);
    sleep(1);
    QCoreApplication::processEvents();

    r = _clientProtocol->receive(*client);
    CPPUNIT_ASSERT( r->type() == ServerResponse::DataSupport );
    DataSupportResponse* res = static_cast<DataSupportResponse*>(r.get());
    CPPUNIT_ASSERT_EQUAL( 1, res->streamData().size() );
    CPPUNIT_ASSERT( res->streamData().contains(stream1) );

    // Use case:
    // Existing stream type arrives
    // Expect:
    // not to receive a new DataRequest
    _server->send(stream1,&blob);
    sleep(1);
    QCoreApplication::processEvents();

    r = _clientProtocol->receive(*client);
    CPPUNIT_ASSERT( r->type() != ServerResponse::DataSupport );
}
Esempio n. 4
0
void DataViewerTest::test_integrationWithDataClient()
{
    // Use Case:
    // full integration test with a rela DataBlobClient
    // Setup a test server
    QString xml = "<PelicanTCPBlobServer>"
        "   <connection port=\"0\"/>"  // 0 = find unused system port
        "</PelicanTCPBlobServer>";

    ConfigNode config(xml);
    PelicanTCPBlobServer server(config);
    usleep(100);

    DataBlobClient* client = _client( &server );
//    DataViewer viewer(config, address);
//    viewer.setClient(client);
    TestDataBlob blob;
    blob.setData("integration test");
    server.send("testData", &blob);

    delete client;
}
Esempio n. 5
0
/**
 * @details
 */
void PelicanTCPBlobServerTest::test_connection()
{
    // Use Case:
    // ?????? TODO
    // Expect: TODO
    //
    // Create and configure TCP server
    QString xml = "<PelicanTCPBlobServer>"
                  "   <connection port=\"0\"/>"  // 0 = find unused system port
                  "</PelicanTCPBlobServer>";
    ConfigNode config(xml);
    PelicanTCPBlobServer server(config);
    sleep(1);

    // Create a client and connect it to the server
    QTcpSocket tcpSocket;
    tcpSocket.connectToHost( QHostAddress::LocalHost, server.serverPort() );
    if (!tcpSocket.waitForConnected(5000) || tcpSocket.state() == QAbstractSocket::UnconnectedState)
        CPPUNIT_FAIL("Client could not connect to server");

    // Define the data type which the client will except and send request
    StreamDataRequest req;
    DataRequirements require;
    require.setStreamData("testData");
    req.addDataOption(require);

    PelicanClientProtocol clientProtocol;
    QByteArray data = clientProtocol.serialise(req);
    tcpSocket.write(data);
    tcpSocket.waitForBytesWritten(data.size());
    tcpSocket.flush();

    /// ensure we are registered before continuing
    while( server.clientsForStream("testData") == 0 )
    {
        sleep(1);
    }

    {
        // Test Server send
        TestDataBlob blob;
        blob.setData("Testing TCPServer");
        server.send("testData", &blob);

        // Evaluate the response from the server
        tcpSocket.waitForReadyRead();
        boost::shared_ptr<ServerResponse> r = clientProtocol.receive(tcpSocket);
        CPPUNIT_ASSERT( r->type() == ServerResponse::Blob );
        TestDataBlob blobResult;
        blobResult.deserialise(tcpSocket, ((DataBlobResponse*)r.get())->byteOrder());
        CPPUNIT_ASSERT(blobResult == blob);
    }

    {
        // Test Server send
        TestDataBlob blob;
        blob.setData("Testing TCPServer again");
        server.send("testData", &blob);

        // Evaluate the response from the server
        tcpSocket.waitForReadyRead();
        boost::shared_ptr<ServerResponse> r = clientProtocol.receive(tcpSocket);
        CPPUNIT_ASSERT( r->type() == ServerResponse::Blob );
        TestDataBlob blobResult;
        blobResult.deserialise(tcpSocket, ((DataBlobResponse*)r.get())->byteOrder());
        CPPUNIT_ASSERT(blobResult == blob);
    }
}
Esempio n. 6
0
void PelicanServerClientTestMT::test_getData()
{
    try{
    // set up the test server
    TestServiceAdapter serviceAdapter;
    TestStreamAdapter streamAdapter;
    TestServer server;
    QString stream1("stream1");
    QString service1("service1");
    QString version1("version1");
    QString version2("version2");
    DataRequirements reqStream1;
    reqStream1.addStreamData(stream1);
    QByteArray data1("pelican/data1");
    QByteArray data2("pelican/data2");
    unsigned int port = server.port();

    // common client configuration
    Config config;
    config.setFromString(
        "",
        "<testconfig>"
        "   <server host=\"127.0.0.1\"/>"
        "</testconfig>"
    );
    Config::TreeAddress address;
    address << Config::NodeId("server", "");
    address << Config::NodeId("testconfig", "");
    ConfigNode configNode = config.get(address);
    {
        // Use Case:
        // request for Service Data only
        // Expect :
        // throw
        DataChunk sd(service1, version1, data1 );
        CPPUNIT_ASSERT_EQUAL( (long)data1.size(), (long)sd.size() );

        // setup the test
        DataRequirements req;
        req.addServiceData(service1);
        QList<DataRequirements> lreq;
        lreq.append(req);
        DataTypes dt;
        dt.addData(lreq);
        dt.setAdapter(service1, &serviceAdapter);
        PelicanServerClient client(configNode, dt, 0);
        client.setPort(port);

        QHash<QString, DataBlob*> dataHash;
        DataBlob db("DataBlob");
        dataHash.insert(service1, &db);
        CPPUNIT_ASSERT_THROW( client.getData(dataHash), QString );
    }
    {
        // Use Case:
        // Single Request for an existing stream dataset
        // no service data
        // Expect:
        // return the required data stream
        StreamData sd(stream1, version1, data1 );
        CPPUNIT_ASSERT_EQUAL( (long)data1.size(), (long)sd.size() );
        server.serveStreamData(sd);

        // setup the test
        QList<DataRequirements> lreq;
        lreq.append(reqStream1);
        DataTypes dt;
        dt.addData(lreq);
        dt.setAdapter(stream1, &streamAdapter);
        PelicanServerClient client(configNode, dt, 0);
        client.setPort(port);

        QHash<QString, DataBlob*> dataHash;
        TestDataBlob db;
        dataHash.insert(stream1, &db);
        client.getData(dataHash);
        CPPUNIT_ASSERT_EQUAL(version1.toStdString(), db.version().toStdString() );
        CPPUNIT_ASSERT_EQUAL( std::string(data1.data()), std::string(db.data()) );
    }
    {
        // Use Case:
        // Single Request for an existing stream dataset
        // with existing service data
        // Expect:
        // return the required data stream
        DataChunk servd(service1, version2, data2 );
        server.serveServiceData(servd);
        StreamData sd(stream1, version1, data1 );
        CPPUNIT_ASSERT_EQUAL( (long)data1.size(), (long)sd.size() );
        server.serveStreamData(sd);

        // setup the test
        DataRequirements req;
        req.addServiceData(service1);
        req.addStreamData(stream1);
        QList<DataRequirements> lreq;
        lreq.append(req);
        DataTypes dt;
        dt.addData(lreq);
        dt.setAdapter(stream1, &streamAdapter);
        dt.setAdapter(service1, &serviceAdapter);
        PelicanServerClient client(configNode, dt, 0);
        client.setPort(port);

        QHash<QString, DataBlob*> dataHash;
        TestDataBlob db;
        TestDataBlob db_service;
        dataHash.insert(stream1, &db);
        dataHash.insert(service1, &db_service);
        client.getData(dataHash);
        CPPUNIT_ASSERT_EQUAL( version1.toStdString(), db.version().toStdString() );
        CPPUNIT_ASSERT_EQUAL( version2.toStdString(), db_service.version().toStdString() );
        CPPUNIT_ASSERT_EQUAL( std::string(data2.data()), std::string(db_service.data()) );
        CPPUNIT_ASSERT_EQUAL( std::string(data1.data()), std::string(db.data()) );
    }
    }
    catch(const QString& e)
    {
        std::cerr << "error: uncaught exception" << e.toStdString();
        throw e;
    }
}