1 /*----------------------------------------------------------------------------
  2  Name:      xmlBlaster/testsuite/src/c/TestTimeout.c
  3  Project:   xmlBlaster.org
  4  Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
  5  Comment:   Test C client library
  6  Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
  7  Compile:   cd xmlBlaster; build c
  8  Invoke:    'TestTimeout'
  9  See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/c.client.socket.html
 10  -----------------------------------------------------------------------------*/
 11 #include <stdio.h>
 12 #include <stdlib.h>
 13 #include <string.h>
 14 #include <util/helper.h>
 15 #include <util/Timeout.h>
 16 #include <util/Timestampc.h>
 17 #include "test.h"
 18 
 19 static int countTimeouts = 0;
 20 
 21 /**
 22  * Callback function on timeout.
 23  */
 24 static void onTimeout(Timeout *timeout, void *userData, void *userData2) {
 25         const char *data = (char *) userData;
 26         char timeStr[64];
 27         printf("%s Timeout occurred, timer=%s delay=%ld userData=%s\n",
 28                         getCurrentTimeStr(timeStr, 64), timeout->name,
 29                         timeout->timeoutContainer.delay, data);
 30         /*mu_assert("onTimeout()", !strcmp("dummyData", data));*/
 31         countTimeouts++;
 32         if (userData2) userData2 = 0; /* use variable to avoid compiler warning */
 33 }
 34 
 35 static const char * test_timeout() {
 36         const long millisecs = 2000;
 37         countTimeouts = 0;
 38         printf("millisec=%ld\n", millisecs);
 39         {
 40                 Timeout *timeout = createTimeout("TestTimer");
 41                 timeout->setTimeoutListener(timeout, onTimeout, millisecs, (void*)"dummyData", 0);
 42                 sleepMillis(5000);
 43                 mu_assert("test_timeout()", 2==countTimeouts);
 44                 freeTimeout(timeout);
 45                 printf("SUCCESS test_timeout\n");
 46         }
 47         return 0;
 48 }
 49 
 50 static const char * test_timeoutNoCb() {
 51         const long millisecs = 2000;
 52         countTimeouts = 0;
 53         printf("millisec=%ld\n", millisecs);
 54         {
 55                 Timeout *timeout = createTimeout("TestTimerNoCb");
 56                 timeout->setTimeoutListener(timeout, 0, millisecs, (void*)"dummyData", 0);
 57                 sleepMillis(3000);
 58                 mu_assert("test_timeout()", 0==countTimeouts);
 59                 freeTimeout(timeout);
 60                 printf("SUCCESS test_timeoutNoCb\n");
 61         }
 62         return 0;
 63 }
 64 
 65 static const char * test_timeoutErr() {
 66         const long millisecs = 2000;
 67         countTimeouts = 0;
 68         printf("millisec=%ld\n", millisecs);
 69         {
 70                 Timeout *timeout = createTimeout("TestTimerErr");
 71                 timeout->setTimeoutListener(timeout, 0, 200, 0, 0);
 72                 sleepMillis(1000);
 73                 timeout->setTimeoutListener(timeout, onTimeout, 200, 0, 0);
 74                 sleepMillis(1000);
 75                 timeout->setTimeoutListener(timeout, onTimeout, millisecs, (void*)"dummyData", 0);
 76                 countTimeouts = 0;
 77                 sleepMillis(5000);
 78                 mu_assert("test_timeout()", 2==countTimeouts);
 79                 freeTimeout(timeout);
 80                 printf("SUCCESS test_timeoutErr\n");
 81         }
 82         return 0;
 83 }
 84 
 85 static const char * test_timeoutReset() {
 86         const long millisecs = 2000;
 87         countTimeouts = 0;
 88         printf("selfThreadId=%lud millisec=%ld\n", get_pthread_id(pthread_self()), millisecs);
 89         {
 90                 Timeout *timeout = createTimeout("TestTimerReset");
 91                 timeout->setTimeoutListener(timeout, onTimeout, millisecs, (void*)"dummyData", 0);
 92                 sleepMillis(1000);
 93                 timeout->setTimeoutListener(timeout, onTimeout, 0, (void*)"dummyData", 0);
 94                 sleepMillis(1500);
 95                 mu_assert("test_timeoutReset()", 0==countTimeouts);
 96                 freeTimeout(timeout);
 97                 printf("SUCCESS test_timeoutReset\n");
 98         }
 99         return 0;
100 }
101 
102 
103 /**
104  * Callback function on timeout.
105  */
106 static void onTimeoutResetCall(Timeout *timeout, void *userData, void *userData2) {
107         char timeStr[64];
108         countTimeouts++;
109         printf("%s Timeout occurred, timer=%s delay=%ld count=%d\n",
110                         getCurrentTimeStr(timeStr, 64), timeout->name,
111                         timeout->timeoutContainer.delay, countTimeouts);
112         /* Reset timer */
113         if (countTimeouts == 1)
114                 timeout->setTimeoutListener(timeout, onTimeout, 0, (void*)"onTimeoutResetCall", 0);
115         if (userData || userData2) userData = userData2 = 0; /* use variable to avoid compiler warning */
116 }
117 static const char * test_timeoutResetFromCallback() {
118         const long millisecs = 1000;
119         int i;
120         printf("selfThreadId=%lud millisec=%ld\n", get_pthread_id(pthread_self()), millisecs);
121         for (i=0; i<1; i++) {
122                 Timeout *timeout = createTimeout("TestTimerResetFromCallback");
123                 countTimeouts = 0;
124                 timeout->setTimeoutListener(timeout, onTimeoutResetCall, millisecs, (void*)"test_timeoutResetFromCallback", 0);
125                 /* resets timer */
126                 sleepMillis(2500);
127                 mu_assert("test_timeoutReset()", 1==countTimeouts);
128                 timeout->setTimeoutListener(timeout, onTimeoutResetCall, millisecs, (void*)"test_timeoutResetFromCallback", 0);
129                 /* keeps timer */
130                 sleepMillis(2500);
131                 mu_assert("test_timeoutReset()", 3==countTimeouts);
132                 freeTimeout(timeout);
133                 printf("SUCCESS test_timeoutResetFromCallback\n");
134         }
135         return 0;
136 }
137 
138 
139 /**
140  * Callback function on timeout.
141  */
142 static void onTimeoutTwice(Timeout *timeout, void *userData, void *userData2) {
143         int *data = (int *) userData;
144         char timeStr[64];
145         *data += 1;
146         printf("%s Timeout occurred, timer=%s delay=%ld count=%d\n",
147                         getCurrentTimeStr(timeStr, 64), timeout->name,
148                         timeout->timeoutContainer.delay, *data);
149         if (userData2) userData2 = 0; /* use variable to avoid compiler warning */
150 }
151 
152 static const char * test_timeoutTwice() {
153         const long millisecs = 2000;
154         Timeout *timeout1 = 0;
155         int count1 = 0;
156         Timeout *timeout2 = 0;
157         int count2 = 0;
158         countTimeouts = 0;
159 
160         timeout1 = createTimeout("TestTimer1");
161         timeout1->setTimeoutListener(timeout1, onTimeoutTwice, millisecs, &count1, 0);
162 
163         timeout2 = createTimeout("TestTimer2");
164         timeout2->setTimeoutListener(timeout2, onTimeoutTwice, millisecs, &count2, 0);
165 
166         sleepMillis(5000);
167 
168         mu_assert("test_timeoutTwice()", 2==count1);
169         mu_assert("test_timeoutTwice()", 2==count2);
170 
171         freeTimeout(timeout1);
172         freeTimeout(timeout2);
173         printf("SUCCESS test_timeoutTwice\n");
174         return 0;
175 }
176 
177 static const char *all_tests() {
178         mu_run_test(test_timeoutResetFromCallback);
179         mu_run_test(test_timeoutReset);
180         mu_run_test(test_timeoutErr);
181         mu_run_test(test_timeoutNoCb);
182         mu_run_test(test_timeout);
183         mu_run_test(test_timeoutTwice);
184         return 0;
185 }
186 
187 /**
188  * gcc -g -Wall -pedantic -lpthread -I../../../src/c -o TestTimeout TestTimeout.c ../../../src/c/util/Timeout.c ../../../src/c/util/helper.c
189  * exit(0) if OK
190  */
191 int main(int argc_, char **argv_) {
192         const char *result;
193         result = all_tests();
194         if (result != 0) {
195                 printf("[TEST FAIL] %s\n", result);
196         } else {
197                 printf("ALL %d TESTS PASSED\n", tests_run);
198         }
199         printf("Tests run: %d\n", tests_run);
200         return result != 0;
201 }


syntax highlighted by Code2HTML, v. 0.9.1