Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief loop test for RAC failover/RECON testing
0003  *
0004  * @file atmiclt47_loop.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 "test47.h"
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 
0059 /**
0060  * Check the balance
0061  * @param accnum account number to return balance
0062  * @return on error -1, 0 on succeed.
0063  */
0064 int check_balance(char *accnum, long *balance)
0065 {
0066     int ret=EXSUCCEED;
0067     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0068     long rsplen;
0069     
0070     if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, accnum, 0, BFLD_STRING))
0071     {
0072         NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0073         ret=EXFAIL;
0074         goto out;
0075     }
0076 
0077     if (EXFAIL == tpcall("BALANCE", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,TPNOABORT))
0078     {
0079         NDRX_LOG(log_error, "BALANCE failed: %s", tpstrerror(tperrno));
0080         ret=EXFAIL;
0081         goto out;
0082     }
0083     
0084     if (EXFAIL==Bget(p_ub, T_LONG_FLD, 0, (char *)balance, 0))
0085     {
0086         NDRX_LOG(log_debug, "Failed to set T_LONG_FLD[0]: %s", Bstrerror(Berror));
0087         ret=EXFAIL;
0088         goto out;
0089     }
0090     
0091 out:
0092     if (NULL!=p_ub)
0093     {
0094         tpfree((char *)p_ub);
0095     }
0096 
0097     return ret;
0098 }
0099 
0100 /**
0101  * Do the test call to the server
0102  */
0103 int main(int argc, char** argv)
0104 {
0105     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0106     long rsplen;
0107     long l;
0108     char *rsp=NULL;
0109     char tmp[51];
0110     long bal;
0111     int ret=EXSUCCEED;
0112     int upper1=100, upper2=300;
0113     char *p;
0114     ndrx_stopwatch_t w_loop;
0115     ndrx_stopwatch_t w_rsp;
0116     long max_rsp=-1;
0117 
0118     if (NULL!=(p=getenv(CONF_NDRX_XA_FLAGS)) && NULL!=strstr(p, "BTIGHT"))
0119     {
0120         upper1=5;
0121         upper2=205;
0122     }
0123     
0124     if (EXSUCCEED!=tpinit(NULL))
0125     {
0126         NDRX_LOG(log_debug, "Failed to init!: %s", tpstrerror(tperrno));
0127         EXFAIL_OUT(ret);
0128     }
0129     
0130     /* will work with transactions */
0131     if (EXSUCCEED!=tpopen())
0132     {
0133         NDRX_LOG(log_debug, "Failed to tpopen!: %s", tpstrerror(tperrno));
0134         EXFAIL_OUT(ret);
0135     }
0136     
0137     ndrx_stopwatch_reset(&w_loop);
0138     ndrx_stopwatch_reset(&w_rsp);
0139     
0140     /* run for 13 min */
0141     while (1)
0142     {
0143 again:
0144         /* terminate our loop... */
0145         if (ndrx_stopwatch_get_delta_sec(&w_loop) >= 800)
0146         {
0147             break;
0148         }
0149         /* if response is longer than than... go bad..*/
0150         if ((l=ndrx_stopwatch_get_delta_sec(&w_rsp)) > MAX_RSP)
0151         {
0152             NDRX_LOG(log_error, "TESTERROR: Expected OK response in %d sec got %ld", MAX_RSP, l);
0153             EXFAIL_OUT(ret);
0154         }
0155         
0156         /* delete accounts no global transactional */
0157         while (EXFAIL == tpcall("ACCCLEAN", NULL, 0L, &rsp, &rsplen,0))
0158         {
0159             NDRX_LOG(log_error, "ACCCLEAN failed: %s", tpstrerror(tperrno));
0160             sleep(1);
0161             
0162             /* go out if looped here */
0163             if ((l=ndrx_stopwatch_get_delta_sec(&w_rsp)) > MAX_RSP)
0164             {
0165                 NDRX_LOG(log_error, "TESTERROR: Expected OK response in %d sec got %ld (2)", 
0166                         MAX_RSP, l);
0167                 EXFAIL_OUT(ret);
0168             }
0169         }
0170 
0171         while (EXSUCCEED != tpbegin(MAX_RSP, 0))
0172         {
0173             NDRX_LOG(log_error, "tpbegin failed: %s", tpstrerror(tperrno));
0174             sleep(1);
0175         }
0176 
0177         /* Check multi commit... */
0178         for (l=200; l<upper2; l++)
0179         {
0180             snprintf(tmp, sizeof(tmp), "ACC%03ld", l);
0181         
0182             if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, tmp, 0, BFLD_STRING))
0183             {
0184                 NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0185                 ret=EXFAIL;
0186                 goto out;
0187             }    
0188         
0189             if (EXFAIL==CBchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0, BFLD_LONG))
0190             {
0191                 NDRX_LOG(log_debug, "Failed to set T_LONG_FLD[0]: %s", Bstrerror(Berror));
0192                 ret=EXFAIL;
0193                 goto out;
0194             }    
0195         
0196             /* TPTRANSUSPEND must be present, otherwise other RMID must be defined
0197              * as two binaries cannot operate on the same transaction
0198              */
0199             if (EXFAIL == tpcall("ACCOPEN", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0200             {
0201                 NDRX_LOG(log_error, "ACCOPEN failed: %s", tpstrerror(tperrno));
0202                 /* abort the transaction, or mark for rollback... */
0203                 if (EXSUCCEED!=tpabort(0))
0204                 {
0205                     NDRX_LOG(log_error, "tpabort failed: %s", tpstrerror(tperrno));
0206                 }
0207                 /* restart the test.. */
0208                 goto again;
0209             }
0210             /* we shall have some positive response! */
0211             ndrx_stopwatch_reset(&w_rsp);
0212         }
0213     
0214         /* at this point transaction must be marked for rollback */
0215         if (EXSUCCEED != tpcommit(0))
0216         {
0217             NDRX_LOG(log_error, "tpcommit failed: %s", tpstrerror(tperrno));
0218             tpabort(0);
0219         }
0220     }
0221 
0222     
0223 out:
0224 
0225     /* if response is longer than than... go bad..*/
0226     if ((l=ndrx_stopwatch_get_delta_sec(&w_rsp)) > MAX_RSP)
0227     {
0228         NDRX_LOG(log_error, "TESTERROR: Expected OK response in %d sec got %ld", MAX_RSP, l);
0229         ret=EXFAIL;
0230     }
0231 
0232     if (EXSUCCEED!=ret)
0233     {
0234         /* Abort anyway... */
0235         tpabort(0);
0236     }
0237 
0238     if (NULL!=p_ub)
0239     {
0240         tpfree((char *)p_ub);
0241     }
0242 
0243     tpclose();
0244     tpterm();
0245     
0246     /* return an error marking */
0247     if (EXSUCCEED!=ret)
0248     {
0249         NDRX_LOG(log_error, "TESTERROR: Exit with %d", ret);
0250     }
0251     
0252     fprintf(stderr, "Exit with %d\n", ret);
0253 
0254     return ret;
0255 }
0256 /* vim: set ts=4 sw=4 et smartindent: */