javacommons/comms/tsrc/src/sendreceive.cpp
branchRCL_3
changeset 19 04becd199f91
child 87 1627c337e51e
equal deleted inserted replaced
16:f5050f1da672 19:04becd199f91
       
     1 /*
       
     2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:  Unit tests for send and sendReceive functionality
       
    15 *
       
    16 */
       
    17 
       
    18 #include <pthread.h>
       
    19 #include <unistd.h>
       
    20 #include "TestHarness.h"
       
    21 #include "monitor.h"
       
    22 #include "logger.h"
       
    23 
       
    24 #include "comms.h"
       
    25 #include "commsclientendpoint.h"
       
    26 #include "commsendpoint.h"
       
    27 #include "echoserver.h"
       
    28 
       
    29 using namespace java::comms;
       
    30 using namespace std;
       
    31 using java::util::Monitor;
       
    32 
       
    33 TEST_GROUP(SendReceive)
       
    34 {
       
    35     EchoServer server;
       
    36     CommsClientEndpoint client;
       
    37 
       
    38     TEST_SETUP()
       
    39     {
       
    40         server.start(IPC_ADDRESS_COMMS_MODULE_TEST);
       
    41         client.connect(IPC_ADDRESS_COMMS_MODULE_TEST);
       
    42     }
       
    43 
       
    44     TEST_TEARDOWN()
       
    45     {
       
    46         client.disconnect();
       
    47         server.stop();
       
    48     }
       
    49 };
       
    50 
       
    51 /**
       
    52  * Test message sending
       
    53  * 1. Sending empty message
       
    54  * 2. Check contents of message (empty message)
       
    55  * 3. Message payload check
       
    56  */
       
    57 
       
    58 TEST(SendReceive, normal)
       
    59 {
       
    60 #ifdef __SYMBIAN32__
       
    61     EXPECT_N_LEAKS(1);
       
    62 #else
       
    63     EXPECT_N_LEAKS(2);
       
    64 #endif
       
    65     CommsMessage msg;
       
    66     msg.setModuleId(MODULE_ID_NO_REPLY);
       
    67 
       
    68     // 1. Sending empty message
       
    69     CHECK(client.send(msg) == 0);
       
    70 
       
    71     // 2. Check contents of message (empty message)
       
    72     CommsMessage msg2;
       
    73     msg2.setModuleId(MODULE_ID_ECHO);
       
    74     CommsMessage receivedMsg;
       
    75 
       
    76     CHECK(client.sendReceive(msg2, receivedMsg, 1) == 0);
       
    77     CHECK(msg2.toString().compare(receivedMsg.toString()) == 0);
       
    78     CHECK(msg2.getModuleId() == receivedMsg.getModuleId());
       
    79 
       
    80     // 3. Message payload check
       
    81     CommsMessage msg3;
       
    82     msg3.setModuleId(MODULE_ID_ECHO);
       
    83     CommsMessage receivedMsg2;
       
    84 
       
    85     int in = 0xCAFEBABE;
       
    86     int zero = 0;
       
    87     int one = 1;
       
    88     int max = 0xFFFFFFFF;
       
    89     int neg = -12345;
       
    90     string ascii = "abcdefghijklmnopqrstyvwxyzABCDEFGHIJKLMNOPQRSTYVWXYZ0123456789";
       
    91     msg3 << ascii << in << zero << one << max << neg;
       
    92 
       
    93     CHECK(client.sendReceive(msg3, receivedMsg2, 1) == 0);
       
    94 
       
    95     string s;
       
    96     int i;
       
    97     receivedMsg2 >> s;
       
    98     CHECK(s.compare(ascii) == 0);
       
    99     receivedMsg2 >> i;
       
   100     CHECK(i == in);
       
   101     receivedMsg2 >> i;
       
   102     CHECK(i == zero);
       
   103     receivedMsg2 >> i;
       
   104     CHECK(i == one);
       
   105     receivedMsg2 >> i;
       
   106     CHECK(i == max);
       
   107     receivedMsg2 >> i;
       
   108     CHECK(i == neg);
       
   109 
       
   110 }
       
   111 
       
   112 /**
       
   113  * Test message sending with wait for ever flag
       
   114  * 1. Check contents of message (empty message)
       
   115  * 2. Message payload check
       
   116  */
       
   117 
       
   118 TEST(SendReceive, normalwaitforever)
       
   119 {
       
   120 #ifdef __SYMBIAN32__
       
   121     EXPECT_N_LEAKS(1);
       
   122 #else
       
   123     EXPECT_N_LEAKS(2);
       
   124 #endif
       
   125     // 1. Check contents of message (empty message)
       
   126     CommsMessage msg2;
       
   127     msg2.setModuleId(MODULE_ID_ECHO);
       
   128     CommsMessage receivedMsg;
       
   129 
       
   130     CHECK(client.sendReceive(msg2, receivedMsg, WAIT_FOR_EVER) == 0);
       
   131     CHECK(msg2.toString().compare(receivedMsg.toString()) == 0);
       
   132     CHECK(msg2.getModuleId() == receivedMsg.getModuleId());
       
   133 
       
   134     // 2. Message payload check
       
   135     CommsMessage msg3;
       
   136     msg3.setModuleId(MODULE_ID_ECHO);
       
   137     CommsMessage receivedMsg2;
       
   138 
       
   139     int in = 0xCAFEBABE;
       
   140     int zero = 0;
       
   141     int one = 1;
       
   142     int max = 0xFFFFFFFF;
       
   143     int neg = -12345;
       
   144     string ascii = "abcdefghijklmnopqrstyvwxyzABCDEFGHIJKLMNOPQRSTYVWXYZ0123456789";
       
   145     msg3 << ascii << in << zero << one << max << neg;
       
   146 
       
   147     CHECK(client.sendReceive(msg3, receivedMsg2, WAIT_FOR_EVER) == 0);
       
   148 
       
   149     string s;
       
   150     int i;
       
   151     receivedMsg2 >> s;
       
   152     CHECK(s.compare(ascii) == 0);
       
   153     receivedMsg2 >> i;
       
   154     CHECK(i == in);
       
   155     receivedMsg2 >> i;
       
   156     CHECK(i == zero);
       
   157     receivedMsg2 >> i;
       
   158     CHECK(i == one);
       
   159     receivedMsg2 >> i;
       
   160     CHECK(i == max);
       
   161     receivedMsg2 >> i;
       
   162     CHECK(i == neg);
       
   163 
       
   164 }
       
   165 
       
   166 /**
       
   167  * Test message sending with big timeout values
       
   168  * 1. OpenC timeout limit
       
   169  * 2. 1h timeout
       
   170  * 3. 24h timeout
       
   171  */
       
   172 
       
   173 TEST(SendReceive, bigtimeout)
       
   174 {
       
   175     EXPECT_N_LEAKS(1);
       
   176     CommsMessage msg;
       
   177     msg.setModuleId(MODULE_ID_ECHO);
       
   178     CommsMessage receivedMsg;
       
   179 
       
   180     // 1. OpenC timeout limit
       
   181     CHECK(client.sendReceive(msg, receivedMsg, 2148) == 0);
       
   182     CHECK(msg.getModuleId() == receivedMsg.getModuleId());
       
   183 
       
   184     // 2. 1h timeout
       
   185     CHECK(client.sendReceive(msg, receivedMsg, 3600) == 0);
       
   186     CHECK(msg.getModuleId() == receivedMsg.getModuleId());
       
   187 
       
   188     // 3. 24h timeout
       
   189     CHECK(client.sendReceive(msg, receivedMsg, 86400) == 0);
       
   190     CHECK(msg.getModuleId() == receivedMsg.getModuleId());
       
   191 }
       
   192 
       
   193 /**
       
   194  * Test message sending
       
   195  * 1. Sending big message
       
   196  */
       
   197 
       
   198 TEST(SendReceive, bigmessage)
       
   199 {
       
   200 #ifdef __SYMBIAN32__
       
   201     EXPECT_N_LEAKS(1);
       
   202 #else
       
   203     EXPECT_N_LEAKS(2);
       
   204 #endif
       
   205     // 1. Sending big message
       
   206     int SIZE = 60000; // 60k
       
   207     char* array = new char[SIZE];
       
   208     for (int i = 0; i < SIZE; i++)
       
   209     {
       
   210         array[i] = i;
       
   211     }
       
   212     string input(array, SIZE);
       
   213     delete[] array;
       
   214 
       
   215     CommsMessage msg;
       
   216     msg.setModuleId(MODULE_ID_ECHO);
       
   217     msg << input;
       
   218 
       
   219     CommsMessage receivedMsg;
       
   220     CHECK(client.sendReceive(msg, receivedMsg, 1) == 0);
       
   221 
       
   222     string s;
       
   223     receivedMsg >> s;
       
   224     CHECK(s.compare(input) == 0);
       
   225 }
       
   226 
       
   227 
       
   228 /**
       
   229  * Test reply from listener
       
   230  * 1. sendReceive and reply from listener
       
   231  */
       
   232 
       
   233 TEST(SendReceive, listener)
       
   234 {
       
   235     // 1. sendReceive and reply from listener
       
   236     int MODULEID = 666;
       
   237     CommsEndpoint* comms = server.getComms();
       
   238     EchoListener listener(comms);
       
   239 
       
   240     CHECK(!comms->registerListener(MODULEID, &listener));
       
   241 
       
   242     CommsMessage msg;
       
   243     msg.setModuleId(MODULEID);
       
   244     msg << "hello world";
       
   245 
       
   246     CommsMessage receivedMsg;
       
   247     CHECK(client.sendReceive(msg, receivedMsg, 1) == 0);
       
   248 
       
   249     string s;
       
   250     receivedMsg >> s;
       
   251     CHECK(s.compare("hello world") == 0);
       
   252 
       
   253     CHECK(!comms->unregisterListener(MODULEID, &listener));
       
   254 }
       
   255 
       
   256 
       
   257 /**
       
   258  * Test sendReceive timeout
       
   259  * 1. sendReceive no timeout
       
   260  * 2. sendReceive timeout
       
   261  */
       
   262 
       
   263 TEST(SendReceive, timeouts)
       
   264 {
       
   265     EXPECT_N_LEAKS(1);
       
   266     // 1. sendReceive no timeout
       
   267     CommsMessage msg;
       
   268     msg.setModuleId(MODULE_ID_SLEEP_1S);
       
   269 
       
   270     CommsMessage receivedMsg;
       
   271     CHECK(client.sendReceive(msg, receivedMsg, 2) == 0);
       
   272 
       
   273     // 2. sendReceive timeout
       
   274     msg.setModuleId(MODULE_ID_NO_REPLY);
       
   275     CHECK(client.sendReceive(msg, receivedMsg, 1) != 0);
       
   276 }
       
   277 
       
   278 
       
   279 /**
       
   280  * Test send error cases
       
   281  * 1. send when not connected
       
   282  * 2. sendReceive when not connected
       
   283  * 3. send to non-existing endpoint
       
   284  * 4. invalid timeout values
       
   285  */
       
   286 
       
   287 TEST(SendReceive, errors)
       
   288 {
       
   289     EXPECT_N_LEAKS(1);
       
   290     CommsClientEndpoint con;
       
   291 
       
   292     // 1. send when not connected
       
   293     CommsMessage msg;
       
   294     msg.setModuleId(MODULE_ID_ECHO);
       
   295     CHECK(!con.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   296     CHECK(!con.send(msg));
       
   297     CHECK(!con.disconnect());
       
   298     CHECK(con.send(msg)!=0);
       
   299 
       
   300     // 2. sendReceive when not connected
       
   301     CommsMessage receivedMsg;
       
   302     CHECK(!con.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   303     CHECK(!con.sendReceive(msg, receivedMsg, 2));
       
   304     CHECK(!con.sendReceive(msg, receivedMsg, WAIT_FOR_EVER));
       
   305     CHECK(!con.disconnect());
       
   306     CHECK(con.sendReceive(msg, receivedMsg, 2)!=0);
       
   307 
       
   308     // 3. send to non-existing endpoint
       
   309     CommsMessage dummy;
       
   310     dummy.setReceiver(666);
       
   311     CHECK(server.getComms()->send(msg) != 0);
       
   312 
       
   313     // 4. invalid timeout values
       
   314     msg.setModuleId(MODULE_ID_ECHO);
       
   315     CHECK(!con.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   316     CHECK(con.sendReceive(msg, receivedMsg, 0)!=0);
       
   317     CHECK(con.sendReceive(msg, receivedMsg, -2)!=0);
       
   318     CHECK(!con.disconnect());
       
   319 }
       
   320 
       
   321 /**
       
   322  * Test multithread send (client)
       
   323  * 1. send in multiple threads
       
   324  * 2. sendReceive in multiple threads
       
   325  */
       
   326 
       
   327 void doSends(CommsClientEndpoint* aComms)
       
   328 {
       
   329     CommsMessage msg;
       
   330     msg.setModuleId(MODULE_ID_ECHO);
       
   331     CommsMessage receivedMsg;
       
   332 
       
   333     for (int i = 0; i < 5; i++)
       
   334     {
       
   335         CHECK(aComms->send(msg) == 0);
       
   336         CHECK(aComms->sendReceive(msg, receivedMsg, 1) == 0);
       
   337         CHECK(aComms->sendReceive(msg, receivedMsg, WAIT_FOR_EVER) == 0);
       
   338         usleep(300000); //0.3s
       
   339     }
       
   340 }
       
   341 
       
   342 void* clientSendThread(void* aComms)
       
   343 {
       
   344     CommsClientEndpoint* comms = reinterpret_cast<CommsClientEndpoint*>(aComms);
       
   345     doSends(comms);
       
   346     return 0;
       
   347 }
       
   348 
       
   349 TEST(SendReceive, multiThreadClient)
       
   350 {
       
   351     EXPECT_N_LEAKS(1);
       
   352     CommsClientEndpoint con;
       
   353     CHECK(!con.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   354 
       
   355     const int NUM_THREADS = 5;
       
   356     pthread_t thread[NUM_THREADS];
       
   357 
       
   358     for (int i=0; i<NUM_THREADS; i++)
       
   359     {
       
   360         pthread_create(&thread[i], 0, &clientSendThread, &con);
       
   361     }
       
   362 
       
   363     for (int i=0; i<NUM_THREADS; i++)
       
   364     {
       
   365         pthread_join(thread[i], 0);
       
   366     }
       
   367 
       
   368     CHECK(!con.disconnect());
       
   369 }
       
   370 
       
   371 
       
   372 class ReplyListener : public CommsListener
       
   373 {
       
   374 public:
       
   375     ReplyListener(CommsEndpoint& aComms, Monitor& aMonitor)
       
   376             : mComms(aComms), mMonitor(aMonitor), mReplyCount(30) {}
       
   377     virtual void processMessage(CommsMessage& aMessage)
       
   378     {
       
   379         CommsMessage reply;
       
   380         reply.replyTo(aMessage);
       
   381         //ELOG1(EJavaComms, "ReplyListener count=%d", mReplyCount);
       
   382         mComms.send(reply);
       
   383 
       
   384         if (--mReplyCount < 0)
       
   385         {
       
   386             mMonitor.notify();
       
   387         }
       
   388         //ELOG1(EJavaComms, "ReplyListener after send count=%d", mReplyCount);
       
   389     }
       
   390 private:
       
   391     CommsEndpoint& mComms;
       
   392     Monitor& mMonitor;
       
   393     int mReplyCount;
       
   394 };
       
   395 
       
   396 TEST(SendReceive, sendsInProcessMessage)
       
   397 {
       
   398     EXPECT_N_LEAKS(1);
       
   399     std::auto_ptr<Monitor> monitor(Monitor::createMonitor());
       
   400 
       
   401     CommsClientEndpoint con;
       
   402     ReplyListener listener(con, *monitor);
       
   403 
       
   404     CHECK(!con.connect(IPC_ADDRESS_COMMS_MODULE_TEST));
       
   405     CHECK(!con.registerDefaultListener(&listener));
       
   406 
       
   407     CommsMessage msg;
       
   408     CHECK(!con.send(msg));
       
   409 
       
   410     monitor->wait();
       
   411     CHECK(!con.disconnect());
       
   412 }