testsuite/src/c/TestQueue.c

Go to the documentation of this file.
00001 /*----------------------------------------------------------------------------
00002 Name:      xmlBlaster/testsuite/src/c/TestQueue.c
00003 Project:   xmlBlaster.org
00004 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
00005 Comment:   Test C client library
00006 Author:    "Marcel Ruff" <xmlBlaster@marcelruff.info>
00007 Compile:   cd xmlBlaster; build -DXMLBLASTER_PERSISTENT_QUEUE=1 c-test
00008 See:       http://www.xmlblaster.org/xmlBlaster/doc/requirements/client.c.queue.html
00009 -----------------------------------------------------------------------------*/
00010 #include <stdio.h>
00011 #include <stdlib.h>
00012 #include <string.h>
00013 #include <util/queue/QueueInterface.h>
00014 #include "test.h"
00015 
00016 #ifdef XMLBLASTER_PERSISTENT_QUEUE
00017 
00018 static int argc = 0;
00019 static char** argv = 0;
00020 #define  ERRORSTR_LEN EXCEPTIONSTRUCT_ERRORCODE_LEN + EXCEPTIONSTRUCT_MESSAGE_LEN + 64
00021 static char errorString[ERRORSTR_LEN];
00022 static char int64Str_[32];
00023 static char * const int64Str = int64Str_;   /* to make the pointer address const */
00024 static char int64StrX_[32];
00025 static char * const int64StrX = int64StrX_;   /* a second one */
00026 /* Try switch on/off logging with using function pointer 'xmlBlasterDefaultLogging' or '0' */
00027 static XmlBlasterLogging loggingFp = xmlBlasterDefaultLogging;
00028 
00029 #define mu_assert_checkException(message, exception) \
00030       do {\
00031          if (*exception.errorCode != 0) {\
00032             sprintf(MU_ASSERT_TEXT, "[TEST FAIL]%s:%d %s: Caught exception: '%s'", __FILE__, __LINE__, message, getExceptionStr(errorString, ERRORSTR_LEN, &exception));\
00033             return MU_ASSERT_TEXT;\
00034          }\
00035       } while (0)
00036 
00037 #define mu_assert_checkWantException(message, exception) \
00038       do {\
00039          if (*exception.errorCode == 0) {\
00040             sprintf(MU_ASSERT_TEXT, "[TEST FAIL]%s:%d %s: Missing exception", __FILE__, __LINE__, message);\
00041             return MU_ASSERT_TEXT;\
00042          }\
00043          else {\
00044             char out[1024];\
00045             printf("OK, expected exception: %s\n", getExceptionStr(out, 1024, &exception));\
00046          }\
00047       } while (0)
00048 
00049 static bool destroy(const char *dbName);
00050 
00054 static bool destroy(const char *dbName)
00055 {
00056    bool stateOk;
00057    I_Queue *queueP;
00058    ExceptionStruct exception;
00059    QueueProperties queueProperties;
00060    memset(&queueProperties, 0, sizeof(QueueProperties));
00061    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
00062    strncpy0(queueProperties.queueName, "b", QUEUE_ID_MAX);
00063    strncpy0(queueProperties.tablePrefix, "c", QUEUE_PREFIX_MAX);
00064    queueProperties.maxNumOfEntries = 10;
00065    queueProperties.maxNumOfBytes = 10;
00066 
00067    queueP = createQueue(&queueProperties, &exception);
00068    
00069    stateOk = queueP->destroy(&queueP, &exception);
00070 
00071    if (!stateOk) {
00072       printf("Ignoring problem during destroy: %s", getExceptionStr(errorString, ERRORSTR_LEN, &exception));
00073    }
00074    return stateOk;
00075 }
00076 
00080 static const char * test_illegal()
00081 {
00082    ExceptionStruct exception;
00083    QueueEntry queueEntry;
00084    QueueEntryArr *entries = 0;
00085    QueueProperties queueProperties;
00086    I_Queue *queueP = 0;
00087    const char *dbName = "xmlBlasterClient-C-Test.db";
00088    int32_t numRemoved;
00089 
00090    printf("\n--------test_illegal---------------------\n");
00091    destroy(dbName); /* Delete old db file */
00092 
00093    memset(&queueProperties, 0, sizeof(QueueProperties));
00094 
00095    queueP = createQueue(0, 0);
00096    mu_assert("create() Wrong properties", queueP == 0);
00097 
00098    queueP = createQueue(&queueProperties, &exception);
00099    mu_assert("create()", queueP == 0);
00100    mu_assert_checkWantException("create()", exception);
00101 
00102    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
00103    strncpy0(queueProperties.queueName, "connection_clientJoe", QUEUE_ID_MAX);
00104    strncpy0(queueProperties.tablePrefix, "XB_", QUEUE_PREFIX_MAX);
00105    queueProperties.maxNumOfEntries = 0;
00106    queueProperties.maxNumOfBytes = 0;
00107    queueP = createQueue(&queueProperties, &exception);
00108    mu_assert("create()", queueP == 0);
00109    mu_assert_checkWantException("create()", exception);
00110 
00111    queueProperties.maxNumOfEntries = 10;
00112    queueProperties.maxNumOfBytes = 100;
00113    queueP = createQueue(&queueProperties, &exception);
00114    mu_assert("create()", queueP != 0);
00115    mu_assert_checkException("create()", exception);
00116 
00117    queueP->put(0, 0, 0);
00118    queueP->put(0, 0, &exception);
00119    mu_assert_checkWantException("put()", exception);
00120 
00121    queueP->put(queueP, 0, &exception);
00122    mu_assert_checkWantException("put()", exception);
00123 
00124    memset(&queueEntry, 0, sizeof(QueueEntry));
00125    queueP->put(queueP, &queueEntry, &exception);
00126    mu_assert_checkWantException("put()", exception);
00127 
00128    entries = queueP->peekWithSamePriority(0, 0, 0, 0);
00129    entries = queueP->peekWithSamePriority(0, 0, 0, &exception);
00130    mu_assert_checkWantException("peekWithSamePriority()", exception);
00131 
00132    entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
00133    mu_assert_checkException("peekWithSamePriority()", exception);
00134    mu_assert("peekWithSamePriority() entries", entries != 0);
00135    mu_assertEqualsInt("peekWithSamePriority() entries", 0, entries->len);
00136    mu_assert("peekWithSamePriority() entries", entries->queueEntryArr == 0);
00137    freeQueueEntryArr(entries);
00138    entries = 0;
00139 
00140    numRemoved = queueP->randomRemove(0, 0, 0);
00141    numRemoved = queueP->randomRemove(0, 0, &exception);
00142    mu_assert_checkWantException("randomRemove()", exception);
00143    numRemoved = queueP->randomRemove(queueP, 0, &exception);
00144    mu_assert_checkException("randomRemove()", exception);
00145 
00146    numRemoved = queueP->randomRemove(queueP, entries, &exception);
00147    mu_assert_checkException("randomRemove()", exception);
00148    mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
00149 
00150    entries = (QueueEntryArr *)calloc(1, sizeof(QueueEntryArr));;
00151    numRemoved = queueP->randomRemove(queueP, entries, &exception);
00152    mu_assert_checkException("randomRemove()", exception);
00153    mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
00154    freeQueueEntryArr(entries);
00155    entries = 0;
00156 
00157    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
00158    queueP->clear(0, 0);
00159    queueP->clear(0, &exception);
00160    mu_assert_checkWantException("clear()", exception);
00161    queueP->clear(queueP, &exception);
00162    mu_assert_checkException("clear()", exception);
00163 
00164    queueP->empty(0);
00165 
00166    mu_assertEqualsInt("numOfEntries", 0, queueP->getNumOfEntries(queueP));
00167    mu_assertEqualsInt("numOfBytes", 0, (int)queueP->getNumOfBytes(queueP));
00168 
00169    mu_assertEqualsInt("numOfEntries", -1, queueP->getNumOfEntries(0));
00170    mu_assertEqualsInt("numOfBytes", -1, (int)queueP->getNumOfBytes(0));
00171 
00172    queueP->shutdown(&queueP, &exception);
00173    printf("Testing test_illegal DONE\n");
00174    printf("-----------------------------------------\n");
00175    return 0;
00176 }
00177 
00181 static const char * test_overflow()
00182 {
00183    ExceptionStruct exception;
00184    QueueProperties queueProperties;
00185    I_Queue *queueP = 0;
00186    const char *dbName = "xmlBlasterClient-C-Test.db";
00187 
00188    const int64_t idArr[] =   { 1081492136826000000ll, 1081492136856000000ll, 1081492136876000000ll, 1081492136911000000ll, 1081492136922000000ll };
00189    const int16_t prioArr[] = { 5                    , 1                    , 9                    , 9                    , 5 };
00190    const char *data[] =      { ""                   , ""                   , ""                   , ""                   , ""};
00191    const size_t numPut = sizeof(idArr)/sizeof(int64_t);
00192 
00193    printf("\n---------test_overflow-------------------\n");
00194    destroy(dbName); /* Delete old db file */
00195 
00196    memset(&queueProperties, 0, sizeof(QueueProperties));
00197    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
00198    strncpy0(queueProperties.queueName, "connection_clientJoe", QUEUE_ID_MAX);
00199    strncpy0(queueProperties.tablePrefix, "XB_", QUEUE_PREFIX_MAX);
00200    queueProperties.maxNumOfEntries = 4L;
00201    queueProperties.maxNumOfBytes = 25LL;
00202    queueProperties.logFp = loggingFp;
00203    queueProperties.logLevel = XMLBLASTER_LOG_TRACE;
00204    queueProperties.userObject = 0;
00205 
00206    queueP = createQueue(&queueProperties, &exception);
00207    mu_assertEqualsLong("create() maxNumOfEntries", 4L, (long)queueP->getMaxNumOfEntries(queueP));
00208    mu_assertEqualsString("create() maxNumOfBytes", int64ToStr(int64Str, 25LL), int64ToStr(int64StrX, queueP->getMaxNumOfBytes(queueP)));
00209    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
00210 
00211    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
00212 
00213    {  /* Test entry overflow */
00214       size_t i;
00215       size_t lenPut = 0;
00216       for (i=0; i<numPut; i++) {
00217          QueueEntry queueEntry;
00218          memset(&queueEntry, 0, sizeof(QueueEntry));
00219          queueEntry.priority = prioArr[i];
00220          queueEntry.isPersistent = true;
00221          queueEntry.uniqueId = idArr[i];
00222          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
00223          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
00224          queueEntry.embeddedBlob.data = (char *)data[i];
00225          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
00226 
00227          queueP->put(queueP, &queueEntry, &exception);
00228          if (i < 4) {
00229             mu_assert_checkException("put()", exception);
00230             lenPut += strlen(queueEntry.embeddedBlob.data);
00231          }
00232          else {
00233             mu_assert_checkWantException("put() numOfEntries overflow", exception);
00234          }
00235 
00236       }
00237       mu_assertEqualsInt("put() numOfEntries", 4, queueP->getNumOfEntries(queueP));
00238       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00239       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00240 
00241    }
00242    queueP->clear(queueP, &exception);
00243 
00244    {  /* Test byte overflow */
00245       size_t i;
00246       size_t lenPut = 0;
00247       for (i=0; i<numPut; i++) {
00248          QueueEntry queueEntry;
00249          memset(&queueEntry, 0, sizeof(QueueEntry));
00250          queueEntry.priority = prioArr[i];
00251          queueEntry.isPersistent = true;
00252          queueEntry.uniqueId = idArr[i];
00253          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
00254          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
00255          queueEntry.embeddedBlob.data = (char *)"0123456789";
00256          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
00257 
00258          queueP->put(queueP, &queueEntry, &exception);
00259          if (i < 3) {
00260             mu_assert_checkException("put()", exception);
00261             lenPut += strlen(queueEntry.embeddedBlob.data);
00262          }
00263          else {
00264             mu_assert_checkWantException("put() numOfBytes overflow", exception);
00265          }
00266 
00267       }
00268       mu_assertEqualsInt("put() numOfEntries", 3, queueP->getNumOfEntries(queueP));
00269       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00270       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00271 
00272    }
00273    queueP->clear(queueP, &exception);
00274 
00275    {  /* Test byte overflow with preset entry size */
00276       size_t i;
00277       size_t lenPut = 0;
00278       for (i=0; i<numPut; i++) {
00279          QueueEntry queueEntry;
00280          memset(&queueEntry, 0, sizeof(QueueEntry));
00281          queueEntry.priority = prioArr[i];
00282          queueEntry.isPersistent = true;
00283          queueEntry.uniqueId = idArr[i];
00284          queueEntry.sizeInBytes = 20;
00285          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
00286          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
00287          queueEntry.embeddedBlob.data = (char *)"0123456789";
00288          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
00289 
00290          queueP->put(queueP, &queueEntry, &exception);
00291          if (i < 2) { /* (index 0 and 1): 2 entries * 20 bytes = 40 bytes */
00292             mu_assert_checkException("put()", exception);
00293             lenPut += (int)queueEntry.sizeInBytes;
00294          }
00295          else {
00296             mu_assert_checkWantException("put() numOfBytes overflow", exception);
00297          }
00298 
00299       }
00300       mu_assertEqualsInt("put() numOfEntries", 2, queueP->getNumOfEntries(queueP));
00301       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00302       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00303 
00304    }
00305    queueP->clear(queueP, &exception);
00306 
00307    queueP->shutdown(&queueP, &exception);
00308    return 0;
00309 }
00310 
00314 static const char * test_queue()
00315 {
00316    ExceptionStruct exception;
00317    QueueEntryArr *entries = 0;
00318    QueueProperties queueProperties;
00319    I_Queue *queueP = 0;
00320    const char *dbName = "xmlBlasterClient-C-Test.db";
00321    const char *dummy = "bla";
00322 
00323    const int64_t idArr[] =   { 1081492136826000000ll, 1081492136856000000ll, 1081492136876000000ll, 1081492136911000000ll, 1081492136922000000ll };
00324    const int16_t prioArr[] = { 5                    , 1                    , 9                    , 9                    , 5 };
00325    const char *data[] =      { "1. Hello"           , "2. World"           , "3. High Prio 1"     , "4. High Prio 2"     , "5. done"};
00326    const size_t numPut = sizeof(idArr)/sizeof(int64_t);
00327    size_t lenPut = 0;
00328 
00329    printf("\n---------test_queue----------------------\n");
00330    destroy(dbName); /* Delete old db file */
00331 
00332    memset(&queueProperties, 0, sizeof(QueueProperties));
00333    strncpy0(queueProperties.dbName, dbName, QUEUE_DBNAME_MAX);
00334    strncpy0(queueProperties.queueName, "connection_clientJoe", QUEUE_ID_MAX);
00335    strncpy0(queueProperties.tablePrefix, "XB_", QUEUE_PREFIX_MAX);
00336    queueProperties.maxNumOfEntries = 10000000L;
00337    queueProperties.maxNumOfBytes = 1000000000LL;
00338    queueProperties.logFp = loggingFp;
00339    queueProperties.logLevel = XMLBLASTER_LOG_TRACE;
00340    queueProperties.userObject = (void *)dummy;
00341 
00342    queueP = createQueue(&queueProperties, &exception);
00343    mu_assert("create()", queueP != 0);
00344    mu_assert("create() QueueProperty", queueP->getProperties(queueP) != 0);
00345    mu_assert("create() userObject", queueP->userObject == dummy);
00346    mu_assertEqualsBool("create() isInitialized", true, queueP->isInitialized);
00347    mu_assertEqualsString("create() dbName", queueProperties.dbName, queueP->getProperties(queueP)->dbName);
00348    mu_assertEqualsString("create() tablePrefix", queueProperties.tablePrefix, queueP->getProperties(queueP)->tablePrefix);
00349    mu_assertEqualsString("create() queueName", queueProperties.queueName, queueP->getProperties(queueP)->queueName);
00350    mu_assertEqualsLong("create() maxNumOfEntries", 10000000L, (long)queueP->getMaxNumOfEntries(queueP));
00351    mu_assertEqualsString("create() maxNumOfBytes", int64ToStr(int64Str, 1000000000LL), int64ToStr(int64StrX, queueP->getMaxNumOfBytes(queueP)));
00352    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
00353 
00354    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
00355 
00356    {
00357       size_t i;
00358       for (i=0; i<numPut; i++) {
00359          QueueEntry queueEntry;
00360          memset(&queueEntry, 0, sizeof(QueueEntry));
00361          queueEntry.priority = prioArr[i];
00362          queueEntry.isPersistent = true;
00363          queueEntry.uniqueId = idArr[i];
00364          strncpy0(queueEntry.embeddedType, "MSG_RAW|publish", QUEUE_ENTRY_EMBEDDEDTYPE_LEN);
00365          queueEntry.embeddedType[QUEUE_ENTRY_EMBEDDEDTYPE_LEN-1] = 0;
00366          queueEntry.embeddedBlob.data = (char *)data[i];
00367          queueEntry.embeddedBlob.dataLen = strlen(queueEntry.embeddedBlob.data);
00368          lenPut += strlen(queueEntry.embeddedBlob.data);
00369 
00370          queueP->put(queueP, &queueEntry, &exception);
00371          mu_assert_checkException("put()", exception);
00372       }
00373       mu_assertEqualsInt("put() numOfEntries", (int)numPut, queueP->getNumOfEntries(queueP));
00374       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00375       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00376 
00377       printf("-----------------------------------------\n");
00378       printf("Testing shutdown and restart ...\n");
00379       queueP->shutdown(&queueP, &exception);
00380       mu_assert("shutdown()", queueP == 0);
00381 
00382       queueP = createQueue(&queueProperties, &exception);
00383       mu_assertEqualsInt("put() numOfEntries", (int)numPut, queueP->getNumOfEntries(queueP));
00384       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00385       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00386       printf("-----------------------------------------\n");
00387    }
00388 
00389    {
00390       int j;
00391       printf("-----------------------------------------\n");
00392       printf("Testing peekWithSamePriority 9 ...\n");
00393       for (j=0; j<10; j++) {
00394          entries = queueP->peekWithSamePriority(queueP, 0, 0, &exception);
00395          mu_assert_checkException("peekWithSamePriority()", exception);
00396          mu_assert(" peekWithSamePriority()", entries != 0);
00397          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 0, entries->len);
00398          freeQueueEntryArr(entries);
00399 
00400          entries = queueP->peekWithSamePriority(queueP, 1, -1, &exception);
00401          mu_assert_checkException("peekWithSamePriority()", exception);
00402          mu_assert(" peekWithSamePriority()", entries != 0);
00403          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00404          freeQueueEntryArr(entries);
00405 
00406          entries = queueP->peekWithSamePriority(queueP, -1, 3, &exception);
00407          mu_assert_checkException("peekWithSamePriority()", exception);
00408          mu_assert(" peekWithSamePriority()", entries != 0);
00409          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00410          freeQueueEntryArr(entries);
00411 
00412          mu_assertEqualsInt("put() numOfEntries", (int)numPut, queueP->getNumOfEntries(queueP));
00413          mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00414          mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00415       }
00416       {
00417          size_t i;
00418          entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
00419          mu_assert_checkException("peekWithSamePriority()", exception);
00420          mu_assert(" peekWithSamePriority()", entries != 0);
00421          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 2, entries->len);
00422          printf("testRun after peekWithSamePriority() dump %lu entries:\n", (unsigned long)entries->len);
00423          for (i=0; i<entries->len; i++) {
00424             char *tmp;
00425             size_t expectedIndex = i+2;
00426             QueueEntry *queueEntry = &entries->queueEntryArr[i];
00427             char *dump = queueEntryToXml(queueEntry, 200);
00428             printf("%s\n", dump);
00429             freeEntryDump(dump);
00430                            mu_assert("uniqueId fail", queueEntry->uniqueId == idArr[expectedIndex]);
00431             mu_assertEqualsString("uniqueId", int64ToStr(int64Str, idArr[expectedIndex]), int64ToStr(int64StrX, queueEntry->uniqueId));
00432             mu_assertEqualsInt("priority", 9, queueEntry->priority);
00433             mu_assertEqualsBool("persistent", true, queueEntry->isPersistent);
00434             mu_assertEqualsInt("bloblen", strlen(data[expectedIndex]), queueEntry->embeddedBlob.dataLen);
00435             tmp = strFromBlobAlloc(queueEntry->embeddedBlob.data, queueEntry->embeddedBlob.dataLen);
00436             mu_assertEqualsString("blob", data[expectedIndex], tmp);
00437             freeEntryDump(tmp);
00438          }
00439          freeQueueEntryArr(entries);
00440       }
00441       printf("-----------------------------------------\n");
00442    }
00443 
00444    {
00445       int32_t numRemoved;
00446       printf("-----------------------------------------\n");
00447       printf("Testing randomRemove prio=9 ...\n");
00448       printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
00449       entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
00450 
00451       numRemoved = queueP->randomRemove(queueP, entries, &exception);
00452       mu_assert_checkException("randomRemove()", exception);
00453       mu_assertEqualsInt("numRemoved", 2, (int)numRemoved);
00454 
00455       numRemoved = queueP->randomRemove(queueP, entries, &exception);
00456       mu_assert_checkException("randomRemove()", exception);
00457       mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
00458 
00459       freeQueueEntryArr(entries);
00460 
00461       mu_assertEqualsInt("put() numOfEntries", 3, queueP->getNumOfEntries(queueP));
00462       lenPut = strlen(data[0]) + strlen(data[1]) + strlen(data[4]);
00463       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00464       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00465       printf("-----------------------------------------\n");
00466    }
00467 
00468 
00469    {
00470       int j;
00471       printf("-----------------------------------------\n");
00472       printf("Testing peekWithSamePriority 5 ...\n");
00473       for (j=0; j<10; j++) {
00474          entries = queueP->peekWithSamePriority(queueP, 0, 0, &exception);
00475          mu_assert_checkException("peekWithSamePriority()", exception);
00476          mu_assert(" peekWithSamePriority()", entries != 0);
00477          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 0, entries->len);
00478          freeQueueEntryArr(entries);
00479 
00480          entries = queueP->peekWithSamePriority(queueP, 1, -1, &exception);
00481          mu_assert_checkException("peekWithSamePriority()", exception);
00482          mu_assert(" peekWithSamePriority()", entries != 0);
00483          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00484          freeQueueEntryArr(entries);
00485 
00486          entries = queueP->peekWithSamePriority(queueP, -1, 3, &exception);
00487          mu_assert_checkException("peekWithSamePriority()", exception);
00488          mu_assert(" peekWithSamePriority()", entries != 0);
00489          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00490          freeQueueEntryArr(entries);
00491       }
00492       {
00493          size_t i;
00494          entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
00495          mu_assert_checkException("peekWithSamePriority()", exception);
00496          mu_assert(" peekWithSamePriority()", entries != 0);
00497          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 2, entries->len);
00498          printf("testRun after peekWithSamePriority() dump %lu entries:\n", (unsigned long)entries->len);
00499          for (i=0; i<entries->len; i++) {
00500             char *tmp;
00501             int expectedIndex = (i==0) ? 0 : 4;
00502             QueueEntry *queueEntry = &entries->queueEntryArr[i];
00503             char *dump = queueEntryToXml(queueEntry, 200);
00504             printf("%s\n", dump);
00505             freeEntryDump(dump);
00506             mu_assertEqualsString("uniqueId", int64ToStr(int64Str, idArr[expectedIndex]), int64ToStr(int64StrX, queueEntry->uniqueId));
00507             mu_assertEqualsInt("priority", 5, queueEntry->priority);
00508             mu_assertEqualsBool("persistent", true, queueEntry->isPersistent);
00509             mu_assertEqualsInt("bloblen", strlen(data[expectedIndex]), queueEntry->embeddedBlob.dataLen);
00510             tmp = strFromBlobAlloc(queueEntry->embeddedBlob.data, queueEntry->embeddedBlob.dataLen);
00511             mu_assertEqualsString("blob", data[expectedIndex], tmp);
00512             freeEntryDump(tmp);
00513          }
00514          freeQueueEntryArr(entries);
00515       }
00516 
00517       mu_assertEqualsInt("put() numOfEntries", 3, queueP->getNumOfEntries(queueP));
00518       lenPut = strlen(data[0]) + strlen(data[1]) + strlen(data[4]);
00519       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00520       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00521       printf("-----------------------------------------\n");
00522    }
00523 
00524    {
00525       int32_t numRemoved;
00526       printf("-----------------------------------------\n");
00527       printf("Testing randomRemove prio=5 ...\n");
00528       printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
00529       entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
00530 
00531       numRemoved = queueP->randomRemove(queueP, entries, &exception);
00532       mu_assert_checkException("randomRemove()", exception);
00533       mu_assertEqualsInt("numRemoved", 2, (int)numRemoved);
00534 
00535       numRemoved = queueP->randomRemove(queueP, entries, &exception);
00536       mu_assert_checkException("randomRemove()", exception);
00537       mu_assertEqualsInt("numRemoved", 0, (int)numRemoved);
00538 
00539       freeQueueEntryArr(entries);
00540 
00541       mu_assertEqualsInt("put() numOfEntries", 1, queueP->getNumOfEntries(queueP));
00542       lenPut = strlen(data[1]);
00543       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00544       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00545       printf("-----------------------------------------\n");
00546    }
00547 
00548    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
00549 
00550    {
00551       int j;
00552       printf("-----------------------------------------\n");
00553       printf("Testing peekWithSamePriority 1 ...\n");
00554       for (j=0; j<10; j++) {
00555          entries = queueP->peekWithSamePriority(queueP, 0, 0, &exception);
00556          mu_assert_checkException("peekWithSamePriority()", exception);
00557          mu_assert(" peekWithSamePriority()", entries != 0);
00558          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 0, entries->len);
00559          freeQueueEntryArr(entries);
00560 
00561          entries = queueP->peekWithSamePriority(queueP, 1, -1, &exception);
00562          mu_assert_checkException("peekWithSamePriority()", exception);
00563          mu_assert(" peekWithSamePriority()", entries != 0);
00564          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00565          freeQueueEntryArr(entries);
00566 
00567          entries = queueP->peekWithSamePriority(queueP, -1, 3, &exception);
00568          mu_assert_checkException("peekWithSamePriority()", exception);
00569          mu_assert(" peekWithSamePriority()", entries != 0);
00570          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00571          freeQueueEntryArr(entries);
00572       }
00573       {
00574          size_t i;
00575          entries = queueP->peekWithSamePriority(queueP, -1, -1, &exception);
00576          mu_assert_checkException("peekWithSamePriority()", exception);
00577          mu_assert(" peekWithSamePriority()", entries != 0);
00578          mu_assertEqualsInt("peekWithSamePriority() numOfEntries", 1, entries->len);
00579          printf("testRun after peekWithSamePriority() dump %lu entries:\n", (unsigned long)entries->len);
00580          for (i=0; i<entries->len; i++) {
00581             char *tmp;
00582             int expectedIndex = (i==0) ? 1 : 0;
00583             QueueEntry *queueEntry = &entries->queueEntryArr[i];
00584             char *dump = queueEntryToXml(queueEntry, 200);
00585             printf("%s\n", dump);
00586             freeEntryDump(dump);
00587             mu_assertEqualsString("uniqueId", int64ToStr(int64Str, idArr[expectedIndex]), int64ToStr(int64StrX, queueEntry->uniqueId));
00588             mu_assertEqualsInt("priority", 1, queueEntry->priority);
00589             mu_assertEqualsBool("persistent", true, queueEntry->isPersistent);
00590             mu_assertEqualsInt("bloblen", strlen(data[expectedIndex]), queueEntry->embeddedBlob.dataLen);
00591             tmp = strFromBlobAlloc(queueEntry->embeddedBlob.data, queueEntry->embeddedBlob.dataLen);
00592             mu_assertEqualsString("blob", data[expectedIndex], tmp);
00593             freeEntryDump(tmp);
00594          }
00595          freeQueueEntryArr(entries);
00596       }
00597 
00598       mu_assertEqualsInt("put() numOfEntries", 1, queueP->getNumOfEntries(queueP));
00599       lenPut = strlen(data[1]);
00600       mu_assertEqualsInt("put() numOfBytes", lenPut, (int)queueP->getNumOfBytes(queueP));
00601       mu_assertEqualsBool("put() empty", false, queueP->empty(queueP));
00602       printf("-----------------------------------------\n");
00603    }
00604    
00605    queueP->clear(queueP, &exception);
00606    printf("Queue numOfEntries=%d, numOfBytes=%s, empty=%s\n", queueP->getNumOfEntries(queueP), int64ToStr(int64Str, queueP->getNumOfBytes(queueP)), queueP->empty(queueP) ? "true" : "false");
00607    mu_assertEqualsInt("put() numOfEntries", 0, queueP->getNumOfEntries(queueP));
00608    mu_assertEqualsInt("put() numOfBytes", 0, (int)queueP->getNumOfBytes(queueP));
00609    mu_assertEqualsBool("put() empty", true, queueP->empty(queueP));
00610 
00611    queueP->shutdown(&queueP, &exception);
00612 
00613    destroy(dbName); /* Delete the db file */
00614 
00615    return 0;
00616 }
00617 
00618 
00619 static const char *all_tests()
00620 {
00621    mu_run_test(test_illegal);
00622    mu_run_test(test_queue);
00623    mu_run_test(test_overflow);
00624    return 0;
00625 }
00626 
00627 int main(int argc_, char **argv_)
00628 {
00629    const char *result;
00630    argc = argc_;
00631    argv = argv_;
00632    if (argc || argv) {} /* to avoid compiler warning */
00633 
00634    result = all_tests();
00635 
00636    if (result != 0) {
00637       printf("%s\n", result);
00638    }
00639    else {
00640       printf("ALL TESTS PASSED\n");
00641    }
00642    printf("Tests run: %d\n", tests_run);
00643 
00644    return result != 0;
00645 }
00646 
00647 
00648 #else /* XMLBLASTER_PERSISTENT_QUEUE */
00649 
00650 int main(int argc_, char **argv_)
00651 {
00652    printf("C-client is compiled without -DXMLBLASTER_PERSISTENT_QUEUE=1, no persistent queue tested\n");
00653    if (*MU_ASSERT_TEXT || tests_run) {} /* To suppress compiler warning */
00654    return 0;
00655 }
00656 
00657 #endif /* XMLBLASTER_PERSISTENT_QUEUE */
00658