tests/auto/qlocalsocket/tst_qlocalsocket.cpp
changeset 33 3e2da88830cd
parent 18 2f34d5167611
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   113     void multiConnect();
   113     void multiConnect();
   114     void writeOnlySocket();
   114     void writeOnlySocket();
   115     void writeToClientAndDisconnect();
   115     void writeToClientAndDisconnect();
   116     void debug();
   116     void debug();
   117     void bytesWrittenSignal();
   117     void bytesWrittenSignal();
   118 
   118     void syncDisconnectNotify();
       
   119     void asyncDisconnectNotify();
   119 
   120 
   120 #ifdef Q_OS_SYMBIAN
   121 #ifdef Q_OS_SYMBIAN
   121 private:
   122 private:
   122     void unlink(QString serverName);
   123     void unlink(QString serverName);
   123 #endif
   124 #endif
   681     void run()
   682     void run()
   682     {
   683     {
   683         QString testLine = "test";
   684         QString testLine = "test";
   684         LocalSocket socket;
   685         LocalSocket socket;
   685         QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead()));
   686         QSignalSpy spyReadyRead(&socket, SIGNAL(readyRead()));
   686         int tries = 0;
   687         socket.connectToServer("qlocalsocket_threadtest");
   687         do {
   688         QVERIFY(socket.waitForConnected(1000));
   688             socket.connectToServer("qlocalsocket_threadtest");
       
   689             if (socket.error() != QLocalSocket::ServerNotFoundError
       
   690                 && socket.error() != QLocalSocket::ConnectionRefusedError)
       
   691                 break;
       
   692             QTest::qWait(100);
       
   693             ++tries;
       
   694         } while ((socket.error() == QLocalSocket::ServerNotFoundError
       
   695                   || socket.error() == QLocalSocket::ConnectionRefusedError)
       
   696              && tries < 1000);
       
   697         if (tries == 0 && socket.state() != QLocalSocket::ConnectedState) {
       
   698             QVERIFY(socket.waitForConnected(7000));
       
   699             QVERIFY(socket.state() == QLocalSocket::ConnectedState);
       
   700         }
       
   701 
   689 
   702         // We should *not* have this signal yet!
   690         // We should *not* have this signal yet!
   703         if (tries == 0)
   691         QCOMPARE(spyReadyRead.count(), 0);
   704             QCOMPARE(spyReadyRead.count(), 0);
       
   705         socket.waitForReadyRead();
   692         socket.waitForReadyRead();
   706         QCOMPARE(spyReadyRead.count(), 1);
   693         QCOMPARE(spyReadyRead.count(), 1);
   707         QTextStream in(&socket);
   694         QTextStream in(&socket);
   708         QCOMPARE(in.readLine(), testLine);
   695         QCOMPARE(in.readLine(), testLine);
   709         socket.close();
   696         socket.close();
   713 class Server : public QThread
   700 class Server : public QThread
   714 {
   701 {
   715 
   702 
   716 public:
   703 public:
   717     int clients;
   704     int clients;
       
   705     QMutex mutex;
       
   706     QWaitCondition wc;
   718     void run()
   707     void run()
   719     {
   708     {
   720         QString testLine = "test";
   709         QString testLine = "test";
   721         LocalServer server;
   710         LocalServer server;
   722         server.setMaxPendingConnections(10);
   711         server.setMaxPendingConnections(10);
   723         QVERIFY2(server.listen("qlocalsocket_threadtest"),
   712         QVERIFY2(server.listen("qlocalsocket_threadtest"),
   724                  server.errorString().toLatin1().constData());
   713                  server.errorString().toLatin1().constData());
       
   714         mutex.lock();
       
   715         wc.wakeAll();
       
   716         mutex.unlock();
   725         int done = clients;
   717         int done = clients;
   726         while (done > 0) {
   718         while (done > 0) {
   727             bool timedOut = true;
   719             bool timedOut = true;
   728             QVERIFY(server.waitForNewConnection(7000, &timedOut));
   720             QVERIFY(server.waitForNewConnection(7000, &timedOut));
   729             QVERIFY(!timedOut);
   721             QVERIFY(!timedOut);
   744 void tst_QLocalSocket::threadedConnection_data()
   736 void tst_QLocalSocket::threadedConnection_data()
   745 {
   737 {
   746     QTest::addColumn<int>("threads");
   738     QTest::addColumn<int>("threads");
   747     QTest::newRow("1 client") << 1;
   739     QTest::newRow("1 client") << 1;
   748     QTest::newRow("2 clients") << 2;
   740     QTest::newRow("2 clients") << 2;
   749 #ifdef Q_OS_WINCE
       
   750     QTest::newRow("4 clients") << 4;
       
   751 #endif
       
   752 #ifndef Q_OS_WIN
       
   753     QTest::newRow("5 clients") << 5;
   741     QTest::newRow("5 clients") << 5;
       
   742 #ifndef Q_OS_WINCE
   754     QTest::newRow("10 clients") << 10;
   743     QTest::newRow("10 clients") << 10;
   755 #endif
       
   756 #ifndef Q_OS_WINCE
       
   757     QTest::newRow("20 clients") << 20;
   744     QTest::newRow("20 clients") << 20;
   758 #endif
   745 #endif
   759 }
   746 }
   760 
   747 
   761 void tst_QLocalSocket::threadedConnection()
   748 void tst_QLocalSocket::threadedConnection()
   768     Server server;
   755     Server server;
   769 #if defined(Q_OS_SYMBIAN)
   756 #if defined(Q_OS_SYMBIAN)
   770     server.setStackSize(0x14000);
   757     server.setStackSize(0x14000);
   771 #endif
   758 #endif
   772     server.clients = threads;
   759     server.clients = threads;
       
   760     server.mutex.lock();
   773     server.start();
   761     server.start();
       
   762     server.wc.wait(&server.mutex);
   774 
   763 
   775     QList<Client*> clients;
   764     QList<Client*> clients;
   776     for (int i = 0; i < threads; ++i) {
   765     for (int i = 0; i < threads; ++i) {
   777         clients.append(new Client());
   766         clients.append(new Client());
   778 #if defined(Q_OS_SYMBIAN)
   767 #if defined(Q_OS_SYMBIAN)
   782     }
   771     }
   783 
   772 
   784     server.wait();
   773     server.wait();
   785     while (!clients.isEmpty()) {
   774     while (!clients.isEmpty()) {
   786         QVERIFY(clients.first()->wait(3000));
   775         QVERIFY(clients.first()->wait(3000));
   787         Client *client =clients.takeFirst();
   776         delete clients.takeFirst();
   788         client->terminate();
       
   789         delete client;
       
   790     }
   777     }
   791 }
   778 }
   792 
   779 
   793 void tst_QLocalSocket::processConnection_data()
   780 void tst_QLocalSocket::processConnection_data()
   794 {
   781 {
   992     unlink("writeAndDisconnectServer");
   979     unlink("writeAndDisconnectServer");
   993 #endif
   980 #endif
   994 
   981 
   995     QLocalServer server;
   982     QLocalServer server;
   996     QLocalSocket client;
   983     QLocalSocket client;
       
   984     QSignalSpy readChannelFinishedSpy(&client, SIGNAL(readChannelFinished()));
   997 
   985 
   998     QVERIFY(server.listen("writeAndDisconnectServer"));
   986     QVERIFY(server.listen("writeAndDisconnectServer"));
   999     client.connectToServer("writeAndDisconnectServer");
   987     client.connectToServer("writeAndDisconnectServer");
  1000     QVERIFY(client.waitForConnected(200));
   988     QVERIFY(client.waitForConnected(200));
  1001     QVERIFY(server.waitForNewConnection(200));
   989     QVERIFY(server.waitForNewConnection(200));
  1004 
   992 
  1005     char buffer[100];
   993     char buffer[100];
  1006     memset(buffer, 0, sizeof(buffer));
   994     memset(buffer, 0, sizeof(buffer));
  1007     QCOMPARE(clientSocket->write(buffer, sizeof(buffer)), (qint64)sizeof(buffer));
   995     QCOMPARE(clientSocket->write(buffer, sizeof(buffer)), (qint64)sizeof(buffer));
  1008     clientSocket->waitForBytesWritten();
   996     clientSocket->waitForBytesWritten();
  1009     clientSocket->disconnectFromServer();
   997     clientSocket->close();
  1010     QVERIFY(client.waitForReadyRead());
   998     server.close();
       
   999 
       
  1000     QTRY_COMPARE(readChannelFinishedSpy.count(), 1);
  1011     QCOMPARE(client.read(buffer, sizeof(buffer)), (qint64)sizeof(buffer));
  1001     QCOMPARE(client.read(buffer, sizeof(buffer)), (qint64)sizeof(buffer));
  1012     QVERIFY(client.waitForDisconnected());
  1002     client.waitForDisconnected();
  1013     QCOMPARE(client.state(), QLocalSocket::UnconnectedState);
  1003     QCOMPARE(client.state(), QLocalSocket::UnconnectedState);
  1014 }
  1004 }
  1015 
  1005 
  1016 void tst_QLocalSocket::debug()
  1006 void tst_QLocalSocket::debug()
  1017 {
  1007 {
  1063     QVERIFY(!timedOut);
  1053     QVERIFY(!timedOut);
  1064     QTest::qWait(2000);
  1054     QTest::qWait(2000);
  1065     QVERIFY(writeThread.wait(2000));
  1055     QVERIFY(writeThread.wait(2000));
  1066 }
  1056 }
  1067 
  1057 
       
  1058 void tst_QLocalSocket::syncDisconnectNotify()
       
  1059 {
       
  1060 #ifdef Q_OS_SYMBIAN
       
  1061     unlink("syncDisconnectNotify");
       
  1062 #endif
       
  1063 
       
  1064     QLocalServer server;
       
  1065     QVERIFY(server.listen("syncDisconnectNotify"));
       
  1066     QLocalSocket client;
       
  1067     client.connectToServer("syncDisconnectNotify");
       
  1068     QVERIFY(server.waitForNewConnection());
       
  1069     QLocalSocket* serverSocket = server.nextPendingConnection();
       
  1070     QVERIFY(serverSocket);
       
  1071     delete serverSocket;
       
  1072     QCOMPARE(client.waitForReadyRead(), false);
       
  1073 }
       
  1074 
       
  1075 void tst_QLocalSocket::asyncDisconnectNotify()
       
  1076 {
       
  1077 #ifdef Q_OS_SYMBIAN
       
  1078     unlink("asyncDisconnectNotify");
       
  1079 #endif
       
  1080 
       
  1081     QLocalServer server;
       
  1082     QVERIFY(server.listen("asyncDisconnectNotify"));
       
  1083     QLocalSocket client;
       
  1084     QSignalSpy disconnectedSpy(&client, SIGNAL(disconnected()));
       
  1085     client.connectToServer("asyncDisconnectNotify");
       
  1086     QVERIFY(server.waitForNewConnection());
       
  1087     QLocalSocket* serverSocket = server.nextPendingConnection();
       
  1088     QVERIFY(serverSocket);
       
  1089     delete serverSocket;
       
  1090     QTRY_VERIFY(!disconnectedSpy.isEmpty());
       
  1091 }
       
  1092 
  1068 #ifdef Q_OS_SYMBIAN
  1093 #ifdef Q_OS_SYMBIAN
  1069 void tst_QLocalSocket::unlink(QString name)
  1094 void tst_QLocalSocket::unlink(QString name)
  1070 {
  1095 {
  1071     if(name.length() == 0)
  1096     if(name.length() == 0)
  1072         return;
  1097         return;