Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief TMQ test client /limits
0003  *  TODO: add test case for: tmq session timeout 
0004  *   1. no commit in time => TPEABORT.
0005  *   2. forward tpenqueue() (ERRORQ not defined). Define after a while, msg shall be put in errorq
0006  *
0007  * @file atmiclt86.c
0008  */
0009 /* -----------------------------------------------------------------------------
0010  * Enduro/X Middleware Platform for Distributed Transaction Processing
0011  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0012  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0013  * This software is released under one of the following licenses:
0014  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0015  * See LICENSE file for full text.
0016  * -----------------------------------------------------------------------------
0017  * AGPL license:
0018  *
0019  * This program is free software; you can redistribute it and/or modify it under
0020  * the terms of the GNU Affero General Public License, version 3 as published
0021  * by the Free Software Foundation;
0022  *
0023  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0024  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0025  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0026  * for more details.
0027  *
0028  * You should have received a copy of the GNU Affero General Public License along 
0029  * with this program; if not, write to the Free Software Foundation, Inc.,
0030  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0031  *
0032  * -----------------------------------------------------------------------------
0033  * A commercial use license is available from Mavimax, Ltd
0034  * contact@mavimax.com
0035  * -----------------------------------------------------------------------------
0036  */
0037 #include <string.h>
0038 #include <stdio.h>
0039 #include <stdlib.h>
0040 #include <memory.h>
0041 #include <unistd.h>
0042 
0043 #include <atmi.h>
0044 #include <ubf.h>
0045 #include <ndebug.h>
0046 #include <test.fd.h>
0047 #include <ndrstandard.h>
0048 #include <ubfutil.h>
0049 #include <nstopwatch.h>
0050 #include <nstdutil.h>
0051 /*---------------------------Externs------------------------------------*/
0052 /*---------------------------Macros-------------------------------------*/
0053 /*---------------------------Enums--------------------------------------*/
0054 /*---------------------------Typedefs-----------------------------------*/
0055 /*---------------------------Globals------------------------------------*/
0056 /*---------------------------Statics------------------------------------*/
0057 /*---------------------------Prototypes---------------------------------*/
0058 exprivate int basic_tmqrestart(int maxmsg);
0059 exprivate int basic_tmsrvrestart(int maxmsg);
0060 exprivate int basic_diskfull(int maxmsg);
0061 exprivate int basic_commit_shut(int maxmsg);
0062 exprivate int basic_loadprep(int maxmsg);
0063 exprivate int basic_tmsrvdiskerr(int maxmsg);
0064 exprivate int basic_badmsg(int maxmsg);
0065 exprivate int basic_commit_crash(int maxmsg);
0066 exprivate int basic_deqwriteerr(int maxmsg);
0067 exprivate int basic_enqdeq(int maxmsg);
0068 exprivate int basic_rmrollback(int maxmsg);
0069 exprivate int basic_rmnorollback(int maxmsg);
0070 exprivate int basic_fwdcrash(int maxmsg);
0071 exprivate int basic_autoperf(int maxmsg);
0072 exprivate int basic_txtout(int maxmsg);
0073 exprivate int basic_seqvalid(int maxmsg);
0074 exprivate int basic_emptyq_commit_crash(int maxmsg);
0075 
0076 extern int basic_abort_rules(int maxmsg);
0077 extern int basic_errorq(void);
0078 extern int basic_crashloop(char *qname);
0079 
0080 int main(int argc, char** argv)
0081 {
0082     int ret = EXSUCCEED;
0083     
0084     if (argc<=1)
0085     {
0086         NDRX_LOG(log_error, "usage: %s <test_case: qfull>", argv[0]);
0087         return EXFAIL;
0088     }
0089     NDRX_LOG(log_error, "\n\n\n\n\n !!!!!!!!!!!!!! TEST CASE %s !!!!!!!! \n\n\n\n\n\n", argv[1]);
0090     
0091     if (EXSUCCEED!=tpopen())
0092     {
0093         EXFAIL_OUT(ret);
0094     }
0095     
0096     if (0==strcmp(argv[1], "rmrollback"))
0097     {
0098         return basic_rmrollback(1200);
0099     }
0100     else if (0==strcmp(argv[1], "rmnorollback"))
0101     {
0102         return basic_rmnorollback(1200);
0103     }
0104     else if (0==strcmp(argv[1], "autoperf"))
0105     {
0106         return basic_autoperf(200);
0107     }
0108     else if (0==strcmp(argv[1], "tmqrestart"))
0109     {
0110         return basic_tmqrestart(1200);
0111     }
0112     else if (0==strcmp(argv[1], "fwdcrash"))
0113     {
0114         return basic_fwdcrash(1200);
0115     }
0116     else if (0==strcmp(argv[1], "tmsrvrestart"))
0117     {
0118         return basic_tmsrvrestart(1200);
0119     }
0120     else if (0==strcmp(argv[1], "commit_shut"))
0121     {
0122         return basic_commit_shut(1200);
0123     }
0124     else if (0==strcmp(argv[1], "loadprep"))
0125     {
0126         return basic_loadprep(1200);
0127     }
0128     else if (0==strcmp(argv[1], "diskfull"))
0129     {
0130         return basic_diskfull(10);
0131     }
0132     else if (0==strcmp(argv[1], "tmsrvdiskerr"))
0133     {
0134         return basic_tmsrvdiskerr(100);
0135     }
0136     else if (0==strcmp(argv[1], "badmsg"))
0137     {
0138         return basic_badmsg(100);
0139     }
0140     else if (0==strcmp(argv[1], "commit_crash"))
0141     {
0142         return basic_commit_crash(100);
0143     }
0144     else if (0==strcmp(argv[1], "deqwriteerr"))
0145     {
0146         return basic_deqwriteerr(100);
0147     }
0148     else if (0==strcmp(argv[1], "abortrules"))
0149     {
0150         return basic_abort_rules(1);
0151     }
0152     else if (0==strcmp(argv[1], "errorq"))
0153     {
0154         return basic_errorq();
0155     }
0156     else if (0==strcmp(argv[1], "crashloop"))
0157     {
0158         return basic_crashloop("ERROR");
0159     }
0160     else if (0==strcmp(argv[1], "crashloop_t"))
0161     {
0162         return basic_crashloop("ERROR_T");
0163     }
0164     else if (0==strcmp(argv[1], "enqdeq"))
0165     {
0166         return basic_enqdeq(200);
0167     }
0168     else if (0==strcmp(argv[1], "txtout"))
0169     {
0170         return basic_txtout(1);
0171     }
0172     else if (0==strcmp(argv[1], "emptyq_commit_crash"))
0173     {
0174         return basic_emptyq_commit_crash(1);
0175     }
0176     else if (0==strcmp(argv[1], "seqvalid"))
0177     {
0178         return basic_seqvalid(1000);
0179     }
0180     else
0181     {
0182         NDRX_LOG(log_error, "Invalid test case!");
0183         return EXFAIL;
0184     }
0185     
0186 out:
0187 
0188     tpclose();
0189 
0190     return ret;   
0191 }
0192 
0193 
0194 /**
0195  * TMQ resource manager performs rollback due to timeout
0196  * @param maxmsg max messages to be ok
0197  */
0198 exprivate int basic_rmrollback(int maxmsg)
0199 {
0200     int ret = EXSUCCEED;
0201     TPQCTL qc;
0202     int i;
0203     
0204     NDRX_LOG(log_error, "case rmrollback");
0205     if (EXSUCCEED!=tpbegin(9999, 0))
0206     {
0207         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0208         EXFAIL_OUT(ret);
0209     }
0210     
0211     /* Initial test... */
0212     for (i=0; i<maxmsg; i++)
0213     {
0214         char *testbuf_ref = tpalloc("CARRAY", "", 10);
0215         long len=10;
0216 
0217         testbuf_ref[0]=0;
0218         testbuf_ref[1]=1;
0219         testbuf_ref[2]=2;
0220         testbuf_ref[3]=3;
0221         testbuf_ref[4]=4;
0222         testbuf_ref[5]=5;
0223         testbuf_ref[6]=6;
0224         testbuf_ref[7]=7;
0225         testbuf_ref[8]=8;
0226         testbuf_ref[9]=9;
0227 
0228         /* alloc output buffer */
0229         if (NULL==testbuf_ref)
0230         {
0231             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0232                     tpstrerror(tperrno));
0233             EXFAIL_OUT(ret);
0234         }
0235 
0236         /* enqueue the data buffer */
0237         memset(&qc, 0, sizeof(qc));
0238         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
0239                 len, 0))
0240         {
0241             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0242                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0243             EXFAIL_OUT(ret);
0244         }
0245 
0246         tpfree(testbuf_ref);
0247     }
0248     
0249     /* sleep 55, as tout is set to 45 see -T */
0250     sleep(55);
0251     
0252     if (EXSUCCEED==tpcommit(0))
0253     {
0254         NDRX_LOG(log_error, "TESTERROR: commit must fail, as tmq does not know the tran");
0255         EXFAIL_OUT(ret);
0256     }
0257 
0258     if (TPEABORT!=tperrno)
0259     {
0260         NDRX_LOG(log_error, "TESTERROR: expected %d got %d", TPEABORT, tperrno);
0261         EXFAIL_OUT(ret);
0262     }
0263     
0264     /* no messages are available */
0265     for (i=0; i<1; i++)
0266     {
0267         long len=0;
0268         char *buf;
0269         buf = tpalloc("CARRAY", "", 100);
0270         memset(&qc, 0, sizeof(qc));
0271 
0272         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
0273         {
0274             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, must be none (all aborted)!");
0275             EXFAIL_OUT(ret);
0276         }
0277         
0278         if (TPEDIAGNOSTIC!=tperrno)
0279         {
0280             NDRX_LOG(log_error, "TESTERROR: expected %d got %d", TPEDIAGNOSTIC, tperrno);
0281             EXFAIL_OUT(ret);
0282         }
0283         
0284         if (QMENOMSG!=qc.diagnostic)
0285         {
0286             NDRX_LOG(log_error, "TESTERROR: expected %d got %ld", QMENOMSG, qc.diagnostic);
0287             EXFAIL_OUT(ret);
0288         }
0289         
0290         tpfree(buf);
0291     }
0292     
0293 out:
0294     
0295     if (EXSUCCEED!=tpterm())
0296     {
0297         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
0298         ret=EXFAIL;
0299         goto out;
0300     }
0301 
0302     return ret;
0303 }
0304 
0305 
0306 /**
0307  * Validate the sequence of the message (transaction sync)
0308  * Just load the messages. Activation / Validation is done from shell script
0309  * @param maxmsg max messages to be ok
0310  */
0311 exprivate int basic_seqvalid(int maxmsg)
0312 {
0313     int ret = EXSUCCEED;
0314     TPQCTL qc;
0315     long i;
0316     
0317     NDRX_LOG(log_error, "case seqvalid");
0318     if (EXSUCCEED!=tpbegin(9999, 0))
0319     {
0320         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0321         EXFAIL_OUT(ret);
0322     }
0323     
0324     /* Initial test... */
0325     for (i=1; i<maxmsg+1; i++)
0326     {
0327         UBFH *testbuf_ref = (UBFH *)tpalloc("UBF", "", 1024);
0328         
0329         /* alloc output buffer */
0330         if (NULL==testbuf_ref)
0331         {
0332             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0333                     tpstrerror(tperrno));
0334             EXFAIL_OUT(ret);
0335         }
0336         
0337         if (EXSUCCEED!=Bchg(testbuf_ref, T_LONG_FLD, 0, (char *)&i, 0))
0338         {
0339             NDRX_LOG(log_error, "TESTERROR: Bchg failed %s", 
0340                     Bstrerror(Berror));
0341             EXFAIL_OUT(ret);
0342         }
0343 
0344         /* enqueue the data buffer */
0345         memset(&qc, 0, sizeof(qc));
0346         if (EXSUCCEED!=tpenqueue("MYSPACE", "SEQVALID", &qc, (char *)testbuf_ref, 0, 0))
0347         {
0348             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0349                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0350             EXFAIL_OUT(ret);
0351         }
0352 
0353         tpfree((char *)testbuf_ref);
0354     }
0355     
0356     if (EXSUCCEED!=tpcommit(0))
0357     {
0358         NDRX_LOG(log_error, "TESTERROR: failed to commit: %s", tpstrerror(tperrno));
0359         EXFAIL_OUT(ret);
0360     }
0361     
0362 out:
0363     
0364     if (EXSUCCEED!=tpterm())
0365     {
0366         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
0367         ret=EXFAIL;
0368         goto out;
0369     }
0370 
0371     return ret;
0372 }
0373 
0374 
0375 /**
0376  * Transaction timeout - TPETRAN test (cannot join as rolled back)
0377  * @param maxmsg max messages to be ok
0378  */
0379 exprivate int basic_txtout(int maxmsg)
0380 {
0381     int ret = EXSUCCEED;
0382     TPQCTL qc, o_qc;
0383     int i;
0384     
0385     NDRX_LOG(log_error, "case txtout");
0386     
0387     /* add 1 msg.. with COR & MSGID */
0388     
0389     if (EXSUCCEED!=tpbegin(60, 0))
0390     {
0391         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0392         EXFAIL_OUT(ret);
0393     }
0394     
0395     for (i=0; i<maxmsg; i++)
0396     {
0397         char *testbuf_ref = tpalloc("CARRAY", "", 10);
0398         long len=10;
0399 
0400         testbuf_ref[0]=0;
0401         testbuf_ref[1]=1;
0402         testbuf_ref[2]=2;
0403         testbuf_ref[3]=3;
0404         testbuf_ref[4]=4;
0405         testbuf_ref[5]=5;
0406         testbuf_ref[6]=6;
0407         testbuf_ref[7]=7;
0408         testbuf_ref[8]=8;
0409         testbuf_ref[9]=9;
0410 
0411         /* alloc output buffer */
0412         if (NULL==testbuf_ref)
0413         {
0414             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0415                     tpstrerror(tperrno));
0416             EXFAIL_OUT(ret);
0417         }
0418 
0419         /* enqueue the data buffer */
0420         memset(&o_qc, 0, sizeof(o_qc));
0421         /* set corid & msgid.. */
0422         o_qc.flags|=(TPQCORRID);
0423         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &o_qc, testbuf_ref, 
0424                 len, 0))
0425         {
0426             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0427                     tpstrerror(tperrno), o_qc.diagnostic, o_qc.diagmsg);
0428             EXFAIL_OUT(ret);
0429         }
0430 
0431         tpfree(testbuf_ref);
0432     }
0433     
0434     /* OK ... */
0435     if (EXSUCCEED!=tpcommit(0))
0436     {
0437         NDRX_LOG(log_error, "TESTERROR: failed to commit");
0438         EXFAIL_OUT(ret);
0439     }
0440     
0441     /* try to late deq join... */
0442     
0443     if (EXSUCCEED!=tpbegin(1, 0))
0444     {
0445         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0446         EXFAIL_OUT(ret);
0447     }
0448     
0449     sleep(5);
0450     
0451     do
0452     {
0453         long len=0;
0454         char *buf;
0455         buf = tpalloc("CARRAY", "", 100);
0456         memset(&qc, 0, sizeof(qc));
0457 
0458         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
0459         {
0460             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, must be none (all aborted)!");
0461             EXFAIL_OUT(ret);
0462         }
0463         
0464         if (TPETRAN!=tperrno)
0465         {
0466             NDRX_LOG(log_error, "TESTERROR: expected %d got %d", TPETRAN, tperrno);
0467             EXFAIL_OUT(ret);
0468         }
0469         
0470         tpfree(buf);
0471     } while (0);
0472     
0473     tpabort(0);
0474     
0475     
0476     /* try to late deq join... corid */
0477     
0478     if (EXSUCCEED!=tpbegin(1, 0))
0479     {
0480         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0481         EXFAIL_OUT(ret);
0482     }
0483     
0484     sleep(5);
0485     
0486     do
0487     {
0488         long len=0;
0489         char *buf;
0490         buf = tpalloc("CARRAY", "", 100);
0491         memset(&qc, 0, sizeof(qc));
0492         
0493         qc.flags|=TPQGETBYCORRID;
0494 
0495         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
0496         {
0497             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, must be none (all aborted)!");
0498             EXFAIL_OUT(ret);
0499         }
0500         
0501         if (TPETRAN!=tperrno)
0502         {
0503             NDRX_LOG(log_error, "TESTERROR: expected %d got %d", TPETRAN, tperrno);
0504             EXFAIL_OUT(ret);
0505         }
0506         
0507         tpfree(buf);
0508     } while (0);
0509     
0510     
0511     tpabort(0);
0512     
0513     /* try to late deq join... msgid */
0514     
0515     if (EXSUCCEED!=tpbegin(1, 0))
0516     {
0517         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0518         EXFAIL_OUT(ret);
0519     }
0520     
0521     sleep(5);
0522     
0523     do
0524     {
0525         long len=0;
0526         char *buf;
0527         buf = tpalloc("CARRAY", "", 100);
0528         memset(&qc, 0, sizeof(qc));
0529         
0530         qc.flags|=TPQGETBYMSGID;
0531         
0532         memcpy(qc.msgid, o_qc.msgid, sizeof(qc.msgid));
0533 
0534         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
0535         {
0536             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, must be none (all aborted)!");
0537             EXFAIL_OUT(ret);
0538         }
0539         
0540         if (TPETRAN!=tperrno)
0541         {
0542             NDRX_LOG(log_error, "TESTERROR: expected %d got %d", TPETRAN, tperrno);
0543             EXFAIL_OUT(ret);
0544         }
0545         
0546         tpfree(buf);
0547         
0548     } while (0);
0549     
0550     
0551     tpabort(0);
0552     
0553     /* try enq to expired tran... */
0554     
0555     if (EXSUCCEED!=tpbegin(1, 0))
0556     {
0557         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0558         EXFAIL_OUT(ret);
0559     }
0560     
0561     sleep(5);
0562     
0563     /* Initial test... */
0564     for (i=0; i<maxmsg; i++)
0565     {
0566         char *testbuf_ref = tpalloc("CARRAY", "", 10);
0567         long len=10;
0568 
0569         testbuf_ref[0]=0;
0570         testbuf_ref[1]=1;
0571         testbuf_ref[2]=2;
0572         testbuf_ref[3]=3;
0573         testbuf_ref[4]=4;
0574         testbuf_ref[5]=5;
0575         testbuf_ref[6]=6;
0576         testbuf_ref[7]=7;
0577         testbuf_ref[8]=8;
0578         testbuf_ref[9]=9;
0579 
0580         /* alloc output buffer */
0581         if (NULL==testbuf_ref)
0582         {
0583             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0584                     tpstrerror(tperrno));
0585             EXFAIL_OUT(ret);
0586         }
0587 
0588         /* enqueue the data buffer */
0589         memset(&qc, 0, sizeof(qc));
0590         if (EXSUCCEED==tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
0591                 len, 0))
0592         {
0593             NDRX_LOG(log_error, "TESTERROR: tpenqueue() must fail but was OK!");
0594             EXFAIL_OUT(ret);
0595         }
0596 
0597         /* Must be TPETRAN (but only for static mode...) 
0598          * For dynamic mode it is TPESVCFAIL (probably shall fix in future to TPETRAN)
0599          */
0600         if (TPETRAN!=tperrno)
0601         {
0602             NDRX_LOG(log_error, "TESTERROR: tpenqueue() Expected TPETRAN, got %s diag: %d:%s", 
0603                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0604             EXFAIL_OUT(ret);
0605         }
0606 
0607         tpfree(testbuf_ref);
0608     }
0609     
0610     
0611     tpabort(0);
0612     
0613     /* clean up the q finally... */
0614     
0615     if (EXSUCCEED!=tpbegin(60, 0))
0616     {
0617         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0618         EXFAIL_OUT(ret);
0619     }
0620     
0621     do
0622     {
0623         long len=0;
0624         char *buf;
0625         buf = tpalloc("CARRAY", "", 100);
0626         memset(&qc, 0, sizeof(qc));
0627         
0628         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
0629         {
0630             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0631                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0632             EXFAIL_OUT(ret);
0633         }
0634         
0635         tpfree(buf);
0636         
0637     } while (0);
0638     
0639     if (EXSUCCEED!=tpcommit(0))
0640     {
0641         NDRX_LOG(log_error, "Failed to commit: %s", tpstrerror(tperrno));
0642         EXFAIL_OUT(ret);
0643     }
0644     
0645 out:
0646     
0647     if (EXSUCCEED!=tpterm())
0648     {
0649         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
0650         ret=EXFAIL;
0651         goto out;
0652     }
0653 
0654     return ret;
0655 }
0656 
0657 /**
0658  * We slowly add new msgs... over the 30 sec session -> no rollback as
0659  * counter is being reset
0660  * @param maxmsg max messages to be ok
0661  */
0662 exprivate int basic_rmnorollback(int maxmsg)
0663 {
0664     int ret = EXSUCCEED;
0665     TPQCTL qc;
0666     int i;
0667     
0668     NDRX_LOG(log_error, "case rmnorollback");
0669     if (EXSUCCEED!=tpbegin(9999, 0))
0670     {
0671         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0672         EXFAIL_OUT(ret);
0673     }
0674     
0675     /* Initial test... */
0676     for (i=0; i<6; i++)
0677     {
0678         char *testbuf_ref = tpalloc("CARRAY", "", 10);
0679         long len=10;
0680 
0681         testbuf_ref[0]=0;
0682         testbuf_ref[1]=1;
0683         testbuf_ref[2]=2;
0684         testbuf_ref[3]=3;
0685         testbuf_ref[4]=4;
0686         testbuf_ref[5]=5;
0687         testbuf_ref[6]=6;
0688         testbuf_ref[7]=7;
0689         testbuf_ref[8]=8;
0690         testbuf_ref[9]=9;
0691 
0692         /* alloc output buffer */
0693         if (NULL==testbuf_ref)
0694         {
0695             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0696                     tpstrerror(tperrno));
0697             EXFAIL_OUT(ret);
0698         }
0699 
0700         /* enqueue the data buffer */
0701         memset(&qc, 0, sizeof(qc));
0702         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
0703                 len, 0))
0704         {
0705             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0706                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0707             EXFAIL_OUT(ret);
0708         }
0709 
0710         tpfree(testbuf_ref);
0711         
0712         /* overall 60 sec... and shall complete all OK */
0713         sleep(10);
0714     }
0715     
0716     if (EXSUCCEED!=tpcommit(0))
0717     {
0718         NDRX_LOG(log_error, "TESTERROR: failed to commit: %s", tpstrerror(tperrno));
0719         EXFAIL_OUT(ret);
0720     }
0721     
0722     /* Download all msgs OK */
0723     for (i=0; i<6; i++)
0724     {
0725         long len=0;
0726         char *buf;
0727         buf = tpalloc("CARRAY", "", 100);
0728         memset(&qc, 0, sizeof(qc));
0729 
0730         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
0731         {
0732             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeue failed!: %s %ld", 
0733                     tpstrerror(tperrno), qc.diagnostic);
0734             EXFAIL_OUT(ret);
0735         }
0736         tpfree(buf);
0737     }
0738 
0739 
0740 out:
0741     
0742     if (EXSUCCEED!=tpterm())
0743     {
0744         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
0745         ret=EXFAIL;
0746         goto out;
0747     }
0748 
0749     return ret;
0750 }
0751 
0752 
0753 /**
0754  * Check out message performance -> no sleep if have any task.
0755  * @param maxmsg max messages to be ok
0756  */
0757 exprivate int basic_autoperf(int maxmsg)
0758 {
0759     int ret = EXSUCCEED;
0760     TPQCTL qc;
0761     int i;
0762     char *buf = NULL;
0763     char *testbuf_ref = tpalloc("CARRAY", "", 10);
0764     long len=10;
0765 
0766     /* alloc output buffer */
0767     if (NULL==testbuf_ref)
0768     {
0769         NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0770                 tpstrerror(tperrno));
0771         EXFAIL_OUT(ret);
0772     }
0773 
0774     testbuf_ref[0]=0;
0775     testbuf_ref[1]=1;
0776     testbuf_ref[2]=2;
0777     testbuf_ref[3]=3;
0778     testbuf_ref[4]=4;
0779     testbuf_ref[5]=5;
0780     testbuf_ref[6]=6;
0781     testbuf_ref[7]=7;
0782     testbuf_ref[8]=8;
0783     testbuf_ref[9]=9;
0784 
0785     NDRX_LOG(log_error, "case autoperf");
0786     if (EXSUCCEED!=tpbegin(9999, 0))
0787     {
0788         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0789         EXFAIL_OUT(ret);
0790     }
0791     
0792     /* Initial test... */
0793     for (i=0; i<200; i++)
0794     {        
0795         /* add 1 queues, to pull in the forward lists... */
0796         if (i<1)
0797         {
0798             memset(&qc, 0, sizeof(qc));
0799             if (EXSUCCEED!=tpenqueue("MYSPACE", "PERF1", &qc, testbuf_ref, 
0800                 len, 0))
0801             {
0802                 NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0803                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0804                 EXFAIL_OUT(ret);
0805             }
0806         }
0807         
0808         /* middle queues have more, had issues that trailing queues made sleep */
0809         memset(&qc, 0, sizeof(qc));
0810         if (EXSUCCEED!=tpenqueue("MYSPACE", "PERF2", &qc, testbuf_ref, 
0811             len, 0))
0812         {
0813             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0814                 tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0815             EXFAIL_OUT(ret);
0816         }
0817 
0818         memset(&qc, 0, sizeof(qc));
0819         if (EXSUCCEED!=tpenqueue("MYSPACE", "PERF3", &qc, testbuf_ref, 
0820             len, 0))
0821         {
0822             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0823                 tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0824             EXFAIL_OUT(ret);
0825         }
0826 
0827         /* order of Q is important here as it spawns queues... and later uses
0828          * scan in that order
0829          */
0830         if (i<1)
0831         {
0832             memset(&qc, 0, sizeof(qc));
0833             if (EXSUCCEED!=tpenqueue("MYSPACE", "PERF4", &qc, testbuf_ref, 
0834                 len, 0))
0835             {
0836                 NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0837                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0838                 EXFAIL_OUT(ret);
0839             }
0840         }
0841 
0842     }
0843     
0844     if (EXSUCCEED!=tpcommit(0))
0845     {
0846         NDRX_LOG(log_error, "TESTERROR: failed to commit: %s", tpstrerror(tperrno));
0847         EXFAIL_OUT(ret);
0848     }
0849     /* all queues must be empty! */
0850     sleep(60);
0851     
0852     buf = tpalloc("CARRAY", "", 100);
0853 
0854     if (NULL==buf)
0855     {
0856         NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0857                 tpstrerror(tperrno));
0858         EXFAIL_OUT(ret);
0859     }
0860 
0861     len=100;
0862     memset(&qc, 0, sizeof(qc));
0863     if (EXSUCCEED==tpdequeue("MYSPACE", "PERF1", &qc, (char **)&buf, &len, 0))
0864     {
0865         NDRX_LOG(log_error, "TESTERROR: PERF1 must be empty!");
0866         EXFAIL_OUT(ret);
0867     }
0868 
0869     memset(&qc, 0, sizeof(qc));
0870     if (EXSUCCEED==tpdequeue("MYSPACE", "PERF2", &qc, (char **)&buf, &len, 0))
0871     {
0872         NDRX_LOG(log_error, "TESTERROR: PERF2 must be empty!");
0873         EXFAIL_OUT(ret);
0874     }
0875 
0876     memset(&qc, 0, sizeof(qc));
0877     if (EXSUCCEED==tpdequeue("MYSPACE", "PERF3", &qc, (char **)&buf, &len, 0))
0878     {
0879         NDRX_LOG(log_error, "TESTERROR: PERF3 must be empty!");
0880         EXFAIL_OUT(ret);
0881     }
0882 
0883     memset(&qc, 0, sizeof(qc));
0884     if (EXSUCCEED==tpdequeue("MYSPACE", "PERF4", &qc, (char **)&buf, &len, 0))
0885     {
0886         NDRX_LOG(log_error, "TESTERROR: PERF4 must be empty!");
0887         EXFAIL_OUT(ret);
0888     }
0889 
0890 out:
0891     if (NULL!=testbuf_ref)
0892     {
0893         tpfree(testbuf_ref);
0894     }
0895 
0896     if (NULL!=buf)
0897     {
0898         tpfree(buf);
0899     }
0900     
0901     if (EXSUCCEED!=tpterm())
0902     {
0903         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
0904         ret=EXFAIL;
0905         goto out;
0906     }
0907 
0908     return ret;
0909 }
0910 
0911 /**
0912  * Verify that we can process number of messages
0913  * @param maxmsg max messages to be ok
0914  */
0915 exprivate int basic_tmqrestart(int maxmsg)
0916 {
0917     int ret = EXSUCCEED;
0918     TPQCTL qc;
0919     int i;
0920     
0921     NDRX_LOG(log_error, "case tmqrestart");
0922     if (EXSUCCEED!=tpbegin(9999, 0))
0923     {
0924         NDRX_LOG(log_error, "TESTERROR: failed to begin");
0925         EXFAIL_OUT(ret);
0926     }
0927     
0928     /* Initial test... */
0929     for (i=0; i<maxmsg; i++)
0930     {
0931         char *testbuf_ref = tpalloc("CARRAY", "", 10);
0932         long len=10;
0933 
0934         testbuf_ref[0]=0;
0935         testbuf_ref[1]=1;
0936         testbuf_ref[2]=2;
0937         testbuf_ref[3]=3;
0938         testbuf_ref[4]=4;
0939         testbuf_ref[5]=5;
0940         testbuf_ref[6]=6;
0941         testbuf_ref[7]=7;
0942         testbuf_ref[8]=8;
0943         testbuf_ref[9]=9;
0944 
0945         /* alloc output buffer */
0946         if (NULL==testbuf_ref)
0947         {
0948             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
0949                     tpstrerror(tperrno));
0950             EXFAIL_OUT(ret);
0951         }
0952 
0953         /* enqueue the data buffer */
0954         memset(&qc, 0, sizeof(qc));
0955         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
0956                 len, 0))
0957         {
0958             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0959                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0960             EXFAIL_OUT(ret);
0961         }
0962 
0963         tpfree(testbuf_ref);
0964     }
0965     
0966     /* restart tmqueue.... 
0967      * it shall be able to commit OK
0968      */
0969     if (EXSUCCEED!=system("xadmin stop -s tmqueue"))
0970     {
0971         NDRX_LOG(log_error, "TESTERROR: failed to stop tmqueue");
0972         EXFAIL_OUT(ret);
0973     }
0974     
0975     if (EXSUCCEED!=system("xadmin start -s tmqueue"))
0976     {
0977         NDRX_LOG(log_error, "TESTERROR: failed to start tmqueue");
0978         EXFAIL_OUT(ret);
0979     }
0980     
0981     /* no messages available OK */
0982     for (i=0; i<1; i++)
0983     {
0984         long len=0;
0985         char *buf;
0986         buf = tpalloc("CARRAY", "", 100);
0987         memset(&qc, 0, sizeof(qc));
0988 
0989         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
0990         {
0991             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, even already in progress!");
0992             EXFAIL_OUT(ret);
0993         }
0994         
0995         tpfree(buf);
0996     }
0997     
0998     if (EXSUCCEED==tpcommit(0))
0999     {
1000         NDRX_LOG(log_error, "TESTERROR: commit must fail, as tmq does not know the tran");
1001         EXFAIL_OUT(ret);
1002     }
1003 
1004     if (TPEABORT!=tperrno)
1005     {
1006         NDRX_LOG(log_error, "TESTERROR: expected %d got %d", TPEABORT, tperrno);
1007         EXFAIL_OUT(ret);
1008     }
1009     
1010     /* check that number of messages are available... */
1011     NDRX_LOG(log_error, "Try to dequeue messages after tran restart...");
1012     
1013     if (EXSUCCEED!=tpbegin(9999, 0))
1014     {
1015         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1016         EXFAIL_OUT(ret);
1017     }
1018     
1019     /* all messages shall be removed even after tran restart */
1020     for (i=0; i<1; i++)
1021     {
1022         long len=0;
1023         char *buf;
1024         buf = tpalloc("CARRAY", "", 100);
1025         memset(&qc, 0, sizeof(qc));
1026 
1027         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
1028         {
1029             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, but all must be aborted!");
1030             EXFAIL_OUT(ret);
1031         }
1032         
1033         tpfree(buf);
1034     }
1035     
1036     if (EXSUCCEED!=tpcommit(0))
1037     {
1038         NDRX_LOG(log_error, "TESTERROR: failed to commit");
1039         EXFAIL_OUT(ret);
1040     }
1041     
1042 out:
1043     
1044     if (EXSUCCEED!=tpterm())
1045     {
1046         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1047         ret=EXFAIL;
1048         goto out;
1049     }
1050 
1051     return ret;
1052 }
1053 
1054 /**
1055  * Verify that we can handle tmsrv restarts in the middle of transaction
1056  * @param maxmsg max messages to be ok
1057  */
1058 exprivate int basic_tmsrvrestart(int maxmsg)
1059 {
1060     int ret = EXSUCCEED;
1061     TPQCTL qc;
1062     int i;
1063     
1064     NDRX_LOG(log_error, "case basic_tmsrvrestart");
1065     if (EXSUCCEED!=tpbegin(9999, 0))
1066     {
1067         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1068         EXFAIL_OUT(ret);
1069     }
1070     
1071     /* Initial test... */
1072     for (i=0; i<maxmsg; i++)
1073     {
1074         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1075         long len=10;
1076 
1077         testbuf_ref[0]=0;
1078         testbuf_ref[1]=1;
1079         testbuf_ref[2]=2;
1080         testbuf_ref[3]=3;
1081         testbuf_ref[4]=4;
1082         testbuf_ref[5]=5;
1083         testbuf_ref[6]=6;
1084         testbuf_ref[7]=7;
1085         testbuf_ref[8]=8;
1086         testbuf_ref[9]=9;
1087 
1088         /* alloc output buffer */
1089         if (NULL==testbuf_ref)
1090         {
1091             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1092                     tpstrerror(tperrno));
1093             EXFAIL_OUT(ret);
1094         }
1095 
1096         /* enqueue the data buffer */
1097         memset(&qc, 0, sizeof(qc));
1098         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1099                 len, 0))
1100         {
1101             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1102                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1103             EXFAIL_OUT(ret);
1104         }
1105 
1106         tpfree(testbuf_ref);
1107     }
1108     
1109     /* restart tmsrv.... 
1110      * it shall be able to commit OK
1111      */
1112     if (EXSUCCEED!=system("xadmin stop -s tmsrv"))
1113     {
1114         NDRX_LOG(log_error, "TESTERROR: failed to stop tmsrv");
1115         EXFAIL_OUT(ret);
1116     }
1117 
1118     /* restart tmqueue.... / reload msgs... */
1119     if (EXSUCCEED!=system("xadmin stop -s tmqueue"))
1120     {
1121         NDRX_LOG(log_error, "TESTERROR: failed to stop tmqueue");
1122         EXFAIL_OUT(ret);
1123     }
1124     
1125     if (EXSUCCEED!=system("xadmin start -s tmqueue"))
1126     {
1127         NDRX_LOG(log_error, "TESTERROR: failed to start tmqueue");
1128         EXFAIL_OUT(ret);
1129     }
1130 
1131     /* also.. here all messages shall be locked */
1132     for (i=0; i<1; i++)
1133     {
1134         long len=0;
1135         char *buf;
1136         buf = tpalloc("CARRAY", "", 100);
1137         memset(&qc, 0, sizeof(qc));
1138 
1139         /* This shall be updated so that we do not need to use TPNOABORT  */
1140         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
1141         {
1142             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, even already in progress!");
1143             EXFAIL_OUT(ret);
1144         }
1145 
1146         tpfree(buf);
1147     }
1148         
1149     /* dequeue must fail as we have already dequeued the messages */
1150     /* try just 1 msg. */
1151     
1152     if (EXSUCCEED!=system("xadmin start -s tmsrv"))
1153     {
1154         NDRX_LOG(log_error, "TESTERROR: failed to start tmsrv");
1155         EXFAIL_OUT(ret);
1156     }
1157 
1158     /* let tmsrv to load the logs in background... and perform abort... */
1159     sleep(20);
1160     
1161     /* also.. here all message shall be removed. */
1162     for (i=0; i<1; i++)
1163     {
1164         long len=0;
1165         char *buf;
1166         buf = tpalloc("CARRAY", "", 100);
1167         memset(&qc, 0, sizeof(qc));
1168 
1169         /* This shall be updated so that we do not need to use TPNOABORT  */
1170         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
1171         {
1172             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, even already in progress!");
1173             EXFAIL_OUT(ret);
1174         }
1175         
1176         tpfree(buf);
1177     }
1178     
1179     if (EXSUCCEED==tpcommit(0))
1180     {
1181         NDRX_LOG(log_error, "TESTERROR: failed to commit");
1182         EXFAIL_OUT(ret);
1183     }
1184     
1185 out:
1186     
1187     if (EXSUCCEED!=tpterm())
1188     {
1189         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1190         ret=EXFAIL;
1191         goto out;
1192     }
1193 
1194     return ret;
1195 }
1196 
1197 
1198 /**
1199  * Try commit, queue is shutdown
1200  * It shall abort with retries... boot tmqueue back, no messages shall be available
1201  * @param maxmsg max messages to be ok
1202  */
1203 exprivate int basic_commit_shut(int maxmsg)
1204 {
1205     int ret = EXSUCCEED;
1206     TPQCTL qc;
1207     int i;
1208     
1209     NDRX_LOG(log_error, "case basic_commit_shut");
1210     if (EXSUCCEED!=tpbegin(9999, 0))
1211     {
1212         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1213         EXFAIL_OUT(ret);
1214     }
1215     
1216     /* Initial test... */
1217     for (i=0; i<maxmsg; i++)
1218     {
1219         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1220         long len=10;
1221 
1222         testbuf_ref[0]=0;
1223         testbuf_ref[1]=1;
1224         testbuf_ref[2]=2;
1225         testbuf_ref[3]=3;
1226         testbuf_ref[4]=4;
1227         testbuf_ref[5]=5;
1228         testbuf_ref[6]=6;
1229         testbuf_ref[7]=7;
1230         testbuf_ref[8]=8;
1231         testbuf_ref[9]=9;
1232 
1233         /* alloc output buffer */
1234         if (NULL==testbuf_ref)
1235         {
1236             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1237                     tpstrerror(tperrno));
1238             EXFAIL_OUT(ret);
1239         }
1240 
1241         /* enqueue the data buffer */
1242         memset(&qc, 0, sizeof(qc));
1243         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1244                 len, 0))
1245         {
1246             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1247                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1248             EXFAIL_OUT(ret);
1249         }
1250 
1251         tpfree(testbuf_ref);
1252     }
1253     
1254     /* restart tmqueue.... 
1255      * it shall be able to commit OK
1256      */
1257     if (EXSUCCEED!=system("xadmin stop -s tmqueue"))
1258     {
1259         NDRX_LOG(log_error, "TESTERROR: failed to stop tmqueue");
1260         EXFAIL_OUT(ret);
1261     }
1262     
1263     if (EXSUCCEED==tpcommit(0))
1264     {
1265         NDRX_LOG(log_error, "TESTERROR: commit must fail");
1266         EXFAIL_OUT(ret);
1267     }
1268     
1269     /* we shall get abort error... */
1270     if (TPEABORT!=tperrno)
1271     {
1272         NDRX_LOG(log_error, "TESTERROR: invalid error, expected TPEABORT got %d",
1273                 tperrno);
1274         EXFAIL_OUT(ret);
1275     }
1276     
1277     if (EXSUCCEED!=system("xadmin start -s tmqueue"))
1278     {
1279         NDRX_LOG(log_error, "TESTERROR: failed to start tmqueue");
1280         EXFAIL_OUT(ret);
1281     }
1282     
1283     /* no messages shall be available, refactored after initial design. */
1284     for (i=0; i<1; i++)
1285     {
1286         long len=0;
1287         char *buf;
1288         buf = tpalloc("CARRAY", "", 100);
1289         memset(&qc, 0, sizeof(qc));
1290 
1291         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
1292         {
1293             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeue OK, must fail!");
1294             EXFAIL_OUT(ret);
1295         }
1296 
1297         /* check error no msg */
1298         if (tperrno!=TPEDIAGNOSTIC)
1299         {
1300             NDRX_LOG(log_error, "TESTERROR: expected %d got %d err!", TPEDIAGNOSTIC, tperrno);
1301             EXFAIL_OUT(ret);
1302         }
1303 
1304         if (qc.diagnostic!=QMENOMSG)
1305         {
1306             NDRX_LOG(log_error, "TESTERROR: expected %d got %d err (diag)!", QMENOMSG, qc.diagnostic);
1307             EXFAIL_OUT(ret);
1308         }
1309         
1310         tpfree(buf);
1311     }
1312     
1313 out:
1314     
1315     if (EXSUCCEED!=tpterm())
1316     {
1317         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1318         ret=EXFAIL;
1319         goto out;
1320     }
1321 
1322     return ret;
1323 }
1324 
1325 /**
1326  * Dequeue write error (disk full on command file) must be reported
1327  * @param maxmsg max messages to be ok
1328  */
1329 exprivate int basic_deqwriteerr(int maxmsg)
1330 {
1331     int ret = EXSUCCEED;
1332     TPQCTL qc;
1333     int i;
1334     
1335     NDRX_LOG(log_error, "case basic_deqwriteerr");
1336     if (EXSUCCEED!=tpbegin(9999, 0))
1337     {
1338         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1339         EXFAIL_OUT(ret);
1340     }
1341     
1342     /* Initial test... */
1343     for (i=0; i<maxmsg; i++)
1344     {
1345         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1346         long len=10;
1347 
1348         testbuf_ref[0]=0;
1349         testbuf_ref[1]=1;
1350         testbuf_ref[2]=2;
1351         testbuf_ref[3]=3;
1352         testbuf_ref[4]=4;
1353         testbuf_ref[5]=5;
1354         testbuf_ref[6]=6;
1355         testbuf_ref[7]=7;
1356         testbuf_ref[8]=8;
1357         testbuf_ref[9]=9;
1358 
1359         /* alloc output buffer */
1360         if (NULL==testbuf_ref)
1361         {
1362             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1363                     tpstrerror(tperrno));
1364             EXFAIL_OUT(ret);
1365         }
1366 
1367         /* enqueue the data buffer */
1368         memset(&qc, 0, sizeof(qc));
1369         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1370                 len, 0))
1371         {
1372             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1373                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1374             EXFAIL_OUT(ret);
1375         }
1376 
1377         tpfree(testbuf_ref);
1378     }
1379     
1380     if (EXSUCCEED!=tpcommit(0))
1381     {
1382         NDRX_LOG(log_error, "TESTERROR: commit failed: %s", tpstrerror(tperrno));
1383         EXFAIL_OUT(ret);
1384     }
1385     
1386     if (EXSUCCEED!=system("xadmin lcf qwriterr -A 1 -a"))
1387     {
1388         NDRX_LOG(log_error, "TESTERROR: xadmin lcf qwriterr -A 1 -a failed");
1389         EXFAIL_OUT(ret);
1390     }
1391     
1392     if (EXSUCCEED!=tpbegin(9999, 0))
1393     {
1394         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1395         EXFAIL_OUT(ret);
1396     }
1397     
1398     /* all messages must be available */
1399     for (i=0; i<1; i++)
1400     {
1401         long len=0;
1402         char *buf;
1403         buf = tpalloc("CARRAY", "", 100);
1404         memset(&qc, 0, sizeof(qc));
1405 
1406         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
1407         {
1408             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeue must fail!!");
1409             EXFAIL_OUT(ret);
1410         }
1411         
1412         if (tperrno!=TPEDIAGNOSTIC)
1413         {
1414             NDRX_LOG(log_error, "TESTERROR: TPEDIAGNOSTIC expected, got: %d!", tperrno);
1415             EXFAIL_OUT(ret);
1416         }
1417         
1418         if (QMEOS!=qc.diagnostic)
1419         {
1420             NDRX_LOG(log_error, "TESTERROR: QMEOS expected, got: %d!", qc.diagnostic);
1421             EXFAIL_OUT(ret);
1422         }   
1423         tpfree(buf);
1424 
1425     }
1426     
1427     /* terminate the transaction */
1428     tpabort(0);
1429     
1430     
1431     if (EXSUCCEED!=system("xadmin lcf qwriterr -A 0 -a"))
1432     {
1433         NDRX_LOG(log_error, "TESTERROR: xadmin lcf qwriterr -A 0 -a failed");
1434         EXFAIL_OUT(ret);
1435     }
1436     
1437     if (EXSUCCEED!=tpbegin(9999, 0))
1438     {
1439         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1440         EXFAIL_OUT(ret);
1441     }
1442     
1443     /* all messages must be available */
1444     for (i=0; i<maxmsg; i++)
1445     {
1446         long len=0;
1447         char *buf;
1448         buf = tpalloc("CARRAY", "", 100);
1449         memset(&qc, 0, sizeof(qc));
1450 
1451         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
1452         {
1453             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeue failed!: %s %ld", 
1454                     tpstrerror(tperrno), qc.diagnostic);
1455             EXFAIL_OUT(ret);
1456         }
1457         
1458         tpfree(buf);
1459         
1460     }
1461     
1462     if (EXSUCCEED!=tpcommit(0))
1463     {
1464         NDRX_LOG(log_error, "TESTERROR: failed to tpcommit");
1465         EXFAIL_OUT(ret);
1466     }
1467     
1468 out:
1469     
1470     if (EXSUCCEED!=tpterm())
1471     {
1472         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1473         ret=EXFAIL;
1474         goto out;
1475     }
1476 
1477     return ret;
1478 }
1479 
1480 /**
1481  * Load prepared messages (also tmq scans this twice, thus check that no problem)
1482  * @param maxmsg
1483  * @return 
1484  */
1485 exprivate int basic_loadprep(int maxmsg)
1486 {
1487     int ret = EXSUCCEED;
1488     TPQCTL qc;
1489     int i;
1490     
1491     NDRX_LOG(log_error, "case basic_loadprep");
1492     if (EXSUCCEED!=tpbegin(9999, 0))
1493     {
1494         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1495         EXFAIL_OUT(ret);
1496     }
1497     
1498     /* Initial test... */
1499     for (i=0; i<maxmsg; i++)
1500     {
1501         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1502         long len=10;
1503 
1504         testbuf_ref[0]=0;
1505         testbuf_ref[1]=1;
1506         testbuf_ref[2]=2;
1507         testbuf_ref[3]=3;
1508         testbuf_ref[4]=4;
1509         testbuf_ref[5]=5;
1510         testbuf_ref[6]=6;
1511         testbuf_ref[7]=7;
1512         testbuf_ref[8]=8;
1513         testbuf_ref[9]=9;
1514 
1515         /* alloc output buffer */
1516         if (NULL==testbuf_ref)
1517         {
1518             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1519                     tpstrerror(tperrno));
1520             EXFAIL_OUT(ret);
1521         }
1522 
1523         /* enqueue the data buffer */
1524         memset(&qc, 0, sizeof(qc));
1525         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1526                 len, 0))
1527         {
1528             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1529                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1530             EXFAIL_OUT(ret);
1531         }
1532 
1533         tpfree(testbuf_ref);
1534     }
1535     
1536     /* restart tmqueue.... 
1537      * it shall be able to commit OK
1538      */
1539     if (EXSUCCEED!=system("xadmin stop -s tmqueue"))
1540     {
1541         NDRX_LOG(log_error, "TESTERROR: failed to stop tmqueue");
1542         EXFAIL_OUT(ret);
1543     }
1544     
1545     /* move files from active to prep... 
1546      * we will not commit, but tmqueue startup shall not fail.
1547      */
1548     
1549     if (EXSUCCEED!=system("mv QSPACE1/active/* QSPACE1/prepared/"))
1550     {
1551         NDRX_LOG(log_error, "TESTERROR: failed to move transaction files...");
1552         EXFAIL_OUT(ret);
1553     }
1554     
1555     if (EXSUCCEED!=system("xadmin start -s tmqueue"))
1556     {
1557         NDRX_LOG(log_error, "TESTERROR: failed to start tmqueue");
1558         EXFAIL_OUT(ret);
1559     }
1560     
1561 out:
1562     
1563     if (EXSUCCEED!=tpterm())
1564     {
1565         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1566         ret=EXFAIL;
1567         goto out;
1568     }
1569 
1570     return ret;
1571 }
1572 
1573 /**
1574  * Check the case when disk is full, shall fail to enqueue
1575  * @param maxmsg max messages to be ok
1576  */
1577 exprivate int basic_diskfull(int maxmsg)
1578 {
1579     int ret = EXSUCCEED;
1580     TPQCTL qc;
1581     int i;
1582     
1583     NDRX_LOG(log_error, "case basic_diskfull");
1584     
1585     if (EXSUCCEED!=tpbegin(9999, 0))
1586     {
1587         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1588         EXFAIL_OUT(ret);
1589     }
1590     
1591     /* Set disk failure on for all processes...
1592      */
1593     if (EXSUCCEED!=system("xadmin lcf qwriterr -A 1 -a"))
1594     {
1595         NDRX_LOG(log_error, "TESTERROR: failed to enable write failure");
1596         EXFAIL_OUT(ret);
1597     }
1598     
1599     /* Initial test... */
1600     for (i=0; i<maxmsg; i++)
1601     {
1602         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1603         long len=10;
1604 
1605         testbuf_ref[0]=0;
1606         testbuf_ref[1]=1;
1607         testbuf_ref[2]=2;
1608         testbuf_ref[3]=3;
1609         testbuf_ref[4]=4;
1610         testbuf_ref[5]=5;
1611         testbuf_ref[6]=6;
1612         testbuf_ref[7]=7;
1613         testbuf_ref[8]=8;
1614         testbuf_ref[9]=9;
1615 
1616         /* alloc output buffer */
1617         if (NULL==testbuf_ref)
1618         {
1619             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1620                     tpstrerror(tperrno));
1621             EXFAIL_OUT(ret);
1622         }
1623 
1624         /* enqueue the data buffer */
1625         memset(&qc, 0, sizeof(qc));
1626         
1627         if (EXSUCCEED==tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1628                 len, 0))
1629         {
1630             NDRX_LOG(log_error, "TESTERROR: tpenqueue() must fail - disk full");
1631             EXFAIL_OUT(ret);
1632         }
1633         
1634         NDRX_LOG(log_error, "tpenqueue() failed %s diag: %d:%s", 
1635                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1636         
1637         if (TPEDIAGNOSTIC!=tperrno || QMEOS!=qc.diagnostic)
1638         {
1639             NDRX_LOG(log_error, "TESTERROR: expected tperrno==TPEDIAGNOSTIC got %d and qc.diagnostic==QMESYSTEM got %d",
1640                     tperrno, qc.diagnostic);
1641             EXFAIL_OUT(ret);
1642         }
1643         
1644         tpfree(testbuf_ref);
1645     }
1646     
1647     if (EXSUCCEED==tpcommit(0))
1648     {
1649         NDRX_LOG(log_error, "TESTERROR: it shall fail to commit, as transactions ar marked for abort!");
1650         EXFAIL_OUT(ret);
1651     }
1652     
1653     if (TPEABORT!=tperrno)
1654     {
1655         NDRX_LOG(log_error, "TESTERROR: Expected TPEABORT got %d", tperrno);
1656         EXFAIL_OUT(ret);
1657     }
1658     
1659     /* reset write error back to norm. */
1660     if (EXSUCCEED!=system("xadmin lcf qwriterr -A 0 -a"))
1661     {
1662         NDRX_LOG(log_error, "TESTERROR: failed to enable write failure");
1663         EXFAIL_OUT(ret);
1664     }
1665     
1666     /* restart tmqueue.... no message shall be available as no files
1667      * are saved due to write error
1668      */
1669     if (EXSUCCEED!=system("xadmin stop -s tmqueue; xadmin start -s tmqueue"))
1670     {
1671         NDRX_LOG(log_error, "TESTERROR: failed to stop tmqueue");
1672         EXFAIL_OUT(ret);
1673     }
1674     
1675     for (i=0; i<1; i++)
1676     {
1677         long len=0;
1678         char *buf;
1679         buf = tpalloc("CARRAY", "", 100);
1680         memset(&qc, 0, sizeof(qc));
1681 
1682         /* This shall be updated so that we do not need to use TPNOABORT  */
1683         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
1684         {
1685             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, even already in progress!");
1686             EXFAIL_OUT(ret);
1687         }
1688         
1689         tpfree(buf);
1690     }
1691     
1692 out:
1693     
1694     if (EXSUCCEED!=tpterm())
1695     {
1696         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1697         ret=EXFAIL;
1698         goto out;
1699     }
1700 
1701     return ret;
1702 }
1703 
1704 /**
1705  * tmsrv fails to log transaction - disk full  / abort
1706  * @param maxmsg
1707  * @return 
1708  */
1709 exprivate int basic_tmsrvdiskerr(int maxmsg)
1710 {
1711     int ret = EXSUCCEED;
1712     TPQCTL qc;
1713     int i;
1714     
1715     NDRX_LOG(log_error, "case basic_tmsrvdiskerr");
1716     
1717     if (EXSUCCEED!=tpbegin(20, 0))
1718     {
1719         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1720         EXFAIL_OUT(ret);
1721     }
1722     
1723     /* Initial test... */
1724     for (i=0; i<maxmsg; i++)
1725     {
1726         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1727         long len=10;
1728         
1729         if (i==1)
1730         {
1731             /* Set disk failure for tmsrv - now tmqueue is joined...
1732              */
1733             if (EXSUCCEED!=system("xadmin lcf twriterr -A 1 -a"))
1734             {
1735                 NDRX_LOG(log_error, "TESTERROR: failed to enable write failure");
1736                 EXFAIL_OUT(ret);
1737             }
1738         }
1739 
1740         testbuf_ref[0]=0;
1741         testbuf_ref[1]=1;
1742         testbuf_ref[2]=2;
1743         testbuf_ref[3]=3;
1744         testbuf_ref[4]=4;
1745         testbuf_ref[5]=5;
1746         testbuf_ref[6]=6;
1747         testbuf_ref[7]=7;
1748         testbuf_ref[8]=8;
1749         testbuf_ref[9]=9;
1750 
1751         /* alloc output buffer */
1752         if (NULL==testbuf_ref)
1753         {
1754             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1755                     tpstrerror(tperrno));
1756             EXFAIL_OUT(ret);
1757         }
1758         
1759         /* enqueue the data buffer */
1760         memset(&qc, 0, sizeof(qc));
1761         
1762         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1763                 len, 0))
1764         {
1765             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1766                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1767             EXFAIL_OUT(ret);
1768         }
1769 
1770         tpfree(testbuf_ref);
1771     }
1772     
1773     /* commit shall fail, as failed to log stuff
1774      * abort can complete with out disk
1775      */
1776     if (EXSUCCEED==tpcommit(0))
1777     {
1778         NDRX_LOG(log_error, "TESTERROR: it shall fail to commit, as transactions ar marked for abort!");
1779         EXFAIL_OUT(ret);
1780     }
1781     
1782     /* stuff shall be rolled back... */
1783     /* maybe better it would be to have TPEABORT, but current if storage is not working
1784      * we will give TPEOS error
1785      */
1786     if (TPEABORT!=tperrno)
1787     {
1788         NDRX_LOG(log_error, "TESTERROR: Expected TPEABORT got %d", tperrno);
1789         EXFAIL_OUT(ret);
1790     }
1791     
1792     /* no messages in queue, as rolled back.. */
1793     for (i=0; i<1; i++)
1794     {
1795         long len=0;
1796         char *buf;
1797         buf = tpalloc("CARRAY", "", 100);
1798         memset(&qc, 0, sizeof(qc));
1799 
1800         /* This shall be updated so that we do not need to use TPNOABORT  */
1801         if (EXSUCCEED==tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, TPNOABORT))
1802         {
1803             NDRX_LOG(log_error, "TESTERROR: TEST1 dequeued, even already in progress!");
1804             EXFAIL_OUT(ret);
1805         }
1806         
1807         tpfree(buf);
1808     }
1809 
1810     
1811 out:
1812     
1813     /* reset write error back to norm. */
1814     if (EXSUCCEED!=system("xadmin lcf twriterr -A 0 -a"))
1815     {
1816         NDRX_LOG(log_error, "TESTERROR: failed to enable write failure");
1817         EXFAIL_OUT(ret);
1818     }
1819 
1820     if (EXSUCCEED!=tpterm())
1821     {
1822         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1823         ret=EXFAIL;
1824         goto out;
1825     }
1826 
1827     return ret;
1828 }
1829 
1830 /**
1831  * Skip bad messages on the disk
1832  * @param maxmsg max messages to be ok
1833  */
1834 exprivate int basic_badmsg(int maxmsg)
1835 {
1836     int ret = EXSUCCEED;
1837     TPQCTL qc;
1838     int i;
1839     
1840     NDRX_LOG(log_error, "case basic_badmsg");
1841     if (EXSUCCEED!=tpbegin(9999, 0))
1842     {
1843         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1844         EXFAIL_OUT(ret);
1845     }
1846     
1847     /* Initial test... */
1848     for (i=0; i<maxmsg; i++)
1849     {
1850         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1851         long len=10;
1852 
1853         testbuf_ref[0]=0;
1854         testbuf_ref[1]=1;
1855         testbuf_ref[2]=2;
1856         testbuf_ref[3]=3;
1857         testbuf_ref[4]=4;
1858         testbuf_ref[5]=5;
1859         testbuf_ref[6]=6;
1860         testbuf_ref[7]=7;
1861         testbuf_ref[8]=8;
1862         testbuf_ref[9]=9;
1863 
1864         /* alloc output buffer */
1865         if (NULL==testbuf_ref)
1866         {
1867             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1868                     tpstrerror(tperrno));
1869             EXFAIL_OUT(ret);
1870         }
1871 
1872         /* enqueue the data buffer */
1873         memset(&qc, 0, sizeof(qc));
1874         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1875                 len, 0))
1876         {
1877             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1878                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1879             EXFAIL_OUT(ret);
1880         }
1881 
1882         tpfree(testbuf_ref);
1883     }
1884     
1885     /* create a bad file */
1886     if (EXSUCCEED!=system("touch QSPACE1/active/some_bad_message_file"))
1887     {
1888         NDRX_LOG(log_error, "TESTERROR: failed to create bad message file...");
1889         EXFAIL_OUT(ret);
1890     }
1891     
1892     if (EXSUCCEED!=tpcommit(0))
1893     {
1894         NDRX_LOG(log_error, "TESTERROR: failed to commit got: %s", tpstrerror(tperrno));
1895         EXFAIL_OUT(ret);
1896     }
1897     
1898     if (EXSUCCEED!=system("xadmin sreload -s tmqueue"))
1899     {
1900         NDRX_LOG(log_error, "TESTERROR: failed to retart tmqueue");
1901         EXFAIL_OUT(ret);
1902     }
1903     
1904     /* all messages must be available - bad file ignored...*/
1905     for (i=0; i<maxmsg; i++)
1906     {
1907         long len=0;
1908         char *buf;
1909         buf = tpalloc("CARRAY", "", 100);
1910         memset(&qc, 0, sizeof(qc));
1911 
1912         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
1913         {
1914             NDRX_LOG(log_error, "TESTERROR: TEST1 failed dequeue!");
1915             EXFAIL_OUT(ret);
1916         }
1917         tpfree(buf);
1918     }
1919     
1920 out:
1921     
1922     if (EXSUCCEED!=tpterm())
1923     {
1924         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
1925         ret=EXFAIL;
1926         goto out;
1927     }
1928 
1929     /* create a bad file */
1930     if (system("rm QSPACE1/active/some_bad_message_file"))
1931     {
1932         /* avoid warning... */
1933     }
1934     
1935     return ret;
1936 }
1937 
1938 
1939 /**
1940  * Simulate commit crash & recovery
1941  * We enqueue data.
1942  * Commit fails (change stage to committing, thus we perform automatic
1943  * rollback)
1944  * @param maxmsg max messages to be ok
1945  */
1946 exprivate int basic_commit_crash(int maxmsg)
1947 {
1948     int ret = EXSUCCEED;
1949     TPQCTL qc;
1950     int i;
1951     
1952     NDRX_LOG(log_error, "case basic_commit_crash");
1953     if (EXSUCCEED!=tpbegin(9999, 0))
1954     {
1955         NDRX_LOG(log_error, "TESTERROR: failed to begin");
1956         EXFAIL_OUT(ret);
1957     }
1958     
1959     /* Initial test... */
1960     for (i=0; i<maxmsg; i++)
1961     {
1962         char *testbuf_ref = tpalloc("CARRAY", "", 10);
1963         long len=10;
1964 
1965         testbuf_ref[0]=0;
1966         testbuf_ref[1]=1;
1967         testbuf_ref[2]=2;
1968         testbuf_ref[3]=3;
1969         testbuf_ref[4]=4;
1970         testbuf_ref[5]=5;
1971         testbuf_ref[6]=6;
1972         testbuf_ref[7]=7;
1973         testbuf_ref[8]=8;
1974         testbuf_ref[9]=9;
1975 
1976         /* alloc output buffer */
1977         if (NULL==testbuf_ref)
1978         {
1979             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
1980                     tpstrerror(tperrno));
1981             EXFAIL_OUT(ret);
1982         }
1983 
1984         /* enqueue the data buffer */
1985         memset(&qc, 0, sizeof(qc));
1986         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
1987                 len, 0))
1988         {
1989             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
1990                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
1991             EXFAIL_OUT(ret);
1992         }
1993 
1994         tpfree(testbuf_ref);
1995     }
1996     
1997     if (EXSUCCEED!=tpcommit(0))
1998     {
1999         NDRX_LOG(log_error, "TESTERROR: commit failed: %s", tpstrerror(tperrno));
2000         EXFAIL_OUT(ret);
2001     }
2002     
2003     /* start to dequeue... */
2004     if (EXSUCCEED!=tpbegin(9999, 0))
2005     {
2006         NDRX_LOG(log_error, "TESTERROR: failed to begin");
2007         EXFAIL_OUT(ret);
2008     }
2009     
2010     /* all messages must be available */
2011     for (i=0; i<maxmsg; i++)
2012     {
2013         long len=0;
2014         char *buf;
2015         buf = tpalloc("CARRAY", "", 100);
2016         memset(&qc, 0, sizeof(qc));
2017 
2018         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
2019         {
2020             NDRX_LOG(log_error, "TESTERROR: TEST1 failed dequeue!");
2021             EXFAIL_OUT(ret);
2022         }
2023         
2024         tpfree(buf);
2025     }
2026     
2027     /* 
2028      * Set crash point
2029      */
2030     if (EXSUCCEED!=system("xadmin lcf tcrash -A 50 -a"))
2031     {
2032         NDRX_LOG(log_error, "TESTERROR: failed to enable crash");
2033         EXFAIL_OUT(ret);
2034     }
2035     
2036     /* set timeout time  
2037      * commit will fail...
2038      * and all records will be rolled back (assuming in 30 sec)
2039      */
2040     tptoutset(30);
2041     
2042     if (EXSUCCEED==tpcommit(0))
2043     {
2044         NDRX_LOG(log_error, "TESTERROR: commit must fail");
2045         EXFAIL_OUT(ret);
2046     }
2047     
2048     if (tperrno!=TPETIME)
2049     {
2050         NDRX_LOG(log_error, "TESTERROR: expected TPETIME got %d: %s",
2051             tperrno, tpstrerror(tperrno));
2052         EXFAIL_OUT(ret);
2053     }
2054     
2055     /* set timeout back... */
2056     tptoutset(90);
2057     
2058     if (EXSUCCEED!=system("xadmin lcf tcrash -A 0 -a"))
2059     {
2060         NDRX_LOG(log_error, "TESTERROR: failed to disable crash");
2061         EXFAIL_OUT(ret);
2062     }
2063     
2064     /* let tmsrv to flush all stuff.... */
2065     sleep(20);
2066     /* all messages must be available (rolled back after crash recovery) */
2067     for (i=0; i<maxmsg; i++)
2068     {
2069         long len=0;
2070         char *buf;
2071         buf = tpalloc("CARRAY", "", 100);
2072         memset(&qc, 0, sizeof(qc));
2073 
2074         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
2075         {
2076             NDRX_LOG(log_error, "TESTERROR: TEST1 failed dequeue!");
2077             EXFAIL_OUT(ret);
2078         }
2079         
2080         tpfree(buf);
2081     }
2082     
2083 out:
2084     
2085     if (EXSUCCEED!=tpterm())
2086     {
2087         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
2088         ret=EXFAIL;
2089         goto out;
2090     }
2091 
2092     return ret;
2093 }
2094 
2095 
2096 /**
2097  * Test normal enqueue/dequeue operation
2098  * @param maxmsg max messages to be ok
2099  */
2100 exprivate int basic_enqdeq(int maxmsg)
2101 {
2102     int ret = EXSUCCEED;
2103     TPQCTL qc;
2104     int i;
2105     
2106     NDRX_LOG(log_error, "case enqdeq");
2107     if (EXSUCCEED!=tpbegin(9999, 0))
2108     {
2109         NDRX_LOG(log_error, "TESTERROR: failed to begin");
2110         EXFAIL_OUT(ret);
2111     }
2112     
2113     /* Initial test... */
2114     for (i=0; i<maxmsg; i++)
2115     {
2116         char *testbuf_ref = tpalloc("CARRAY", "", 10);
2117         long len=10;
2118 
2119         testbuf_ref[0]=i%128;
2120         testbuf_ref[1]=1;
2121         testbuf_ref[2]=2;
2122         testbuf_ref[3]=3;
2123         testbuf_ref[4]=4;
2124         testbuf_ref[5]=5;
2125         testbuf_ref[6]=6;
2126         testbuf_ref[7]=7;
2127         testbuf_ref[8]=8;
2128         testbuf_ref[9]=9;
2129 
2130         /* alloc output buffer */
2131         if (NULL==testbuf_ref)
2132         {
2133             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
2134                     tpstrerror(tperrno));
2135             EXFAIL_OUT(ret);
2136         }
2137 
2138         /* enqueue the data buffer */
2139         memset(&qc, 0, sizeof(qc));
2140         if (EXSUCCEED!=tpenqueue("MYSPACE", "TEST1", &qc, testbuf_ref, 
2141                 len, 0))
2142         {
2143             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
2144                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
2145             EXFAIL_OUT(ret);
2146         }
2147 
2148         tpfree(testbuf_ref);
2149     }
2150     
2151     if (EXSUCCEED!=tpcommit(0))
2152     {
2153         NDRX_LOG(log_error, "TESTERROR: failed to commit");
2154         EXFAIL_OUT(ret);
2155     }
2156     
2157     /* check that number of messages are available... */
2158     NDRX_LOG(log_error, "About to dequeue %d messages", maxmsg);
2159     
2160     if (EXSUCCEED!=tpbegin(9999, 0))
2161     {
2162         NDRX_LOG(log_error, "TESTERROR: failed to begin");
2163         EXFAIL_OUT(ret);
2164     }
2165     
2166     /* all messages must be available */
2167     for (i=0; i<maxmsg; i++)
2168     {
2169         long len=0;
2170         char *buf;
2171         buf = tpalloc("CARRAY", "", 100);
2172         memset(&qc, 0, sizeof(qc));
2173 
2174         if (EXSUCCEED!=tpdequeue("MYSPACE", "TEST1", &qc, (char **)&buf, &len, 0))
2175         {
2176             NDRX_LOG(log_error, "TESTERROR: TEST1 failed dequeue!");
2177             EXFAIL_OUT(ret);
2178         }
2179         
2180         /* verify the message number is it correct? 
2181          * as we have fifo queue...
2182          */
2183         if (buf[0]!=i%128)
2184         {
2185             NDRX_LOG(log_error, "TESTERROR: Expected %d at %i, got %d",
2186                     (int)i%128, i, (int)buf[0]);
2187             EXFAIL_OUT(ret);
2188         }
2189         
2190         tpfree(buf);
2191     }
2192     
2193     if (EXSUCCEED!=tpcommit(0))
2194     {
2195         NDRX_LOG(log_error, "TESTERROR: failed to commit");
2196         EXFAIL_OUT(ret);
2197     }
2198     
2199 out:
2200     
2201     if (EXSUCCEED!=tpterm())
2202     {
2203         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
2204         ret=EXFAIL;
2205         goto out;
2206     }
2207 
2208     return ret;
2209 }
2210 
2211 /**
2212  * Ensure that empty commits at Q can be restared, if they are prepared.
2213  * Rollback actually will happen, as tcrash would corrupt the "committing" stage entry
2214  * however, after that, there shall be no prepared transactions on the disk
2215  * in Q space (i.e. dummy record is rolled back).
2216  */
2217 exprivate int basic_emptyq_commit_crash(int maxmsg)
2218 {
2219 
2220     int ret = EXSUCCEED;
2221     TPQCTL qc;
2222     int i;
2223 
2224     /* configure short timeout ... */
2225     if (EXSUCCEED!=tptoutset(1))
2226     {
2227         NDRX_LOG(log_error, "TESTERROR: tptoutset failed: %s",
2228             tpstrerror(tperrno));
2229         EXFAIL_OUT(ret);
2230     }
2231 
2232     for (i=0; i<maxmsg; i++)
2233     {
2234 
2235         if (EXSUCCEED!=tpbegin(60, 0))
2236         {
2237             NDRX_LOG(log_error, "TESTERROR: failed to begin");
2238             EXFAIL_OUT(ret);
2239         }
2240 
2241         /*
2242          * commit will fail. till the respawn happens, then tmqueue shall be reloaded
2243          * external script will verify that no prepared messages are on the disk after the test.
2244          */
2245         if (EXSUCCEED!=system("xadmin lcf tcrash -A 50 -a"))
2246         {
2247             NDRX_LOG(log_error, "TESTERROR: failed to enable commit crash");
2248             EXFAIL_OUT(ret);
2249         }
2250 
2251         if (EXSUCCEED==tpcommit(0))
2252         {
2253             NDRX_LOG(log_error, "TESTERROR: tpcommit must fail");
2254             EXFAIL_OUT(ret);
2255         }
2256 
2257         if (TPETIME!=tperrno)
2258         {
2259             NDRX_LOG(log_error, "TESTERROR: tpcommit expected TPETIME got %d", tperrno);
2260             EXFAIL_OUT(ret);
2261         }
2262 
2263         /* this shall cleanup empty commits... */
2264         if (EXSUCCEED!=system("xadmin sreload tmqueue"))
2265         {
2266             NDRX_LOG(log_error, "TESTERROR: failed to stop tmqueue");
2267             EXFAIL_OUT(ret);
2268         }
2269 
2270         /* wait for respawn ... */
2271         sleep(15);
2272 
2273     }
2274 
2275     if (EXSUCCEED!=system("xadmin lcf tcrash -A 0 -a"))
2276     {
2277         NDRX_LOG(log_error, "TESTERROR: failed to disable commit crash");
2278         EXFAIL_OUT(ret);
2279     }
2280 
2281 out:
2282     tpterm();
2283     return ret;
2284 }
2285 
2286 /**
2287  * Transaction forward crashes. Also ensure that tmsrv instance does not boot
2288  * back. But we start normal tmsrv instances, to ensure that errorq op can
2289  * be completed.
2290  * @param maxmsg max messages to be ok
2291  */
2292 exprivate int basic_fwdcrash(int maxmsg)
2293 {
2294     int ret = EXSUCCEED;
2295     TPQCTL qc;
2296     int i;
2297     
2298     NDRX_LOG(log_error, "case basic_commit_crash");
2299     if (EXSUCCEED!=tpbegin(9999, 0))
2300     {
2301         NDRX_LOG(log_error, "TESTERROR: failed to begin");
2302         EXFAIL_OUT(ret);
2303     }
2304     
2305     /* Initial test... */
2306     for (i=0; i<maxmsg; i++)
2307     {
2308         char *testbuf_ref = tpalloc("CARRAY", "", 10);
2309         long len=10;
2310 
2311         testbuf_ref[0]=0;
2312         testbuf_ref[1]=1;
2313         testbuf_ref[2]=2;
2314         testbuf_ref[3]=3;
2315         testbuf_ref[4]=4;
2316         testbuf_ref[5]=5;
2317         testbuf_ref[6]=6;
2318         testbuf_ref[7]=7;
2319         testbuf_ref[8]=8;
2320         testbuf_ref[9]=9;
2321 
2322         /* alloc output buffer */
2323         if (NULL==testbuf_ref)
2324         {
2325             NDRX_LOG(log_error, "TESTERROR: tpalloc() failed %s", 
2326                     tpstrerror(tperrno));
2327             EXFAIL_OUT(ret);
2328         }
2329 
2330         /* enqueue the data buffer */
2331         memset(&qc, 0, sizeof(qc));
2332         if (EXSUCCEED!=tpenqueue("MYSPACE", "CRASHQ", &qc, testbuf_ref, 
2333                 len, 0))
2334         {
2335             NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
2336                     tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
2337             EXFAIL_OUT(ret);
2338         }
2339 
2340         tpfree(testbuf_ref);
2341     }
2342     
2343     if (EXSUCCEED!=tpcommit(0))
2344     {
2345         NDRX_LOG(log_error, "TESTERROR: commit failed: %s", tpstrerror(tperrno));
2346         EXFAIL_OUT(ret);
2347     }
2348     
2349     /* prepare crash setup */
2350     if (EXSUCCEED!=system("xadmin stop -i 50"))
2351     {
2352         NDRX_LOG(log_error, "TESTERROR: failed to stop 50 inst");
2353         EXFAIL_OUT(ret);
2354     }
2355 
2356     if (EXSUCCEED!=system("xadmin start -i 60"))
2357     {
2358         NDRX_LOG(log_error, "TESTERROR: failed to start 60 inst");
2359         EXFAIL_OUT(ret);
2360     }
2361 
2362     /* prepare will crash the tmsrv. thus no msgs will stay prepard & 
2363      * and tout dmn will rollback
2364      */
2365     if (EXSUCCEED!=system("xadmin lcf tcrash -A 40 -a"))
2366     {
2367         NDRX_LOG(log_error, "TESTERROR: failed to enable commit crash");
2368         EXFAIL_OUT(ret);
2369     }
2370 
2371     /* enable forward */
2372     if (EXSUCCEED!=system("xadmin mqch -n1 -i 100 -qCRASHQ,autoq=y"))
2373     {
2374         NDRX_LOG(log_error, "TESTERROR: failed enable forward");
2375         EXFAIL_OUT(ret);
2376     }
2377 
2378     /* Let some messages to stuck...*/ 
2379     sleep(15);
2380 
2381     if (EXSUCCEED!=system("xadmin lcf tcrash -A 0 -a"))
2382     {
2383         NDRX_LOG(log_error, "TESTERROR: failed to disable commit crash");
2384         EXFAIL_OUT(ret);
2385     }
2386 
2387     if (EXSUCCEED!=system("xadmin stop -i 60"))
2388     {
2389         NDRX_LOG(log_error, "TESTERROR: failed to stop 60 inst");
2390         EXFAIL_OUT(ret);
2391     }
2392 
2393     if (EXSUCCEED!=system("xadmin start -i 50"))
2394     {
2395         NDRX_LOG(log_error, "TESTERROR: failed to start 50 inst");
2396         EXFAIL_OUT(ret);
2397     }
2398 
2399     /* let messages to unlock & forward to CRASHERR
2400      * 90 - is tout.. thus have some more time for stucked msgs...
2401      */
2402     sleep(120);
2403 
2404     /* all messages must be available */
2405     for (i=0; i<maxmsg; i++)
2406     {
2407         long len=0;
2408         char *buf;
2409         buf = tpalloc("CARRAY", "", 100);
2410         memset(&qc, 0, sizeof(qc));
2411 
2412         if (EXSUCCEED!=tpdequeue("MYSPACE", "CRASHERR", &qc, (char **)&buf, &len, 0))
2413         {
2414             NDRX_LOG(log_error, "TESTERROR: CRASHERR failed dequeue!");
2415             EXFAIL_OUT(ret);
2416         }
2417         
2418         tpfree(buf);
2419     }
2420 
2421     /* no messages shall be available */
2422     for (i=0; i<1; i++)
2423     {
2424         long len=0;
2425         char *buf;
2426         buf = tpalloc("CARRAY", "", 100);
2427         memset(&qc, 0, sizeof(qc));
2428 
2429         if (EXSUCCEED==tpdequeue("MYSPACE", "CRASHERR", &qc, (char **)&buf, &len, 0))
2430         {
2431             NDRX_LOG(log_error, "TESTERROR: CRASHERR must not dequeue!");
2432             EXFAIL_OUT(ret);
2433         }
2434 
2435         /* check error no msg */
2436         if (tperrno!=TPEDIAGNOSTIC)
2437         {
2438             NDRX_LOG(log_error, "TESTERROR: expected %d got %d err!", TPEDIAGNOSTIC, tperrno);
2439             EXFAIL_OUT(ret);
2440         }
2441 
2442         if (qc.diagnostic!=QMENOMSG)
2443         {
2444             NDRX_LOG(log_error, "TESTERROR: expected %d got %d err (diag)!", QMENOMSG, qc.diagnostic);
2445             EXFAIL_OUT(ret);
2446         }
2447 
2448         tpfree(buf);
2449     }
2450 
2451 out:
2452     
2453     if (EXSUCCEED!=tpterm())
2454     {
2455         NDRX_LOG(log_error, "tpterm failed with: %s", tpstrerror(tperrno));
2456         ret=EXFAIL;
2457         goto out;
2458     }
2459 
2460     return ret;
2461 }
2462 
2463 /* vim: set ts=4 sw=4 et smartindent: */