Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Test server connection to Oracle DB from C - client
0003  *
0004  * @file atmiclt47.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     /* test Pro*C + OCI at account open */
0115     char opensvc[2][64] = {"ACCOPEN", "ACCOPEN_OCI"};
0116 
0117     if (NULL!=(p=getenv(CONF_NDRX_XA_FLAGS)) && NULL!=strstr(p, "BTIGHT"))
0118     {
0119         upper1=5;
0120         upper2=205;
0121     }
0122     
0123     if (EXSUCCEED!=tpinit(NULL))
0124     {
0125         NDRX_LOG(log_debug, "Failed to init!: %s", tpstrerror(tperrno));
0126         EXFAIL_OUT(ret);
0127     }
0128     
0129     /* will work with transactions */
0130     if (EXSUCCEED!=tpopen())
0131     {
0132         NDRX_LOG(log_debug, "Failed to tpopen!: %s", tpstrerror(tperrno));
0133         EXFAIL_OUT(ret);
0134     }
0135     
0136     /* delete accounts no global transactional */
0137     if (EXFAIL == tpcall("ACCCLEAN", NULL, 0L, &rsp, &rsplen,0))
0138     {
0139         NDRX_LOG(log_error, "ACCCLEAN failed: %s", tpstrerror(tperrno));
0140         ret=EXFAIL;
0141         goto out;
0142     }
0143     
0144     if (EXSUCCEED != tpbegin(60, 0))
0145     {
0146         NDRX_LOG(log_error, "tpbegin failed: %s", tpstrerror(tperrno));
0147         ret=EXFAIL;
0148         goto out;
0149     }
0150 
0151     NDRX_STRCPY_SAFE(tmp, "ACC000");
0152     if (EXSUCCEED==check_balance(tmp, &bal))
0153     {
0154         NDRX_LOG(log_error, "check_balance must fail for [%s]", tmp);
0155         ret=EXFAIL;
0156         goto out;
0157     }
0158     
0159     if (EXSUCCEED != tpcommit(0))
0160     {
0161         NDRX_LOG(log_error, "tpcommit failed: %s", tpstrerror(tperrno));
0162         ret=EXFAIL;
0163         goto out;
0164     }
0165 
0166     for (l=0; l<upper1; l++)
0167     {
0168         snprintf(tmp, sizeof(tmp), "ACC%03ld", l);
0169         
0170         /* make an account, but abort... */
0171         
0172         if (EXSUCCEED != tpbegin(60, 0))
0173         {
0174             NDRX_LOG(log_error, "tpbegin failed: %s", tpstrerror(tperrno));
0175             ret=EXFAIL;
0176             goto out;
0177         }
0178         
0179         if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, tmp, 0, BFLD_STRING))
0180         {
0181             NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0182             ret=EXFAIL;
0183             goto out;
0184         }    
0185         
0186         if (EXFAIL==CBchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0, BFLD_LONG))
0187         {
0188             NDRX_LOG(log_debug, "Failed to set T_LONG_FLD[0]: %s", Bstrerror(Berror));
0189             ret=EXFAIL;
0190             goto out;
0191         }    
0192         
0193         /* TPTRANSUSPEND must be present, otherwise other RMID must be defined
0194          * as two binaries cannot operate on the same transaction
0195          */
0196         if (EXFAIL == tpcall(opensvc[l%2], (char *)p_ub, 0L, (char **)&p_ub, &rsplen,TPTRANSUSPEND))
0197         {
0198             NDRX_LOG(log_error, "%s failed: %s", opensvc[l%2], tpstrerror(tperrno));
0199             ret=EXFAIL;
0200             goto out;
0201         }
0202         
0203         if (EXSUCCEED != tpabort(0))
0204         {
0205             NDRX_LOG(log_error, "tpabort failed: %s", tpstrerror(tperrno));
0206             ret=EXFAIL;
0207             goto out;
0208         }
0209         
0210         if (EXSUCCEED==check_balance(tmp, &bal))
0211         {
0212             NDRX_LOG(log_error, "Account [%s] found after abort! error!", tmp);
0213             ret=EXFAIL;
0214             goto out;
0215         }
0216         
0217         /* start new tran */
0218         if (EXSUCCEED != tpbegin(60, 0))
0219         {
0220             NDRX_LOG(log_error, "tpbegin failed: %s", tpstrerror(tperrno));
0221             ret=EXFAIL;
0222             goto out;
0223         }
0224         
0225         if (EXFAIL == tpcall(opensvc[l%2], (char *)p_ub, 0L, (char **)&p_ub, &rsplen,TPTRANSUSPEND))
0226         {
0227             NDRX_LOG(log_error, "%s failed: %s", opensvc[l%2], tpstrerror(tperrno));
0228             ret=EXFAIL;
0229             goto out;
0230         }
0231         
0232         /* balance shall be checked ok here too, as we use joins. */
0233         if (EXSUCCEED!=check_balance(tmp, &bal))
0234         {
0235             NDRX_LOG(log_error, "Account [%s] NOT found!", tmp);
0236             ret=EXFAIL;
0237             goto out;
0238         }
0239         
0240         if (EXSUCCEED != tpcommit(0))
0241         {
0242             NDRX_LOG(log_error, "tpcommit failed: %s", tpstrerror(tperrno));
0243             ret=EXFAIL;
0244             goto out;
0245         }
0246         
0247         if (EXSUCCEED!=check_balance(tmp, &bal))
0248         {
0249             NDRX_LOG(log_error, "Account [%s] NOT found!", tmp);
0250             ret=EXFAIL;
0251             goto out;
0252         }
0253         
0254         if (bal!=l)
0255         {
0256             NDRX_LOG(log_error, "Invalid balance expected: %ld, but got: %ld", 
0257                     l, bal);
0258             ret=EXFAIL;
0259             goto out;
0260         }
0261     }
0262 
0263     
0264     if (EXSUCCEED != tpbegin(60, 0))
0265     {
0266         NDRX_LOG(log_error, "tpbegin failed: %s", tpstrerror(tperrno));
0267         ret=EXFAIL;
0268         goto out;
0269     }
0270         
0271     /* Check multi commit... */
0272     for (l=200; l<upper2; l++)
0273     {
0274         snprintf(tmp, sizeof(tmp), "ACC%03ld", l);
0275         
0276         if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, tmp, 0, BFLD_STRING))
0277         {
0278             NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0279             ret=EXFAIL;
0280             goto out;
0281         }    
0282         
0283         if (EXFAIL==CBchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0, BFLD_LONG))
0284         {
0285             NDRX_LOG(log_debug, "Failed to set T_LONG_FLD[0]: %s", Bstrerror(Berror));
0286             ret=EXFAIL;
0287             goto out;
0288         }    
0289         
0290         /* TPTRANSUSPEND must be present, otherwise other RMID must be defined
0291          * as two binaries cannot operate on the same transaction
0292          */
0293         if (EXFAIL == tpcall("ACCOPEN", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,TPTRANSUSPEND))
0294         {
0295             NDRX_LOG(log_error, "ACCOPEN failed: %s", tpstrerror(tperrno));
0296             ret=EXFAIL;
0297             goto out;
0298         }
0299     }
0300     
0301     if (EXSUCCEED != tpcommit(0))
0302     {
0303         NDRX_LOG(log_error, "tpcommit failed: %s", tpstrerror(tperrno));
0304         ret=EXFAIL;
0305         goto out;
0306     }
0307     
0308     /* Check balance... */
0309     for (l=200; l<upper2; l++)
0310     {
0311         snprintf(tmp, sizeof(tmp), "ACC%03ld", l);
0312         
0313         if (EXSUCCEED!=check_balance(tmp, &bal))
0314         {
0315             NDRX_LOG(log_error, "Account [%s] NOT found!", tmp);
0316             ret=EXFAIL;
0317             goto out;
0318         }
0319         
0320         if (bal!=l)
0321         {
0322             NDRX_LOG(log_error, "Invalid balance expected: %ld, but got: %ld", 
0323                     l, bal);
0324             ret=EXFAIL;
0325             goto out;
0326         }
0327     }
0328 
0329     NDRX_LOG(log_debug, "Checking fetch + tpcall in tran");
0330 
0331     if (EXSUCCEED != tpbegin(60, 0))
0332     {
0333         NDRX_LOG(log_error, "tpbegin failed: %s", tpstrerror(tperrno));
0334         ret=EXFAIL;
0335         goto out;
0336     }
0337 
0338     if (EXFAIL == tpcall("FETCH", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0339     {
0340         NDRX_LOG(log_error, "FETCH failed: %s", tpstrerror(tperrno));
0341         ret=EXFAIL;
0342         goto out;
0343     }
0344 
0345     if (EXSUCCEED != tpcommit(0))
0346     {
0347         NDRX_LOG(log_error, "tpcommit failed: %s", tpstrerror(tperrno));
0348         ret=EXFAIL;
0349         goto out;
0350     }
0351 
0352     /* Check ops without transaction (local at server trans) */
0353     NDRX_LOG(log_debug, "Checking server transactions");
0354     if (NULL==p_ub)
0355     {
0356         /* FETCH previously returns NULL buffer ... */
0357         p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0358     }
0359     for (l=0; l<upper1; l++)
0360     {
0361         snprintf(tmp, sizeof(tmp), "ACX%03ld", l);
0362 
0363         /* make an account, but abort... */        
0364 
0365         if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, tmp, 0, BFLD_STRING))
0366         {
0367             NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0368             ret=EXFAIL;
0369             goto out;
0370         }
0371 
0372         if (EXFAIL==CBchg(p_ub, T_LONG_FLD, 0, (char *)&l, 0, BFLD_LONG))
0373         {
0374             NDRX_LOG(log_debug, "Failed to set T_LONG_FLD[0]: %s", Bstrerror(Berror));
0375             ret=EXFAIL;
0376             goto out;
0377         }
0378 
0379 
0380         if (EXFAIL == tpcall(opensvc[l%2], (char *)p_ub, 0L, (char **)&p_ub, &rsplen, 0))
0381         {
0382             NDRX_LOG(log_error, "%s failed: %s", opensvc[l%2], tpstrerror(tperrno));
0383             ret=EXFAIL;
0384             goto out;
0385         }
0386         
0387         if (EXSUCCEED!=check_balance(tmp, &bal))
0388         {
0389             NDRX_LOG(log_error, "Account [%s] NOT found!", tmp);
0390             ret=EXFAIL;
0391             goto out;
0392         }
0393 
0394         if (bal!=l)
0395         {
0396             NDRX_LOG(log_error, "Invalid balance expected: %ld, but got: %ld",
0397                     l, bal);
0398             ret=EXFAIL;
0399             goto out;
0400         }
0401     }
0402 
0403     
0404 out:
0405 
0406     if (EXSUCCEED!=ret)
0407     {
0408         /* Abort anyway... */
0409         tpabort(0);
0410     }
0411 
0412     if (NULL!=p_ub)
0413     {
0414         tpfree((char *)p_ub);
0415     }
0416 
0417     tpclose();
0418     tpterm();
0419     fprintf(stderr, "Exit with %d\n", ret);
0420 
0421     return ret;
0422 }
0423 /* vim: set ts=4 sw=4 et smartindent: */