javamanager/javacaptain/tsrc/test_runner/src/timerserverjavatimetest.cpp
branchRCL_3
changeset 14 04becd199f91
equal deleted inserted replaced
13:f5050f1da672 14: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:  TimerServerJavaTime
       
    15 *
       
    16 */
       
    17 
       
    18 #include "TestHarness.h"
       
    19 
       
    20 #include <sys/time.h>
       
    21 #include <time.h>
       
    22 
       
    23 
       
    24 #include "commsclientendpoint.h"
       
    25 #include "commslistener.h"
       
    26 #include "comms.h"
       
    27 #include "monitor.h"
       
    28 
       
    29 #include "testmessages.h"
       
    30 #include "timerserverhelpers.h"
       
    31 
       
    32 using namespace java::comms;
       
    33 using namespace java::captain;
       
    34 using namespace java::util;
       
    35 
       
    36 extern java::comms::CommsClientEndpoint* commsClient;
       
    37 
       
    38 static Monitor* monitor1;
       
    39 static Monitor* monitor2;
       
    40 static int      timerId;
       
    41 
       
    42 class JavaTime
       
    43 {
       
    44 public:
       
    45     JavaTime():mMillisFromEpoch(0LL) {}
       
    46     JavaTime(const long long& aTime):mMillisFromEpoch(aTime) {}
       
    47     JavaTime(const JavaTime& aJt):mMillisFromEpoch(aJt.getTime()) {}
       
    48     JavaTime& operator= (const JavaTime& aJt)
       
    49     {
       
    50         mMillisFromEpoch = aJt.getTime();
       
    51         return *this;
       
    52     }
       
    53     const long long& operator+ (const long long& a)
       
    54     {
       
    55         return (this->mMillisFromEpoch += a);
       
    56     }
       
    57     virtual ~JavaTime() {}
       
    58 
       
    59     virtual long long getTime() const
       
    60     {
       
    61         return mMillisFromEpoch;
       
    62     }
       
    63     virtual void setTime(const long long& aNewTime)
       
    64     {
       
    65         mMillisFromEpoch = aNewTime;
       
    66     }
       
    67 
       
    68 private:
       
    69     long long mMillisFromEpoch;
       
    70 };
       
    71 
       
    72 long long getCurrentJavaTime()
       
    73 {
       
    74     timeval tim;
       
    75     int err = gettimeofday(&tim, NULL);
       
    76     if (-1 == err)
       
    77     {
       
    78         return -1LL;
       
    79     }
       
    80 
       
    81     return (tim.tv_sec * 1000LL) +
       
    82            (tim.tv_usec / 1000LL);
       
    83 }
       
    84 
       
    85 
       
    86 TEST_GROUP(TimerServerJavaTime)
       
    87 {
       
    88     tsCommsListener* tsListener;
       
    89     TEST_SETUP()
       
    90     {
       
    91         monitor1 = Monitor::createMonitor();
       
    92         monitor2 = Monitor::createMonitor();
       
    93         tsListener = new tsCommsListener(monitor1, monitor2, timerId);
       
    94         commsClient->registerDefaultListener(tsListener);
       
    95     }
       
    96 
       
    97     TEST_TEARDOWN()
       
    98     {
       
    99         commsClient->unregisterDefaultListener(tsListener);
       
   100         delete tsListener;
       
   101         delete monitor2;
       
   102         delete monitor1;
       
   103     }
       
   104 };
       
   105 
       
   106 static void omaEquals(int& a, int&b)
       
   107 {
       
   108     if (a != b)
       
   109         printf("Comparing 0x%x == 0x%x fails!!!\n", a, b);
       
   110     CHECK(a == b);
       
   111 }
       
   112 /*
       
   113 TEST(TimerServerJavaTime, TS10_STOP)
       
   114 {
       
   115     int timerId = 0;
       
   116     startTimer(timerId, 10);
       
   117     stopTimer(timerId);
       
   118 }
       
   119 
       
   120 TEST(TimerServerJavaTime, TS09_3_timers_a2_b2_c2_seconds_c_canceled)
       
   121 {
       
   122     int tid1=0, tid2=0, tid3=0;
       
   123     startTimer(tid1, 2);
       
   124     startTimer(tid2, 2);
       
   125     startTimer(tid3, 2);
       
   126     stopTimer(tid3);
       
   127     monitor->wait();
       
   128     CHECK(receivedTimeout==tid2 || receivedTimeout==tid1);
       
   129     monitor2->notify();
       
   130 
       
   131     monitor->wait();
       
   132     CHECK(receivedTimeout==tid2 || receivedTimeout==tid1);
       
   133     monitor2->notify();
       
   134 }
       
   135 
       
   136 TEST(TimerServerJavaTime, TS08_3_timers_a1_b2_c3_seconds_b_canceled)
       
   137 {   int tid1=0, tid2=0, tid3=0;
       
   138     startTimer(tid1, 1);
       
   139     startTimer(tid2, 2);
       
   140     stopTimer(tid2);
       
   141     startTimer(tid3, 3);
       
   142     monitor->wait();
       
   143     CHECK(receivedTimeout==tid1 || receivedTimeout==tid3);
       
   144     monitor2->notify();
       
   145 
       
   146     monitor->wait();
       
   147     CHECK(receivedTimeout==tid1 || receivedTimeout==tid3);
       
   148     monitor2->notify();
       
   149 }
       
   150 
       
   151 TEST(TimerServerJavaTime, TS07_3_timers_a3_b2_c1_seconds_a_canceled)
       
   152 {
       
   153     int tid1=0, tid2=0, tid3=0;
       
   154     startTimer(tid1, 3);
       
   155     startTimer(tid2, 2);
       
   156     stopTimer(tid1);
       
   157     startTimer(tid3, 1);
       
   158     monitor->wait();
       
   159     CHECK(receivedTimeout==tid2 || receivedTimeout==tid3);
       
   160     monitor2->notify();
       
   161 
       
   162     monitor->wait();
       
   163     CHECK(receivedTimeout==tid2 || receivedTimeout==tid3);
       
   164     monitor2->notify();
       
   165 }
       
   166 
       
   167 TEST(TimerServerJavaTime, TS06_3_timers_2_1_2_seconds)
       
   168 {
       
   169     int tid1=0, tid2=0, tid3=0;
       
   170     startTimer(tid1, 2);
       
   171     startTimer(tid2, 1);
       
   172     startTimer(tid3, 2);
       
   173     monitor->wait();
       
   174     omaEquals(tid2, receivedTimeout);
       
   175 //    CHECK(tid2 == receivedTimeout);
       
   176     monitor2->notify();
       
   177 
       
   178     monitor->wait();
       
   179     CHECK(receivedTimeout==tid1 || receivedTimeout==tid3);
       
   180     monitor2->notify();
       
   181 
       
   182     monitor->wait();
       
   183     CHECK(receivedTimeout==tid1 || receivedTimeout==tid3);
       
   184     monitor2->notify();
       
   185 }
       
   186 
       
   187 
       
   188 TEST(TimerServerJavaTime, TS05_3_timers_1_2_3_seconds)
       
   189 {
       
   190     int tid1=0, tid2=0, tid3=0;
       
   191     startTimer(tid1, 1);
       
   192     startTimer(tid2, 2);
       
   193     startTimer(tid3, 3);
       
   194     monitor->wait();
       
   195     omaEquals(tid1, receivedTimeout);
       
   196 //    CHECK(tid1 == receivedTimeout);
       
   197     monitor2->notify();
       
   198 
       
   199     monitor->wait();
       
   200     omaEquals(tid2, receivedTimeout);
       
   201 //    CHECK(tid2 == receivedTimeout);
       
   202     monitor2->notify();
       
   203 
       
   204     monitor->wait();
       
   205     omaEquals(tid3, receivedTimeout);
       
   206 //    CHECK(tid3 == receivedTimeout);
       
   207     monitor2->notify();
       
   208 }
       
   209 */
       
   210 TEST(TimerServerJavaTime, TSJT02_01_3_timers_2_1_2_seconds)
       
   211 {
       
   212     int tid1=0, tid2=0, tid3=0;
       
   213     startJavaTimeTimer(tid1, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   214     startJavaTimeTimer(tid2, JavaTime(getCurrentJavaTime()+(1L * 1000L)).getTime());
       
   215     startJavaTimeTimer(tid3, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   216 
       
   217     monitor1->wait();
       
   218     CHECK(tid2 == timerId);
       
   219     monitor2->notify();
       
   220 
       
   221     monitor1->wait();
       
   222     CHECK(tid1 == timerId || tid3 == timerId);
       
   223     monitor2->notify();
       
   224 
       
   225     monitor1->wait();
       
   226     CHECK(tid1 == timerId || tid3 == timerId);
       
   227     monitor2->notify();
       
   228 }
       
   229 
       
   230 TEST(TimerServerJavaTime, TSJT02_02_3_timers_1_2_3_seconds)
       
   231 {
       
   232     int tid1=0, tid2=0, tid3=0;
       
   233     startJavaTimeTimer(tid1, JavaTime(getCurrentJavaTime()+(1L * 1000L)).getTime());
       
   234     startJavaTimeTimer(tid2, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   235     startJavaTimeTimer(tid3, JavaTime(getCurrentJavaTime()+(3L * 1000L)).getTime());
       
   236 
       
   237     monitor1->wait();
       
   238     CHECK(tid1 == timerId);
       
   239     monitor2->notify();
       
   240 
       
   241     monitor1->wait();
       
   242     CHECK(tid2 == timerId);
       
   243     monitor2->notify();
       
   244 
       
   245     monitor1->wait();
       
   246     CHECK(tid3 == timerId);
       
   247     monitor2->notify();
       
   248 }
       
   249 
       
   250 TEST(TimerServerJavaTime, TSJT02_01_3_timers_3_2_1_seconds)
       
   251 {
       
   252     int tid1=0, tid2=0, tid3=0;
       
   253     startJavaTimeTimer(tid1, JavaTime(getCurrentJavaTime()+(3L * 1000L)).getTime());
       
   254     startJavaTimeTimer(tid2, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   255     startJavaTimeTimer(tid3, JavaTime(getCurrentJavaTime()+(1L * 1000L)).getTime());
       
   256 
       
   257     monitor1->wait();
       
   258     CHECK(tid3 == timerId);
       
   259     monitor2->notify();
       
   260 
       
   261     monitor1->wait();
       
   262     CHECK(tid2 == timerId);
       
   263     monitor2->notify();
       
   264 
       
   265     monitor1->wait();
       
   266     CHECK(tid1 == timerId);
       
   267     monitor2->notify();
       
   268 }
       
   269 
       
   270 TEST(TimerServerJavaTime, TSJT01_05_5_timers_2_2_2_2_2_seconds)
       
   271 {
       
   272     int tid1=0, tid2=0, tid3=0, tid4=0, tid5=0;
       
   273     JavaTime jt(getCurrentJavaTime()+(2L * 1000L));
       
   274     startJavaTimeTimer(tid1, jt.getTime());
       
   275     startJavaTimeTimer(tid2, jt.getTime());
       
   276     startJavaTimeTimer(tid3, jt.getTime());
       
   277     startJavaTimeTimer(tid4, jt.getTime());
       
   278     startJavaTimeTimer(tid5, jt.getTime());
       
   279 
       
   280     monitor1->wait();
       
   281     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4 || timerId==tid5);
       
   282     monitor2->notify();
       
   283     monitor1->wait();
       
   284     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4 || timerId==tid5);
       
   285     monitor2->notify();
       
   286     monitor1->wait();
       
   287     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4 || timerId==tid5);
       
   288     monitor2->notify();
       
   289     monitor1->wait();
       
   290     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4 || timerId==tid5);
       
   291     monitor2->notify();
       
   292     monitor1->wait();
       
   293     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4 || timerId==tid5);
       
   294     monitor2->notify();
       
   295 }
       
   296 
       
   297 TEST(TimerServerJavaTime, TSJT01_04_4_timers_2_2_2_2_seconds)
       
   298 {
       
   299     int tid1=0, tid2=0, tid3=0, tid4=0;
       
   300     startJavaTimeTimer(tid1, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   301     startJavaTimeTimer(tid2, JavaTime(getCurrentJavaTime()+(1900L)).getTime());
       
   302     startJavaTimeTimer(tid3, JavaTime(getCurrentJavaTime()+(1800L)).getTime());
       
   303     startJavaTimeTimer(tid4, JavaTime(getCurrentJavaTime()+(1700L)).getTime());
       
   304 
       
   305     monitor1->wait();
       
   306     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4);
       
   307     monitor2->notify();
       
   308     monitor1->wait();
       
   309     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4);
       
   310     monitor2->notify();
       
   311     monitor1->wait();
       
   312     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4);
       
   313     monitor2->notify();
       
   314     monitor1->wait();
       
   315     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3 || timerId==tid4);
       
   316     monitor2->notify();
       
   317 }
       
   318 
       
   319 TEST(TimerServerJavaTime, TSJT01_03_3_timers_2_2_2_seconds)
       
   320 {
       
   321     int tid1=0, tid2=0, tid3=0;
       
   322     startJavaTimeTimer(tid1, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   323     startJavaTimeTimer(tid2, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   324     startJavaTimeTimer(tid3, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   325 
       
   326     monitor1->wait();
       
   327     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3);
       
   328     monitor2->notify();
       
   329 
       
   330     monitor1->wait();
       
   331     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3);
       
   332     monitor2->notify();
       
   333 
       
   334     monitor1->wait();
       
   335     CHECK(timerId==tid1 || timerId==tid2 || timerId==tid3);
       
   336     monitor2->notify();
       
   337 }
       
   338 
       
   339 TEST(TimerServerJavaTime, TSJT01_02_2_timers_2_2_seconds)
       
   340 {
       
   341     int tid1=0, tid2=0;
       
   342     startJavaTimeTimer(tid1, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   343     startJavaTimeTimer(tid2, JavaTime(getCurrentJavaTime()+(2L * 1000L)).getTime());
       
   344 
       
   345     monitor1->wait();
       
   346     CHECK(timerId==tid1 || timerId==tid2);
       
   347     monitor2->notify();
       
   348 
       
   349     monitor1->wait();
       
   350     CHECK(timerId==tid1 || timerId==tid2);
       
   351     monitor2->notify();
       
   352 }
       
   353 
       
   354 TEST(TimerServerJavaTime, TSJT01_01_1_timer_2_seconds)
       
   355 {
       
   356     int tid = 0;
       
   357     JavaTime jt(getCurrentJavaTime() + (2L * 1000L));
       
   358     startJavaTimeTimer(tid, jt.getTime());
       
   359     monitor1->wait();
       
   360 //    CHECK(tid == receivedTimeout);
       
   361     omaEquals(tid, timerId);
       
   362     monitor2->notify();
       
   363 }
       
   364