Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Dispatch thread xa on cluster, null switch, buildtools - client
0003  *
0004  * @file atmiclt76.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 <atmi.h>
0041 #include <ubf.h>
0042 #include <ndebug.h>
0043 #include <test.fd.h>
0044 #include <ndrstandard.h>
0045 #include <nstopwatch.h>
0046 #include <fcntl.h>
0047 #include <unistd.h>
0048 #include <nstdutil.h>
0049 #include <exassert.h>
0050 #include "test76.h"
0051 /*---------------------------Externs------------------------------------*/
0052 /*---------------------------Macros-------------------------------------*/
0053 /*---------------------------Enums--------------------------------------*/
0054 /*---------------------------Typedefs-----------------------------------*/
0055 /*---------------------------Globals------------------------------------*/
0056 /*---------------------------Statics------------------------------------*/
0057 int M_term = EXFALSE;
0058 volatile int M_has_failure = EXFALSE;
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /* this function is run by the second thread */
0062 void *run_case(void *x_void_ptr)
0063 {
0064     int ret = EXSUCCEED;
0065     long rsplen;
0066     char *svcnm = (char *)x_void_ptr;
0067     int tx_mode = EXFALSE;
0068     int fail_mode = EXFALSE;
0069     char res1[64];
0070     char res2[64];
0071     if (0==strncmp(svcnm, "TX", 2))
0072     {
0073         tx_mode=EXTRUE;
0074     }
0075     
0076     if (0==strcmp(svcnm, "TXFAIL"))
0077     {
0078         fail_mode=EXTRUE;
0079     }
0080     
0081     if (tx_mode)
0082     {
0083         NDRX_ASSERT_TP_OUT((EXSUCCEED==tpopen()), "Failed to tpopen()");
0084     }
0085     
0086     while (!M_term)
0087     {
0088         UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 1024);
0089         
0090         /* start new tran */
0091         if (tx_mode)
0092         {
0093             NDRX_ASSERT_TP_OUT((EXSUCCEED==tpbegin(60, 0)), "Tran shall start OK");
0094         }
0095         
0096         if (EXFAIL == tpcall(svcnm, (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0097         {
0098             NDRX_LOG(log_error, "%s failed: %s", svcnm, tpstrerror(tperrno));
0099             ret=EXFAIL;
0100             goto out;
0101         }
0102         
0103         /* check the service chain */
0104         NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bget(p_ub, T_STRING_FLD, 0, res1, NULL)), 
0105                 "Res 1 missing");
0106         NDRX_ASSERT_UBF_OUT((EXSUCCEED==Bget(p_ub, T_STRING_FLD, 1, res2, NULL)), 
0107                 "Res 2 missing");
0108         
0109         /* check the results */
0110         
0111         if (!tx_mode)
0112         {
0113             NDRX_ASSERT_VAL_OUT((0==strcmp(res1, "NOTX")), "Got [%s]", res1);
0114             NDRX_ASSERT_VAL_OUT((0==strcmp(res2, "NOTX")), "Got [%s]", res2);
0115         }
0116         else
0117         {
0118             NDRX_ASSERT_VAL_OUT((0==strcmp(res1, "INTX")), "Got [%s]", res1);
0119             NDRX_ASSERT_VAL_OUT((0==strcmp(res2, "INTX")), "Got [%s]", res2);
0120         }
0121         
0122         /* try to commit... */
0123         if (tx_mode)
0124         {
0125             if (fail_mode)
0126             {
0127                 /* commit shall fail */
0128                 NDRX_ASSERT_TP_OUT((EXFAIL==tpcommit(0)), "Commit shall fail");
0129                 NDRX_ASSERT_TP_OUT((TPEABORT==tperrno), "Expected abort");
0130             }
0131             else
0132             {
0133                 /* commit shall OK */
0134                 NDRX_ASSERT_TP_OUT((EXSUCCEED==tpcommit(0)), "Commit shall be OK");
0135             }
0136         }
0137         
0138         tpfree((char *)p_ub);
0139         
0140     }
0141     
0142 out:
0143     
0144     if (tx_mode)
0145     {
0146         if (EXSUCCEED!=tpclose())
0147         {
0148             NDRX_LOG(log_error, "TESTERROR: tpclose() failed: %s", 
0149                     tpstrerror(tperrno));
0150         }
0151     }
0152 
0153     tpterm();
0154     
0155     fprintf(stderr, "Exit with %d\n", ret);
0156     
0157     if (EXSUCCEED!=ret)
0158     {
0159         M_has_failure=EXTRUE;
0160     }
0161     
0162     return NULL;
0163 }
0164 
0165 /**
0166  * Run 3x threads.
0167  * Each will call the appropriate chain
0168  */
0169 int main(int argc, char** argv)
0170 {
0171 #define THREADS     3
0172     int ret=EXSUCCEED;
0173     pthread_t thrd[THREADS];
0174     int i;
0175     char *svcnms[] = {"TXOK", "TXFAIL", "NOTX"};
0176     ndrx_stopwatch_t w;
0177     /* open 3x threads, run for 10 min... */
0178     
0179     for (i=0; i<THREADS; i++)
0180     {
0181         if(EXSUCCEED!=pthread_create(&thrd[i], NULL, run_case, svcnms[i]))
0182         {
0183             NDRX_LOG(log_error, "TESTERROR: Failed to create %s thread", svcnms[i]);
0184             EXFAIL_OUT(ret);
0185         }
0186     }
0187     
0188     ndrx_stopwatch_reset(&w);
0189     
0190     /* run for period... */
0191     while(!M_has_failure && ndrx_stopwatch_get_delta_sec(&w) < 180)
0192     {
0193         sleep(1);
0194     }
0195     
0196     /* mark for shutdown */
0197     M_term = EXTRUE;
0198     
0199     /* wait for join */
0200     
0201     for (i=0; i<THREADS; i++)
0202     {
0203         void *retptr;
0204         pthread_join(thrd[i], &retptr);
0205     }
0206     
0207     /* check result */
0208     
0209     if (M_has_failure)
0210     {
0211         NDRX_LOG(log_error, "TESTERROR! Failure is set");
0212         ret=EXFAIL;
0213     }
0214     
0215 out:
0216     return ret;
0217 }
0218 
0219 /* vim: set ts=4 sw=4 et smartindent: */