summaryrefslogtreecommitdiffstats
path: root/libcommuni/tests/auto/irclagtimer/tst_irclagtimer.cpp
blob: 3a69042b0517f6460eef5c5d9a8c6fc99aeda9da (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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
/*
 * Copyright (C) 2008-2014 The Communi Project
 *
 * This test is free, and not covered by the BSD license. There is no
 * restriction applied to their modification, redistribution, using and so on.
 * You can study them, modify them, use them in your own program - either
 * completely or partially.
 */

#include "irclagtimer.h"
#include "ircconnection.h"
#include "tst_ircclientserver.h"
#include "tst_ircdata.h"
#include <QtTest/QtTest>

class tst_IrcLagTimer : public tst_IrcClientServer
{
    Q_OBJECT

private slots:
    void testDefaults();
    void testInterval();
    void testConnection();
    void testLag();
};

void tst_IrcLagTimer::testDefaults()
{
    IrcLagTimer timer;
    QCOMPARE(timer.lag(), qint64(-1));
    QVERIFY(!timer.connection());
    QCOMPARE(timer.interval(), 60);
}

void tst_IrcLagTimer::testInterval()
{
    IrcLagTimer timer;
    timer.setInterval(INT_MIN);
    QCOMPARE(timer.interval(), INT_MIN);
    timer.setInterval(0);
    QCOMPARE(timer.interval(), 0);
    timer.setInterval(INT_MAX);
    QCOMPARE(timer.interval(), INT_MAX);
}

void tst_IrcLagTimer::testConnection()
{
    IrcLagTimer timer(connection);
    QCOMPARE(timer.connection(), connection.data());
    timer.setConnection(0);
    QVERIFY(!timer.connection());
    timer.setConnection(connection);
    QCOMPARE(timer.connection(), connection.data());
}

void tst_IrcLagTimer::testLag()
{
#if QT_VERSION >= 0x040700
    IrcLagTimer timer(connection);

    QSignalSpy lagSpy(&timer, SIGNAL(lagChanged(qint64)));
    QVERIFY(lagSpy.isValid());
    int lagCount = 0;

    connection->open();
    QVERIFY(waitForOpened());

    QCOMPARE(timer.lag(), -1ll);

    QVERIFY(waitForWritten(tst_IrcData::welcome()));

    // cheat a bit to avoid waiting a 1s interval at minimum...
    QMetaObject::invokeMethod(&timer, "_irc_pingServer");
    QVERIFY(clientSocket->waitForBytesWritten(1000));
    QVERIFY(serverSocket->waitForReadyRead(1000));

    QRegExp rx("PING communi/(\\d+)");
    QString written = QString::fromUtf8(serverSocket->readAll());
    QVERIFY(rx.indexIn(written) != -1);

    waitForWritten(QString(":irc.ser.ver PONG communi communi/%1").arg(QDateTime::currentMSecsSinceEpoch() - 1234ll).toUtf8());
    QVERIFY(timer.lag() >= 1234ll);
    QCOMPARE(lagSpy.count(), ++lagCount);
    QVERIFY(lagSpy.last().at(0).toLongLong() >= 1234ll);

    timer.setConnection(0);
    QCOMPARE(timer.lag(), -1ll);
    QCOMPARE(lagSpy.count(), ++lagCount);
    QCOMPARE(lagSpy.last().at(0).toLongLong(), -1ll);

    timer.setConnection(connection);
    QCOMPARE(timer.lag(), -1ll);
    QCOMPARE(lagSpy.count(), lagCount);

    waitForWritten(QString(":irc.ser.ver PONG communi communi/%1").arg(QDateTime::currentMSecsSinceEpoch() - 4321ll).toUtf8());
    QVERIFY(timer.lag() >= 4321ll);
    QCOMPARE(lagSpy.count(), ++lagCount);
    QVERIFY(lagSpy.last().at(0).toLongLong() >= 4321ll);

    connection->close();
    QCOMPARE(timer.lag(), -1ll);
    QCOMPARE(lagSpy.count(), ++lagCount);
    QCOMPARE(lagSpy.last().at(0).toLongLong(), -1ll);
#endif // QT_VERSION >= 0x040700
}

QTEST_MAIN(tst_IrcLagTimer)

#include "tst_irclagtimer.moc"