tests/auto/selftests/alive/qtestalive.cpp
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the test suite of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    33 **
       
    34 **
       
    35 **
       
    36 **
       
    37 **
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 
       
    42 
       
    43 #include <qcoreapplication.h>
       
    44 #include <qcoreevent.h>
       
    45 #include <qthread.h>
       
    46 
       
    47 class QTestAliveEvent: public QEvent
       
    48 {
       
    49 public:
       
    50 
       
    51     enum { AliveEventType = QEvent::User + 422 };
       
    52 
       
    53     inline QTestAliveEvent(int aSequenceId)
       
    54         : QEvent(QEvent::Type(AliveEventType)), seqId(aSequenceId)
       
    55     {}
       
    56     inline int sequenceId() const { return seqId; }
       
    57 
       
    58 private:
       
    59     int seqId;
       
    60 };
       
    61 
       
    62 class QTestAlivePinger: public QObject
       
    63 {
       
    64 public:
       
    65     QTestAlivePinger(QObject *receiver, QObject *parent = 0);
       
    66     bool event(QEvent *e);
       
    67 
       
    68 protected:
       
    69     void timerEvent(QTimerEvent *event);
       
    70 
       
    71 private:
       
    72     QObject *rec;
       
    73     int timerId;
       
    74     int currentSequenceId;
       
    75     int lastSequenceId;
       
    76 };
       
    77 
       
    78 QTestAlivePinger::QTestAlivePinger(QObject *receiver, QObject *parent)
       
    79     : QObject(parent), rec(receiver), currentSequenceId(0), lastSequenceId(0)
       
    80 {
       
    81     Q_ASSERT(rec);
       
    82     timerId = startTimer(850);
       
    83 }
       
    84 
       
    85 bool QTestAlivePinger::event(QEvent *event)
       
    86 {
       
    87     // pong received
       
    88     if (int(event->type()) == QTestAliveEvent::AliveEventType) {
       
    89         QTestAliveEvent *e = static_cast<QTestAliveEvent *>(event);
       
    90         //qDebug("PONG %d received", e->sequenceId());
       
    91         // if the events are not delivered in order, we don't care.
       
    92         if (e->sequenceId() > lastSequenceId)
       
    93             lastSequenceId = e->sequenceId();
       
    94         return true;
       
    95     }
       
    96     return QObject::event(event);
       
    97 }
       
    98 
       
    99 void QTestAlivePinger::timerEvent(QTimerEvent *event)
       
   100 {
       
   101     if (event->timerId() != timerId)
       
   102         return;
       
   103 
       
   104     if (lastSequenceId < currentSequenceId - 2) {
       
   105         qWarning("TEST LAGS %d PINGS behind!", currentSequenceId - lastSequenceId);
       
   106     }
       
   107     ++currentSequenceId;
       
   108     //qDebug("PING %d", currentSequenceId);
       
   109     QCoreApplication::postEvent(rec, new QTestAliveEvent(currentSequenceId));
       
   110 }
       
   111 
       
   112 class QTestAlive: public QThread
       
   113 {
       
   114 public:
       
   115     QTestAlive(QObject *parent = 0);
       
   116     ~QTestAlive();
       
   117     void run();
       
   118 
       
   119     bool event(QEvent *e);
       
   120 
       
   121 private:
       
   122     QTestAlivePinger *pinger;
       
   123 };
       
   124 
       
   125 QTestAlive::QTestAlive(QObject *parent)
       
   126     : QThread(parent), pinger(0)
       
   127 {
       
   128 }
       
   129 
       
   130 QTestAlive::~QTestAlive()
       
   131 {
       
   132     quit();
       
   133     while (isRunning());
       
   134 }
       
   135 
       
   136 bool QTestAlive::event(QEvent *e)
       
   137 {
       
   138     if (int(e->type()) == QTestAliveEvent::AliveEventType && pinger) {
       
   139         // ping received, send back the pong
       
   140         //qDebug("PONG %d", static_cast<QTestAliveEvent *>(e)->sequenceId());
       
   141         QCoreApplication::postEvent(pinger,
       
   142                 new QTestAliveEvent(static_cast<QTestAliveEvent *>(e)->sequenceId()));
       
   143         return true;
       
   144     }
       
   145     return QThread::event(e);
       
   146 }
       
   147 
       
   148 void QTestAlive::run()
       
   149 {
       
   150     Q_ASSERT_X(QCoreApplication::instance(), "QTestAlive::run()",
       
   151                "Cannot start QTestAlive without a QCoreApplication instance.");
       
   152 
       
   153     QTestAlivePinger p(this);
       
   154     pinger = &p;
       
   155     exec();
       
   156     pinger = 0;
       
   157 }
       
   158 
       
   159