TCPLinkTest.cc 7.92 KB
Newer Older
Don Gagne's avatar
Don Gagne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2014 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

#include "TCPLinkTest.h"
25
#include "TCPLoopBackServer.h"
Don Gagne's avatar
Don Gagne committed
26 27 28 29 30 31

/// @file
///     @brief TCPLink class unit test
///
///     @author Don Gagne <don@thegagnes.com>

Don Gagne's avatar
Don Gagne committed
32 33 34 35
// This unit test has gotten too flaky to run reliably under TeamCity. Removing for now till there is
// time to debug.
//UT_REGISTER_TEST(TCPLinkUnitTest)

Don Gagne's avatar
Don Gagne committed
36 37 38 39 40 41
TCPLinkUnitTest::TCPLinkUnitTest(void) :
    _link(NULL),
    _hostAddress(QHostAddress::LocalHost),
    _port(5760),
    _multiSpy(NULL)
{
Don Gagne's avatar
Don Gagne committed
42

Don Gagne's avatar
Don Gagne committed
43 44 45 46 47
}

// Called before every test
void TCPLinkUnitTest::init(void)
{
Don Gagne's avatar
Don Gagne committed
48 49
    UnitTest::init();
    
Don Gagne's avatar
Don Gagne committed
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    Q_ASSERT(_link == NULL);
    Q_ASSERT(_multiSpy == NULL);

    _link = new TCPLink(_hostAddress, _port);
    Q_ASSERT(_link != NULL);

    _rgSignals[bytesReceivedSignalIndex] = SIGNAL(bytesReceived(LinkInterface*, QByteArray));
    _rgSignals[connectedSignalIndex] = SIGNAL(connected(void));
    _rgSignals[disconnectedSignalIndex] = SIGNAL(disconnected(void));
    _rgSignals[connected2SignalIndex] = SIGNAL(connected(bool));
    _rgSignals[nameChangedSignalIndex] = SIGNAL(nameChanged(QString));
    _rgSignals[communicationErrorSignalIndex] = SIGNAL(communicationError(const QString&, const QString&));
    _rgSignals[communicationUpdateSignalIndex] = SIGNAL(communicationUpdate(const QString&, const QString&));
    _rgSignals[deleteLinkSignalIndex] = SIGNAL(deleteLink(LinkInterface* const));

    _multiSpy = new MultiSignalSpy();
    QCOMPARE(_multiSpy->init(_link, _rgSignals, _cSignals), true);
}

// Called after every test
void TCPLinkUnitTest::cleanup(void)
{
Don Gagne's avatar
Don Gagne committed
72 73
    UnitTest::cleanup();
    
Don Gagne's avatar
Don Gagne committed
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_link);

    delete _multiSpy;
    delete _link;

    _multiSpy = NULL;
    _link = NULL;
}

void TCPLinkUnitTest::_properties_test(void)
{
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
    // Make sure we get the right values back
    QHostAddress    hostAddressOut;
    quint16         portOut;
    
    hostAddressOut = _link->getHostAddress();
    QCOMPARE(_hostAddress, hostAddressOut);

    portOut = _link->getPort();
    QCOMPARE(_port, portOut);
}

void TCPLinkUnitTest::_nameChangedSignal_test(void)
{
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
    _link->setHostAddress(QHostAddress("127.1.1.1"));
    QCOMPARE(_multiSpy->checkOnlySignalByMask(nameChangedSignalMask), true);
    _multiSpy->clearSignalByIndex(nameChangedSignalIndex);
    
    _link->setPort(42);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(nameChangedSignalMask), true);
    _multiSpy->clearSignalByIndex(nameChangedSignalIndex);    
}

void TCPLinkUnitTest::_connectFail_test(void)
{
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);
    
122 123
    // With the new threading model connect will always succeed. We only get an error signal
    // for a failed connected.
124
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
125 126 127 128 129 130 131

    // Make sure we get a linkError signal with the right link name
    QCOMPARE(_multiSpy->waitForSignalByIndex(communicationErrorSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(communicationErrorSignalMask), true);
    QList<QVariant> arguments = _multiSpy->getSpyByIndex(communicationErrorSignalIndex)->takeFirst();
    QCOMPARE(arguments.at(0).toString(), _link->getName());
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
132
    
133
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
134 135 136

    // Try to connect again to make sure everything was cleaned up correctly from previous failed connection
    
137
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
    
    // Make sure we get a linkError signal with the right link name
    QCOMPARE(_multiSpy->waitForSignalByIndex(communicationErrorSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(communicationErrorSignalMask), true);
    arguments = _multiSpy->getSpyByIndex(communicationErrorSignalIndex)->takeFirst();
    QCOMPARE(arguments.at(0).toString(), _link->getName());
    _multiSpy->clearSignalByIndex(communicationErrorSignalIndex);
}

void TCPLinkUnitTest::_connectSucceed_test(void)
{
    Q_ASSERT(_link);
    Q_ASSERT(_multiSpy);
    Q_ASSERT(_multiSpy->checkNoSignals() == true);

    // Start the server side
154 155
    TCPLoopBackServer* server = new TCPLoopBackServer(_hostAddress, _port);
    Q_CHECK_PTR(server);
Don Gagne's avatar
Don Gagne committed
156 157
    
    // Connect to the server
158
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
159 160
    
    // Make sure we get the two different connected signals
Don Gagne's avatar
Don Gagne committed
161
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 10000), true);
Don Gagne's avatar
Don Gagne committed
162 163 164
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask | connected2SignalMask), true);
    QList<QVariant> arguments = _multiSpy->getSpyByIndex(connected2SignalIndex)->takeFirst();
    QCOMPARE(arguments.at(0).toBool(), true);
165
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
166 167 168
    
    // Test link->server data path
    
169 170
    QByteArray bytesOut("test");

Don Gagne's avatar
Don Gagne committed
171
    // Write data from link to server
172 173 174
    const char* bytesWrittenSignal = SIGNAL(bytesWritten(qint64));
    MultiSignalSpy bytesWrittenSpy;
    QCOMPARE(bytesWrittenSpy.init(_link->getSocket(), &bytesWrittenSignal, 1), true);
Don Gagne's avatar
Don Gagne committed
175
    _link->writeBytes(bytesOut.data(), bytesOut.size());
176 177 178 179 180 181
    _multiSpy->clearAllSignals();
    
    // We emit this signal such that it will be queued and run on the TCPLink thread. This in turn
    // allows the TCPLink object to pump the bytes through.
    connect(this, SIGNAL(waitForBytesWritten(int)), _link, SLOT(waitForBytesWritten(int)));
    emit waitForBytesWritten(1000);
Don Gagne's avatar
Don Gagne committed
182

183
    // Check for loopback, both from signal received and actual bytes returned
Don Gagne's avatar
Don Gagne committed
184

185 186 187
    QCOMPARE(_multiSpy->waitForSignalByIndex(bytesReceivedSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(bytesReceivedSignalMask), true);
    
Don Gagne's avatar
Don Gagne committed
188
    // Read the data and make sure it matches
189 190
    arguments = _multiSpy->getSpyByIndex(bytesReceivedSignalIndex)->takeFirst();
    QVERIFY(arguments.at(1).toByteArray() == bytesOut);
Don Gagne's avatar
Don Gagne committed
191
    
192 193
    _multiSpy->clearAllSignals();

Don Gagne's avatar
Don Gagne committed
194
    // Disconnect the link
195
    _link->_disconnect();
Don Gagne's avatar
Don Gagne committed
196 197 198 199 200 201
    
    // Make sure we get the disconnected signals on link side
    QCOMPARE(_multiSpy->waitForSignalByIndex(disconnectedSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(disconnectedSignalMask | connected2SignalMask), true);
    arguments = _multiSpy->getSpyByIndex(connected2SignalIndex)->takeFirst();
    QCOMPARE(arguments.at(0).toBool(), false);
202
    _multiSpy->clearAllSignals();
Don Gagne's avatar
Don Gagne committed
203 204 205 206
    
    // Try to connect again to make sure everything was cleaned up correctly from previous connection
    
    // Connect to the server
207
    QCOMPARE(_link->_connect(), true);
Don Gagne's avatar
Don Gagne committed
208 209 210 211 212 213
    
    // Make sure we get the two different connected signals
    QCOMPARE(_multiSpy->waitForSignalByIndex(connectedSignalIndex, 1000), true);
    QCOMPARE(_multiSpy->checkOnlySignalByMask(connectedSignalMask | connected2SignalMask), true);
    arguments = _multiSpy->getSpyByIndex(connected2SignalIndex)->takeFirst();
    QCOMPARE(arguments.at(0).toBool(), true);
214 215 216 217
    _multiSpy->clearAllSignals();
    
    server->quit();
    QTest::qWait(500);  // Wait a little for server thread to terminate
Don Gagne's avatar
Don Gagne committed
218
    delete server;
Don Gagne's avatar
Don Gagne committed
219
}