1 /*----------------------------------------------------------------------------
  2 Name:      xmlBlaster/testsuite/src/c/TestQueue.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 -DXMLBLASTER_PERSISTENT_QUEUE_SQLITE3=1 c-test
  8 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.c.queue.html
  9 -----------------------------------------------------------------------------*/
 10 #include <stdio.h>
 11 #include <stdlib.h>
 12 #include <string.h>
 13 #include <util/queue/QueueInterface.h>
 14 #include "test.h"
 15 
 16 #if defined(XMLBLASTER_PERSISTENT_QUEUE) || defined(XMLBLASTER_PERSISTENT_QUEUE_SQLITE3)
 17 
 18 static int argc = 0;
 19 static char** argv = 0;
 20 #define  ERRORSTR_LEN EXCEPTIONSTRUCT_ERRORCODE_LEN + EXCEPTIONSTRUCT_MESSAGE_LEN + 64
 21 static char errorString[ERRORSTR_LEN];
 22 static char int64Str_[32];
 23 static char * const int64Str = int64Str_;   /* to make the pointer address const */
 24 static char int64StrX_[32];
 25 static char * const int64StrX = int64StrX_;   /* a second one */
 26 /* Try switch on/off logging with using function pointer 'xmlBlasterDefaultLogging' or '0' */
 27 static XmlBlasterLogging loggingFp = xmlBlasterDefaultLogging;
 28 
 29 #define mu_assert_checkException(message, exception) \
 30       do {\
 31          if (*exception.errorCode != 0) {\
 32             sprintf(MU_ASSERT_TEXT, "[TEST FAIL]%s:%d %s: Caught exception: '%s'", __FILE__, __LINE__, message, getExceptionStr(errorString, ERRORSTR_LEN, &exception));\
 33             return MU_ASSERT_TEXT;\
 34          }\
 35       } while (0)
 36 
 37 #define mu_assert_checkWantException(message, exception) \
 38       do {\
 39          if (*exception.errorCode == 0) {\
 40             sprintf(MU_ASSERT_TEXT, "[TEST FAIL]%s:%d %s: Missing exception", __FILE__, __LINE__, message);\
 41             return MU_ASSERT_TEXT;\
 42          }\
 43          else {\
 44             char out[1024];\
 45             printf("OK, expected exception: %s\n", getExceptionStr(out, 1024, &exception));\
 46          }\
 47       } while (0)
 48 
 49 static bool destroy(const char *dbName);
 50 
 51 /**
 52  * Kill complete DB on HD
 53  */
 54 static bool destroy(const char *dbName)
 55 {
 56    bool stateOk;
 57    I_Queue *queueP;
 58    ExceptionStruct exception;
 59    QueueProperties queueProperties;
 60    memset(&queueProperties, 0, sizeof(QueueProperties));
 61    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
 62    strncpy0(queueProperties.queueName, "b", QUEUE_ID_MAX);
 63    strncpy0(queueProperties.tablePrefix, "c", QUEUE_PREFIX_MAX);
 64    queueProperties.maxNumOfEntries = 10;
 65    queueProperties.maxNumOfBytes = 10;
 66 
 67    queueP = createQueue(&queueProperties, &exception);
 68    
 69    stateOk = queueP->destroy(&queueP, &exception);
 70 
 71    if (!stateOk) {
 72       printf("Ignoring problem during destroy: %s", getExceptionStr(errorString, ERRORSTR_LEN, &exception));
 73    }
 74    return stateOk;
 75 }
 76 
 77 /**
 78  * Test illegal function call parameters. 
 79  */
 80 static const char * test_illegal()
 81 {
 82    ExceptionStruct exception;
 83    QueueEntry queueEntry;
 84    QueueEntryArr *entries = 0;
 85    QueueProperties queueProperties;
 86    I_Queue *queueP = 0;
 87    const char *dbName = "xmlBlasterClient-C-Test.db";
 88    int32_t numRemoved;
 89 
 90    printf("\n--------test_illegal---------------------\n");
 91    destroy(dbName); /* Delete old db file */
 92 
 93    memset(&queueProperties, 0, sizeof(QueueProperties));
 94 
 95    queueP = createQueue(0, 0);
 96    mu_assert("create() Wrong properties", queueP == 0);
 97 
 98    queueP = createQueue(&queueProperties, &exception);
 99    mu_assert("create()", queueP == 0);
100    mu_assert_checkWantException("create()", exception);
101 
102    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
103    strncpy0(queueProperties.queueName, "connection_clientJoe", QUEUE_ID_MAX);
104    strncpy0(queueProperties.tablePrefix, "XB_", QUEUE_PREFIX_MAX);
105    queueProperties.maxNumOfEntries = 0;
106    queueProperties.maxNumOfBytes = 0;
107    queueP = createQueue(&queueProperties, &exception);
108    mu_assert("create()", queueP == 0);
109    mu_assert_checkWantException("create()", exception);
110 
111    queueProperties.maxNumOfEntries = 10;
112    queueProperties.maxNumOfBytes = 100;
113    queueP = createQueue(&queueProperties, &exception);
114    mu_assert("create()", queueP != 0);
115    mu_assert_checkException("create()", exception);
116 
117    queueP->put(0, 0, 0);
118    queueP->put(0, 0, &exception);
119    mu_assert_checkWantException("put()", exception);
120 
121    queueP->put(queueP, 0, &exception);
122    mu_assert_checkWantException("put()", exception);
123 
124    memset(&queueEntry, 0, sizeof(QueueEntry));
125    queueP->put(queueP, &queueEntry, &exception);
126    mu_assert_checkWantException("put()", exception);
127 
128    entries = queueP->peekWithSamePriority(0, 0, 0, 0);
129    entries = queueP->peekWithSamePriority(0, 0, 0, &exception);
130    mu_assert_checkWantException("peekWithSamePriority()", exception);
131 
132    entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
133    mu_assert_checkException("peekWithSamePriority()", exception);
134    mu_assert("peekWithSamePriority() entries", entries != 0);
135    mu_assertEqualsInt("peekWithSamePriority() entries", 0, entries->len);
136    mu_assert("peekWithSamePriority() entries", entries->queueEntryArr == 0);
137    freeQueueEntryArr(entries);
138    entries = 0;
139 
140    numRemoved = queueP->randomRemove(0, 0, 0);
141    numRemoved = queueP->randomRemove(0, 0, &exception);
142    mu_assert_checkWantException("randomRemove()", exception);
143    numRemoved = queueP->randomRemove(queueP, 0, &exception);
144    mu_assert_checkException("randomRemove()", exception);
145 
146    numRemoved = queueP->randomRemove(queueP, entries, &exception);
147    mu_assert_checkException("randomRemove()", exception);
148    mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
149 
150    entries = (QueueEntryArr *)calloc(1, sizeof(QueueEntryArr));;
151    numRemoved = queueP->randomRemove(queueP, entries, &exception);
152    mu_assert_checkException("randomRemove()", exception);
153    mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
154    freeQueueEntryArr(entries);
155    entries = 0;
156 
157    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
158    queueP->clear(0, 0);
159    queueP->clear(0, &exception);
160    mu_assert_checkWantException("clear()", exception);
161    queueP->clear(queueP, &exception);
162    mu_assert_checkException("clear()", exception);
163 
164    queueP->empty(0);
165 
166    mu_assertEqualsInt("numOfEntries", 0, queueP->getNumOfEntries(queueP));
167    mu_assertEqualsInt("numOfBytes", 0, (int)queueP->getNumOfBytes(queueP));
168 
169    mu_assertEqualsInt("numOfEntries", -1, queueP->getNumOfEntries(0));
170    mu_assertEqualsInt("numOfBytes", -1, (int)queueP->getNumOfBytes(0));
171 
172    queueP->shutdown(&queueP, &exception);
173    printf("Testing test_illegal DONE\n");
174    printf("-----------------------------------------\n");
175    return 0;
176 }
177 
178 /**
179  * Test overflow of maxNumOfBytes and maxNumOfEntries. 
180  */
181 static const char * test_overflow()
182 {
183    ExceptionStruct exception;
184    QueueProperties queueProperties;
185    I_Queue *queueP = 0;
186    const char *dbName = "xmlBlasterClient-C-Test.db";
187 
188    const int64_t idArr[] =   { 1081492136826000000ll, 1081492136856000000ll, 1081492136876000000ll, 1081492136911000000ll, 1081492136922000000ll };
189    const int16_t prioArr[] = { 5                    , 1                    , 9                    , 9                    , 5 };
190    const char *data[] =      { ""                   , ""                   , ""                   , ""                   , ""};
191    const size_t numPut = sizeof(idArr)/sizeof(int64_t);
192 
193    printf("\n---------test_overflow-------------------\n");
194    destroy(dbName); /* Delete old db file */
195 
196    memset(&queueProperties, 0, sizeof(QueueProperties));
197    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
198    strncpy0(queueProperties.queueName, "connection_clientJoe", QUEUE_ID_MAX);
199    strncpy0(queueProperties.tablePrefix, "XB_", QUEUE_PREFIX_MAX);
200    queueProperties.maxNumOfEntries = 4L;
201    queueProperties.maxNumOfBytes = 25LL;
202    queueProperties.logFp = loggingFp;
203    queueProperties.logLevel = XMLBLASTER_LOG_TRACE;
204    queueProperties.userObject = 0;
205 
206    queueP = createQueue(&queueProperties, &exception);
207    mu_assertEqualsLong("create() maxNumOfEntries", 4L, (long)queueP->getMaxNumOfEntries(queueP));
208    mu_assertEqualsString("create() maxNumOfBytes", int64ToStr(int64Str, 25LL), int64ToStr(int64StrX, queueP->getMaxNumOfBytes(queueP)));
209    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
210 
211    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
212 
213    {  /* Test entry overflow */
214       size_t i;
215       size_t lenPut = 0;
216       for (i=0; i<numPut; i++) {
217          QueueEntry queueEntry;
218          memset(&queueEntry, 0, sizeof(QueueEntry));
219          queueEntry.priority = prioArr[i];
220          queueEntry.isPersistent = true;
221          queueEntry.uniqueId = idArr[i];
222          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
223          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
224          queueEntry.embeddedBlob.data = (char *)data[i];
225          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
226 
227          queueP->put(queueP, &queueEntry, &exception);
228          if (i < 4) {
229             mu_assert_checkException("put()", exception);
230             lenPut += strlen(queueEntry.embeddedBlob.data);
231          }
232          else {
233             mu_assert_checkWantException("put() numOfEntries overflow", exception);
234          }
235 
236       }
237       mu_assertEqualsInt("put() numOfEntries", 4, queueP->getNumOfEntries(queueP));
238       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
239       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
240 
241    }
242    queueP->clear(queueP, &exception);
243 
244    {  /* Test byte overflow */
245       size_t i;
246       size_t lenPut = 0;
247       for (i=0; i<numPut; i++) {
248          QueueEntry queueEntry;
249          memset(&queueEntry, 0, sizeof(QueueEntry));
250          queueEntry.priority = prioArr[i];
251          queueEntry.isPersistent = true;
252          queueEntry.uniqueId = idArr[i];
253          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
254          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
255          queueEntry.embeddedBlob.data = (char *)"0123456789";
256          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
257 
258          queueP->put(queueP, &queueEntry, &exception);
259          if (i < 3) {
260             mu_assert_checkException("put()", exception);
261             lenPut += strlen(queueEntry.embeddedBlob.data);
262          }
263          else {
264             mu_assert_checkWantException("put() numOfBytes overflow", exception);
265          }
266 
267       }
268       mu_assertEqualsInt("put() numOfEntries", 3, queueP->getNumOfEntries(queueP));
269       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
270       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
271 
272    }
273    queueP->clear(queueP, &exception);
274 
275    {  /* Test byte overflow with preset entry size */
276       size_t i;
277       size_t lenPut = 0;
278       for (i=0; i<numPut; i++) {
279          QueueEntry queueEntry;
280          memset(&queueEntry, 0, sizeof(QueueEntry));
281          queueEntry.priority = prioArr[i];
282          queueEntry.isPersistent = true;
283          queueEntry.uniqueId = idArr[i];
284          queueEntry.sizeInBytes = 20;
285          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
286          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
287          queueEntry.embeddedBlob.data = (char *)"0123456789";
288          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
289 
290          queueP->put(queueP, &queueEntry, &exception);
291          if (i < 2) { /* (index 0 and 1): 2 entries * 20 bytes = 40 bytes */
292             mu_assert_checkException("put()", exception);
293             lenPut += (int)queueEntry.sizeInBytes;
294          }
295          else {
296             mu_assert_checkWantException("put() numOfBytes overflow", exception);
297          }
298 
299       }
300       mu_assertEqualsInt("put() numOfEntries", 2, queueP->getNumOfEntries(queueP));
301       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
302       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
303 
304    }
305    queueP->clear(queueP, &exception);
306 
307    queueP->shutdown(&queueP, &exception);
308    return 0;
309 }
310 
311 /**
312  * Test invocation of all queue methods. 
313  */
314 static const char * test_queue()
315 {
316    ExceptionStruct exception;
317    QueueEntryArr *entries = 0;
318    QueueProperties queueProperties;
319    I_Queue *queueP = 0;
320    const char *dbName = "xmlBlasterClient-C-Test.db";
321    const char *dummy = "bla";
322 
323    const int64_t idArr[] =   { 1081492136826000000ll, 1081492136856000000ll, 1081492136876000000ll, 1081492136911000000ll, 1081492136922000000ll };
324    const int16_t prioArr[] = { 5                    , 1                    , 9                    , 9                    , 5 };
325    const char *data[] =      { "1. Hello"           , "2. World"           , "3. High Prio 1"     , "4. High Prio 2"     , "5. done"};
326    const size_t numPut = sizeof(idArr)/sizeof(int64_t);
327    size_t lenPut = 0;
328 
329    printf("\n---------test_queue----------------------\n");
330    destroy(dbName); /* Delete old db file */
331 
332    memset(&queueProperties, 0, sizeof(QueueProperties));
333    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
334    strncpy0(queueProperties.queueName, "connection_clientJoe", QUEUE_ID_MAX);
335    strncpy0(queueProperties.tablePrefix, "XB_", QUEUE_PREFIX_MAX);
336    queueProperties.maxNumOfEntries = 10000000L;
337    queueProperties.maxNumOfBytes = 1000000000LL;
338    queueProperties.logFp = loggingFp;
339    queueProperties.logLevel = XMLBLASTER_LOG_TRACE;
340    queueProperties.userObject = (void *)dummy;
341 
342    queueP = createQueue(&queueProperties, &exception);
343    mu_assert("create()", queueP != 0);
344    mu_assert("create() QueueProperty", queueP->getProperties(queueP) != 0);
345    mu_assert("create() userObject", queueP->userObject == dummy);
346    mu_assertEqualsBool("create() isInitialized", true, queueP->isInitialized);
347    mu_assertEqualsString("create() dbName", queueProperties.dbName, queueP->getProperties(queueP)->dbName);
348    mu_assertEqualsString("create() tablePrefix", queueProperties.tablePrefix, queueP->getProperties(queueP)->tablePrefix);
349    mu_assertEqualsString("create() queueName", queueProperties.queueName, queueP->getProperties(queueP)->queueName);
350    mu_assertEqualsLong("create() maxNumOfEntries", 10000000L, (long)queueP->getMaxNumOfEntries(queueP));
351    mu_assertEqualsString("create() maxNumOfBytes", int64ToStr(int64Str, 1000000000LL), int64ToStr(int64StrX, queueP->getMaxNumOfBytes(queueP)));
352    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
353 
354    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
355 
356    {
357       size_t i;
358       for (i=0; i<numPut; i++) {
359          QueueEntry queueEntry;
360          memset(&queueEntry, 0, sizeof(QueueEntry));
361          queueEntry.priority = prioArr[i];
362          queueEntry.isPersistent = true;
363          queueEntry.uniqueId = idArr[i];
364          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
365          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
366          queueEntry.embeddedBlob.data = (char *)data[i];
367          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
368          lenPut += strlen(queueEntry.embeddedBlob.data);
369 
370          queueP->put(queueP, &queueEntry, &exception);
371          mu_assert_checkException("put()", exception);
372       }
373       mu_assertEqualsInt("put() numOfEntries", (int)numPut, queueP->getNumOfEntries(queueP));
374       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
375       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
376 
377       printf("-----------------------------------------\n");
378       printf("Testing shutdown and restart ...\n");
379       queueP->shutdown(&queueP, &exception);
380       mu_assert("shutdown()", queueP == 0);
381 
382       queueP = createQueue(&queueProperties, &exception);
383       mu_assertEqualsInt("put() numOfEntries", (int)numPut, queueP->getNumOfEntries(queueP));
384       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
385       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
386       printf("-----------------------------------------\n");
387    }
388 
389    {
390       int j;
391       printf("-----------------------------------------\n");
392       printf("Testing peekWithSamePriority 9 ...\n");
393       for (j=0; j<10; j++) {
394          entries = queueP->peekWithSamePriority(queueP, 0, 0, &exception);
395          mu_assert_checkException("peekWithSamePriority()", exception);
396          mu_assert(" peekWithSamePriority()", entries != 0);
397          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 0, entries->len);
398          freeQueueEntryArr(entries);
399 
400          entries = queueP->peekWithSamePriority(queueP, 1, -1, &exception);
401          mu_assert_checkException("peekWithSamePriority()", exception);
402          mu_assert(" peekWithSamePriority()", entries != 0);
403          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
404          freeQueueEntryArr(entries);
405 
406          entries = queueP->peekWithSamePriority(queueP, -1, 3, &exception);
407          mu_assert_checkException("peekWithSamePriority()", exception);
408          mu_assert(" peekWithSamePriority()", entries != 0);
409          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
410          freeQueueEntryArr(entries);
411 
412          mu_assertEqualsInt("put() numOfEntries", (int)numPut, queueP->getNumOfEntries(queueP));
413          mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
414          mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
415       }
416       {
417          size_t i;
418          entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
419          mu_assert_checkException("peekWithSamePriority()", exception);
420          mu_assert(" peekWithSamePriority()", entries != 0);
421          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 2, entries->len);
422          printf("testRun after peekWithSamePriority() dump %lu entries:\n", (unsigned long)entries->len);
423          for (i=0; i<entries->len; i++) {
424             char *tmp;
425             size_t expectedIndex = i+2;
426             QueueEntry *queueEntry = &entries->queueEntryArr[i];
427             char *dump = queueEntryToXml(queueEntry, 200);
428             printf("%s\n", dump);
429             freeEntryDump(dump);
430                            mu_assert("uniqueId fail", queueEntry->uniqueId == idArr[expectedIndex]);
431             mu_assertEqualsString("uniqueId", int64ToStr(int64Str, idArr[expectedIndex]), int64ToStr(int64StrX, queueEntry->uniqueId));
432             mu_assertEqualsInt("priority", 9, queueEntry->priority);
433             mu_assertEqualsBool("persistent", true, queueEntry->isPersistent);
434             mu_assertEqualsInt("bloblen", strlen(data[expectedIndex]), queueEntry->embeddedBlob.dataLen);
435             tmp = strFromBlobAlloc(queueEntry->embeddedBlob.data, queueEntry->embeddedBlob.dataLen);
436             mu_assertEqualsString("blob", data[expectedIndex], tmp);
437             freeEntryDump(tmp);
438          }
439          freeQueueEntryArr(entries);
440       }
441       printf("-----------------------------------------\n");
442    }
443 
444    {
445       int32_t numRemoved;
446       printf("-----------------------------------------\n");
447       printf("Testing randomRemove prio=9 ...\n");
448       printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
449       entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
450 
451       numRemoved = queueP->randomRemove(queueP, entries, &exception);
452       mu_assert_checkException("randomRemove()", exception);
453       mu_assertEqualsInt("numRemoved", 2, (int)numRemoved);
454 
455       numRemoved = queueP->randomRemove(queueP, entries, &exception);
456       mu_assert_checkException("randomRemove()", exception);
457       mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
458 
459       freeQueueEntryArr(entries);
460 
461       mu_assertEqualsInt("put() numOfEntries", 3, queueP->getNumOfEntries(queueP));
462       lenPut = strlen(data[0]) + strlen(data[1]) + strlen(data[4]);
463       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
464       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
465       printf("-----------------------------------------\n");
466    }
467 
468 
469    {
470       int j;
471       printf("-----------------------------------------\n");
472       printf("Testing peekWithSamePriority 5 ...\n");
473       for (j=0; j<10; j++) {
474          entries = queueP->peekWithSamePriority(queueP, 0, 0, &exception);
475          mu_assert_checkException("peekWithSamePriority()", exception);
476          mu_assert(" peekWithSamePriority()", entries != 0);
477          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 0, entries->len);
478          freeQueueEntryArr(entries);
479 
480          entries = queueP->peekWithSamePriority(queueP, 1, -1, &exception);
481          mu_assert_checkException("peekWithSamePriority()", exception);
482          mu_assert(" peekWithSamePriority()", entries != 0);
483          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
484          freeQueueEntryArr(entries);
485 
486          entries = queueP->peekWithSamePriority(queueP, -1, 3, &exception);
487          mu_assert_checkException("peekWithSamePriority()", exception);
488          mu_assert(" peekWithSamePriority()", entries != 0);
489          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
490          freeQueueEntryArr(entries);
491       }
492       {
493          size_t i;
494          entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
495          mu_assert_checkException("peekWithSamePriority()", exception);
496          mu_assert(" peekWithSamePriority()", entries != 0);
497          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 2, entries->len);
498          printf("testRun after peekWithSamePriority() dump %lu entries:\n", (unsigned long)entries->len);
499          for (i=0; i<entries->len; i++) {
500             char *tmp;
501             int expectedIndex = (i==0) ? 0 : 4;
502             QueueEntry *queueEntry = &entries->queueEntryArr[i];
503             char *dump = queueEntryToXml(queueEntry, 200);
504             printf("%s\n", dump);
505             freeEntryDump(dump);
506             mu_assertEqualsString("uniqueId", int64ToStr(int64Str, idArr[expectedIndex]), int64ToStr(int64StrX, queueEntry->uniqueId));
507             mu_assertEqualsInt("priority", 5, queueEntry->priority);
508             mu_assertEqualsBool("persistent", true, queueEntry->isPersistent);
509             mu_assertEqualsInt("bloblen", strlen(data[expectedIndex]), queueEntry->embeddedBlob.dataLen);
510             tmp = strFromBlobAlloc(queueEntry->embeddedBlob.data, queueEntry->embeddedBlob.dataLen);
511             mu_assertEqualsString("blob", data[expectedIndex], tmp);
512             freeEntryDump(tmp);
513          }
514          freeQueueEntryArr(entries);
515       }
516 
517       mu_assertEqualsInt("put() numOfEntries", 3, queueP->getNumOfEntries(queueP));
518       lenPut = strlen(data[0]) + strlen(data[1]) + strlen(data[4]);
519       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
520       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
521       printf("-----------------------------------------\n");
522    }
523 
524    {
525       int32_t numRemoved;
526       printf("-----------------------------------------\n");
527       printf("Testing randomRemove prio=5 ...\n");
528       printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
529       entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
530 
531       numRemoved = queueP->randomRemove(queueP, entries, &exception);
532       mu_assert_checkException("randomRemove()", exception);
533       mu_assertEqualsInt("numRemoved", 2, (int)numRemoved);
534 
535       numRemoved = queueP->randomRemove(queueP, entries, &exception);
536       mu_assert_checkException("randomRemove()", exception);
537       mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
538 
539       freeQueueEntryArr(entries);
540 
541       mu_assertEqualsInt("put() numOfEntries", 1, queueP->getNumOfEntries(queueP));
542       lenPut = strlen(data[1]);
543       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
544       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
545       printf("-----------------------------------------\n");
546    }
547 
548    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
549 
550    {
551       int j;
552       printf("-----------------------------------------\n");
553       printf("Testing peekWithSamePriority 1 ...\n");
554       for (j=0; j<10; j++) {
555          entries = queueP->peekWithSamePriority(queueP, 0, 0, &exception);
556          mu_assert_checkException("peekWithSamePriority()", exception);
557          mu_assert(" peekWithSamePriority()", entries != 0);
558          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 0, entries->len);
559          freeQueueEntryArr(entries);
560 
561          entries = queueP->peekWithSamePriority(queueP, 1, -1, &exception);
562          mu_assert_checkException("peekWithSamePriority()", exception);
563          mu_assert(" peekWithSamePriority()", entries != 0);
564          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
565          freeQueueEntryArr(entries);
566 
567          entries = queueP->peekWithSamePriority(queueP, -1, 3, &exception);
568          mu_assert_checkException("peekWithSamePriority()", exception);
569          mu_assert(" peekWithSamePriority()", entries != 0);
570          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
571          freeQueueEntryArr(entries);
572       }
573       {
574          size_t i;
575          entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
576          mu_assert_checkException("peekWithSamePriority()", exception);
577          mu_assert(" peekWithSamePriority()", entries != 0);
578          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
579          printf("testRun after peekWithSamePriority() dump %lu entries:\n", (unsigned long)entries->len);
580          for (i=0; i<entries->len; i++) {
581             char *tmp;
582             int expectedIndex = (i==0) ? 1 : 0;
583             QueueEntry *queueEntry = &entries->queueEntryArr[i];
584             char *dump = queueEntryToXml(queueEntry, 200);
585             printf("%s\n", dump);
586             freeEntryDump(dump);
587             mu_assertEqualsString("uniqueId", int64ToStr(int64Str, idArr[expectedIndex]), int64ToStr(int64StrX, queueEntry->uniqueId));
588             mu_assertEqualsInt("priority", 1, queueEntry->priority);
589             mu_assertEqualsBool("persistent", true, queueEntry->isPersistent);
590             mu_assertEqualsInt("bloblen", strlen(data[expectedIndex]), queueEntry->embeddedBlob.dataLen);
591             tmp = strFromBlobAlloc(queueEntry->embeddedBlob.data, queueEntry->embeddedBlob.dataLen);
592             mu_assertEqualsString("blob", data[expectedIndex], tmp);
593             freeEntryDump(tmp);
594          }
595          freeQueueEntryArr(entries);
596       }
597 
598       mu_assertEqualsInt("put() numOfEntries", 1, queueP->getNumOfEntries(queueP));
599       lenPut = strlen(data[1]);
600       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
601       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
602       printf("-----------------------------------------\n");
603    }
604    
605    queueP->clear(queueP, &exception);
606    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
607    mu_assertEqualsInt("put() numOfEntries", 0, queueP->getNumOfEntries(queueP));
608    mu_assertEqualsInt("put() numOfBytes", 0, (int)queueP->getNumOfBytes(queueP));
609    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
610 
611    queueP->shutdown(&queueP, &exception);
612 
613    destroy(dbName); /* Delete the db file */
614 
615    return 0;
616 }
617 
618 
619 static const char *all_tests()
620 {
621    mu_run_test(test_illegal);
622    mu_run_test(test_queue);
623    mu_run_test(test_overflow);
624    return 0;
625 }
626 
627 int main(int argc_, char **argv_)
628 {
629    const char *result;
630    argc = argc_;
631    argv = argv_;
632    if (argc || argv) {} /* to avoid compiler warning */
633 
634    result = all_tests();
635 
636    if (result != 0) {
637       printf("%s\n", result);
638    }
639    else {
640       printf("ALL TESTS PASSED\n");
641    }
642    printf("Tests run: %d\n", tests_run);
643 
644    return result != 0;
645 }
646 
647 
648 #else /* XMLBLASTER_PERSISTENT_QUEUE_SQLITE3 */
649 
650 int main(int argc_, char **argv_)
651 {
652    printf("C-client is compiled without -XMLBLASTER_PERSISTENT_QUEUE or -XMLBLASTER_PERSISTENT_QUEUE_SQLITE3=1, no persistent queue tested\n");
653    if (*MU_ASSERT_TEXT || tests_run) {} /* To suppress compiler warning */
654    return 0;
655 }
656 
657 #endif /* XMLBLASTER_PERSISTENT_QUEUE XMLBLASTER_PERSISTENT_QUEUE_SQLITE3 */


syntax highlighted by Code2HTML, v. 0.9.1