Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Enqeueu & 
0003  *
0004  * @file atmiclt104.c
0005  */
0006 /* -----------------------------------------------------------------------------
0007  * Enduro/X Middleware Platform for Distributed Transaction Processing
0008  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0009  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0010  * This software is released under one of the following licenses:
0011  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0012  * See LICENSE file for full text.
0013  * -----------------------------------------------------------------------------
0014  * AGPL license:
0015  * 
0016  * This program is free software; you can redistribute it and/or modify it under
0017  * the terms of the GNU Affero General Public License, version 3 as published
0018  * by the Free Software Foundation;
0019  *
0020  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0021  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0022  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0023  * for more details.
0024  *
0025  * You should have received a copy of the GNU Affero General Public License along 
0026  * with this program; if not, write to the Free Software Foundation, Inc., 
0027  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0028  *
0029  * -----------------------------------------------------------------------------
0030  * A commercial use license is available from Mavimax, Ltd
0031  * contact@mavimax.com
0032  * -----------------------------------------------------------------------------
0033  */
0034 #include <string.h>
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <memory.h>
0038 #include <math.h>
0039 
0040 #include <exassert.h>
0041 #include <atmi.h>
0042 #include <ubf.h>
0043 #include <ndebug.h>
0044 #include <test.fd.h>
0045 #include <ndrstandard.h>
0046 #include <nstopwatch.h>
0047 #include <fcntl.h>
0048 #include <unistd.h>
0049 #include <nstdutil.h>
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 /**
0059  * Do the test call to the server
0060  */
0061 int main(int argc, char** argv)
0062 {
0063     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0064     long rsplen;
0065     int i;
0066     int ret=EXSUCCEED;
0067     TPQCTL qctl;
0068     short num;
0069     long len;
0070 
0071     if (argc < 3 || 0==strcmp(argv[1], "deq") && argc < 4)
0072     {
0073         fprintf(stderr, "Usage: %s {enq <msg_no> | deq <msg_tot> <broken_txn_flag>}\n", argv[0]);
0074         exit(EXFAIL);
0075     }
0076 
0077     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpopen(), "Failed to topen()");
0078     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpbegin(30, 0), "Failed to tpbegin()");
0079 
0080     num = atoi(argv[2]);
0081 
0082     if (0==strcmp(argv[1], "enq"))
0083     {
0084         if (EXSUCCEED!=Bchg(p_ub, T_SHORT_FLD, 0, (char *)&num, 0L))
0085         {
0086             fprintf(stderr, "Failed to set T_SHORT_FLD\n");
0087             EXFAIL_OUT(ret);
0088         }
0089 
0090         /* enqueue buffer */
0091         memset(&qctl, 0, sizeof(qctl));
0092 
0093         if (EXSUCCEED!=tpenqueue("TESTSP", "Q1", &qctl, (char *)p_ub, 0, 0))
0094         {
0095             NDRX_LOG(log_error, "TESTERROR: tpenqueue() to `Q1' failed %s diag: %d:%s",
0096                             tpstrerror(tperrno), qctl.diagnostic, qctl.diagmsg);
0097                     EXFAIL_OUT(ret);
0098             EXFAIL_OUT(ret);
0099         }
0100 
0101         NDRX_ASSERT_TP_OUT(EXSUCCEED==tpcommit(0), "Failed to tpcommit()");
0102 
0103     }
0104     else if (0==strcmp(argv[1], "deq"))
0105     {
0106         /* read number of message and match slots, check for duplicates */
0107         short messages[num];
0108         short val;
0109         int j;
0110         memset(messages, 0, sizeof(messages));
0111 
0112         /* read from q1 or q2, if no msg present, then generate error */
0113         memset(&qctl, 0, sizeof(qctl));
0114         while (EXSUCCEED==tpdequeue("TESTSP", "Q1", &qctl, (char **)&p_ub, &len, 0))
0115         {
0116             if (EXSUCCEED!=Bget(p_ub, T_SHORT_FLD, 0, (char *)&val, NULL))
0117             {
0118                 NDRX_LOG(log_error, "TESTERROR: Failed to get T_SHORT_FLD: %s", Bstrerror(Berror));
0119                 EXFAIL_OUT(ret);
0120             }
0121 
0122             if (val<0 || val>=num)
0123             {
0124                 NDRX_LOG(log_error, "TESTERROR: Message %d out of range in Q1", val);
0125                 EXFAIL_OUT(ret);
0126             }
0127 
0128             if (messages[val]!=0)
0129             {
0130                 NDRX_LOG(log_error, "TESTERROR: Duplicate message %d in Q1", val);
0131                 EXFAIL_OUT(ret);
0132             }
0133             NDRX_LOG(log_error, "GOT val=%hd", val);
0134             messages[val] = 1;
0135             memset(&qctl, 0, sizeof(qctl));
0136         }
0137 
0138         if (TPEDIAGNOSTIC!=tperrno)
0139         {
0140             NDRX_LOG(log_error, "TESTRROR: Expected TPEDIAGNOSTIC, got %s", tpstrerror(tperrno));
0141             EXFAIL_OUT(ret);
0142         }
0143 
0144         NDRX_LOG(log_error, "tpdequeue failed %s diag: %d:%s",
0145                     tpstrerror(tperrno), qctl.diagnostic, qctl.diagmsg);
0146 
0147         for (j=0; j<num; j++)
0148         {
0149             if (messages[j]!=1)
0150             {
0151                 NDRX_LOG(log_error, "Message %d not found in Q", j);
0152                 NDRX_ASSERT_TP_OUT(EXSUCCEED==tpabort(0), "Failed to tpcommit()");
0153                 EXFAIL_OUT(ret);
0154             }
0155         }
0156         
0157         /* commit if needed... */
0158         if ('Y'==argv[3][0])
0159         {
0160             /* jsut terminate, with transaction in progress... */
0161             exit(0);
0162         }
0163 
0164         NDRX_ASSERT_TP_OUT(EXSUCCEED==tpcommit(0), "Failed to tpcommit()");
0165     }
0166     else
0167     {
0168         NDRX_LOG(log_error, "Unknown command %s", argv[1]);
0169         exit(EXFAIL);
0170     }
0171     
0172 out:
0173     tpterm();
0174     fprintf(stderr, "Exit with %d\n", ret);
0175 
0176     return ret;
0177 }
0178 
0179 /* vim: set ts=4 sw=4 et smartindent: */