Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Test auto-transaction functionality - client
0003  *  basically client runs in non-transaction env.
0004  *  We call the auto-tran server which forward to another auto tran server
0005  *  we control commit or not.
0006  *  from script we stop the tmsrv at certain points to simulate system
0007  *  response in case of begin/commit/abort failures.
0008  *  TODO: - put second server in the dom2
0009  *  TODO: - implement conversation error reply responder.
0010  *
0011  * @file atmiclt82.c
0012  */
0013 /* -----------------------------------------------------------------------------
0014  * Enduro/X Middleware Platform for Distributed Transaction Processing
0015  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0016  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0017  * This software is released under one of the following licenses:
0018  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0019  * See LICENSE file for full text.
0020  * -----------------------------------------------------------------------------
0021  * AGPL license:
0022  * 
0023  * This program is free software; you can redistribute it and/or modify it under
0024  * the terms of the GNU Affero General Public License, version 3 as published
0025  * by the Free Software Foundation;
0026  *
0027  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0028  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0029  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0030  * for more details.
0031  *
0032  * You should have received a copy of the GNU Affero General Public License along 
0033  * with this program; if not, write to the Free Software Foundation, Inc., 
0034  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0035  *
0036  * -----------------------------------------------------------------------------
0037  * A commercial use license is available from Mavimax, Ltd
0038  * contact@mavimax.com
0039  * -----------------------------------------------------------------------------
0040  */
0041 #include <string.h>
0042 #include <stdio.h>
0043 #include <stdlib.h>
0044 #include <memory.h>
0045 #include <math.h>
0046 
0047 #include <atmi.h>
0048 #include <ubf.h>
0049 #include <ndebug.h>
0050 #include <test.fd.h>
0051 #include <ndrstandard.h>
0052 #include <nstopwatch.h>
0053 #include <fcntl.h>
0054 #include <unistd.h>
0055 #include <nstdutil.h>
0056 #include "test82.h"
0057 /*---------------------------Externs------------------------------------*/
0058 /*---------------------------Macros-------------------------------------*/
0059 /*---------------------------Enums--------------------------------------*/
0060 /*---------------------------Typedefs-----------------------------------*/
0061 /*---------------------------Globals------------------------------------*/
0062 /*---------------------------Statics------------------------------------*/
0063 /*---------------------------Prototypes---------------------------------*/
0064 
0065 /**
0066  * Do the test call to the server
0067  */
0068 int main(int argc, char** argv)
0069 {
0070     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0071     long rsplen;
0072     int ret=EXSUCCEED;
0073     long olen;
0074     char *buf = NULL;
0075     long revent;
0076     int cd=EXFAIL;
0077     int tmp;
0078     
0079     /* We shall call commands:
0080      * - Commands are: "OK"
0081      * - Commands are: "FAIL"
0082      * - Commands are: "COUNT"
0083      */
0084     if (argc<2)
0085     {
0086         NDRX_LOG(log_error, "Missing command code");
0087         fprintf(stderr, "Usage: %s <command> [C]\n", argv[0]);
0088         EXFAIL_OUT(ret);
0089     }
0090     
0091     if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, argv[1], 0, BFLD_STRING))
0092     {
0093         NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0094         ret=EXFAIL;
0095         goto out;
0096     }
0097     
0098     /* Let the shell count the messages added.. */
0099     if (0==strcmp(argv[1], "COUNT"))
0100     {
0101         /* W/O tran, try to dequeue in loop & print msgs... */
0102         TPQCTL qc;
0103         
0104         memset(&qc, 0, sizeof(qc));
0105         
0106         while (EXSUCCEED==tpdequeue("MYSPACE", "MSGQ", &qc, &buf, &olen, TPNOTRAN))
0107         {
0108             /* Print the buffer... OK? */
0109             Bprint((UBFH *)buf);
0110             
0111             tpfree(buf);
0112             memset(&qc, 0, sizeof(qc));
0113             buf=NULL;
0114         }
0115         
0116         if (tperrno!=TPEDIAGNOSTIC)
0117         {
0118             NDRX_LOG(log_debug, "TESTERROR: Expected TPEDIAGNOSTIC, got: %s", 
0119                     tpstrerror(tperrno));
0120             ret=EXFAIL;
0121             goto out;
0122         }
0123         
0124         if (QMENOMSG!=qc.diagnostic)
0125         {
0126             NDRX_LOG(log_debug, "TESTERROR: Expected QMENOMSG, got: %d", 
0127                     qc.diagnostic);
0128             ret=EXFAIL;
0129             goto out;
0130         }
0131     }
0132     else
0133     {
0134         if (argc>2 && ('C'==argv[2][0] || 'S'==argv[2][0] || 'Z'==argv[2][0]))
0135         {
0136             if ('C'==argv[2][0])
0137             {
0138                 tmp = TPRECVONLY;
0139             }
0140             else if ('S'==argv[2][0])
0141             {
0142                 tmp = TPSENDONLY;
0143             }
0144             
0145             if (EXFAIL == (cd=tpconnect("TESTSV2", (char *)p_ub, 0L, tmp)))
0146             {
0147                 NDRX_LOG(log_error, "TESTSV2 failed: %s", tpstrerror(tperrno));
0148                 /* capture the error code from the script */
0149                 printf("%s\n", tpstrerror(tperrno));
0150                 ret=EXFAIL;
0151                 goto out;
0152             }
0153             
0154             /* get event */
0155             if ('C'==argv[2][0])
0156             {
0157                 tmp = tprecv(cd, &buf, &olen, 0, &revent);
0158             }
0159             else if ('S'==argv[2][0])
0160             {
0161                 sleep(17); /* let server to return */
0162                 /* check with send entry point... */
0163                 tmp = tpsend(cd, NULL, 0, 0, &revent);
0164             }
0165             
0166             if (EXFAIL==tmp)
0167             {
0168                 NDRX_LOG(log_error, "RECV failed: %s %ld", tpstrerror(tperrno), revent);
0169                 
0170                 if (TPEEVENT==tperrno)
0171                 {
0172                     if (TPEV_SVCSUCC==revent)
0173                     {
0174                         /* OK */
0175                         goto out;
0176                     }
0177 
0178                     switch (revent)
0179                     {
0180                         case TPEV_DISCONIMM:
0181                             printf("TPEV_DISCONIMM\n");
0182                             break;
0183                         case TPEV_SENDONLY:
0184                             printf("TPEV_SENDONLY\n");
0185                             break;
0186                         case TPEV_SVCERR:
0187                             printf("TPEV_SVCERR\n");
0188                             break;
0189                         case TPEV_SVCFAIL:
0190                             printf("TPEV_SVCFAIL\n");
0191                             break;
0192                         default:
0193                             printf("TESTERROR event %ld\n", revent);
0194                             break;
0195                     }
0196                 }
0197                 else
0198                 {
0199                     printf("RECV failed: %s %ld\n", tpstrerror(tperrno), revent);
0200                 }
0201                 
0202                 ret=EXFAIL;
0203                 goto out;
0204             }
0205             
0206             if (EXFAIL!=cd && EXSUCCEED!=tpdiscon(cd))
0207             {
0208                 NDRX_LOG(log_error, "TESTERROR: failed to discon: %s", 
0209                         tpstrerror(tperrno));
0210                 EXFAIL_OUT(ret);
0211             }
0212             
0213         }
0214         else if (argc>2 && 'A'==argv[2][0])
0215         {
0216             /* 
0217              * Async check 
0218              * So either committed or aborted. It shall not be active transaction
0219              */
0220             if (EXFAIL == tpacall("TESTSV2", (char *)p_ub, 0L, TPNOREPLY))
0221             {
0222                 NDRX_LOG(log_error, "TESTSV2 async call failed: %s", tpstrerror(tperrno));
0223                 /* capture the error code from the script */
0224                 printf("%s\n", tpstrerror(tperrno));
0225                 ret=EXFAIL;
0226                 goto out;
0227             }
0228             
0229             /* let service to finish */
0230             sleep(3);
0231         }
0232         else
0233         {
0234             char svcnm[XATMI_SERVICE_NAME_LENGTH+1]="TESTSV";
0235 
0236             if (0==strcmp(argv[1], "ABORT3"))
0237             {
0238                 NDRX_STRCPY_SAFE(svcnm, "TESTSVD");
0239             }
0240 
0241             if (EXFAIL == tpcall(svcnm, (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0242             {
0243                 NDRX_LOG(log_error, "%s failed: %s", svcnm, tpstrerror(tperrno));
0244                 /* capture the error code from the script */
0245                 printf("%s\n", tpstrerror(tperrno));
0246                 ret=EXFAIL;
0247                 goto out;
0248 
0249             }
0250         }
0251     }
0252     
0253 out:
0254 
0255     if (NULL!=buf)
0256     {
0257         tpfree(buf);
0258     }
0259     tpterm();
0260     fprintf(stderr, "Exit with %d\n", ret);
0261 
0262     return ret;
0263 }
0264 
0265 /* vim: set ts=4 sw=4 et smartindent: */