Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief PostgreSQL PQ TMSRV driver tests / branch transactions - client
0003  *   Perform local calls with help of PQ commands.
0004  *   the server process shall run the code with Embedded SQL
0005  *
0006  * @file cltqtest67.c
0007  */
0008 /* -----------------------------------------------------------------------------
0009  * Enduro/X Middleware Platform for Distributed Transaction Processing
0010  * Copyright (C) 2009-2016, ATR Baltic, Ltd. All Rights Reserved.
0011  * Copyright (C) 2017-2023, Mavimax, Ltd. All Rights Reserved.
0012  * This software is released under one of the following licenses:
0013  * AGPL (with Java and Go exceptions) or Mavimax's license for commercial use.
0014  * See LICENSE file for full text.
0015  * -----------------------------------------------------------------------------
0016  * AGPL license:
0017  *
0018  * This program is free software; you can redistribute it and/or modify it under
0019  * the terms of the GNU Affero General Public License, version 3 as published
0020  * by the Free Software Foundation;
0021  *
0022  * This program is distributed in the hope that it will be useful, but WITHOUT ANY
0023  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
0024  * PARTICULAR PURPOSE. See the GNU Affero General Public License, version 3
0025  * for more details.
0026  *
0027  * You should have received a copy of the GNU Affero General Public License along 
0028  * with this program; if not, write to the Free Software Foundation, Inc.,
0029  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0030  *
0031  * -----------------------------------------------------------------------------
0032  * A commercial use license is available from Mavimax, Ltd
0033  * contact@mavimax.com
0034  * -----------------------------------------------------------------------------
0035  */
0036 #include <string.h>
0037 #include <stdio.h>
0038 #include <stdlib.h>
0039 #include <memory.h>
0040 #include <math.h>
0041 
0042 #include <atmi.h>
0043 #include <ubf.h>
0044 #include <ndebug.h>
0045 #include <test.fd.h>
0046 #include <ndrstandard.h>
0047 #include <nstopwatch.h>
0048 #include <fcntl.h>
0049 #include <unistd.h>
0050 #include <nstdutil.h>
0051 #include "test67.h"
0052 #include <libpq-fe.h>
0053 /*---------------------------Externs------------------------------------*/
0054 /*---------------------------Macros-------------------------------------*/
0055 /*---------------------------Enums--------------------------------------*/
0056 /*---------------------------Typedefs-----------------------------------*/
0057 /*---------------------------Globals------------------------------------*/
0058 /*---------------------------Statics------------------------------------*/
0059 /*---------------------------Prototypes---------------------------------*/
0060 
0061 /**
0062  * Run generic queue tests
0063  * transaction/insert/queue add - abort (test 0/no msg)
0064  * transaction/insert/queue add - commit (test count ok / have messages OK)
0065  * Say 100 msg·
0066  */
0067 expublic int q_run(UBFH **pp_ub)
0068 {
0069     int ret = EXSUCCEED;
0070     long i, j;
0071     long len;
0072     long tmp;
0073     long rsplen;
0074     TPQCTL qc;
0075     
0076     for (i=0; i<3; i++)
0077     {
0078         sql_mktab();
0079         
0080         /* start tran... */
0081         if (EXSUCCEED!=tpbegin(60, 0))
0082         {
0083             NDRX_LOG(log_error, "TESTERROR: Failed to begin: %s", tpstrerror(tperrno));
0084             EXFAIL_OUT(ret);
0085         }
0086         
0087         for (j=0; j<100; j++)
0088         {
0089             if (EXFAIL==Bchg(*pp_ub, T_LONG_FLD, 0, (char *)&j, 0))
0090             {
0091                 NDRX_LOG(log_debug, "TESTERROR: Failed to set T_LONG_FLD: %s", 
0092                         Bstrerror(Berror));
0093                 EXFAIL_OUT(ret);
0094             }    
0095 
0096             if (EXFAIL == tpcall("TESTSV", (char *)*pp_ub, 0L, (char **)pp_ub, &rsplen,0))
0097             {
0098                 NDRX_LOG(log_error, "TESTERROR: TESTSV failed: %s", tpstrerror(tperrno));
0099                 EXFAIL_OUT(ret);
0100             }
0101             
0102             /* enqueue message... */
0103             
0104             /* enqueue the data buffer */
0105             memset(&qc, 0, sizeof(qc));
0106             if (EXSUCCEED!=tpenqueue("MYSPACE", "STATQ", &qc, (char *)*pp_ub, 
0107                     0L, 0L))
0108             {
0109                 NDRX_LOG(log_error, "TESTERROR: tpenqueue() failed %s diag: %d:%s", 
0110                         tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0111                 EXFAIL_OUT(ret);
0112             }
0113         }
0114         
0115         if (0==i || 2==i)
0116         {
0117             if (2==i)
0118             {
0119                 /* test the timeout of transaction / zapping by tmsrv ... */
0120                 sleep(70);
0121                 
0122                 if (EXSUCCEED==tpcommit(0L))
0123                 {
0124                     NDRX_LOG(log_error, "TESTERROR: tpcommit must fail but succeed!");
0125                     EXFAIL_OUT(ret);
0126                 }
0127                 
0128                 if (TPEABORT!=tperrno)
0129                 {
0130                     NDRX_LOG(log_error, "TESTERROR: transaction must be aborted but: %s!",
0131                             tpstrerror(tperrno));
0132                     EXFAIL_OUT(ret);
0133                 }
0134                 
0135             }
0136             else
0137             {
0138                 if (EXSUCCEED!=tpabort(0L))
0139                 {
0140                     NDRX_LOG(log_error, "TESTERROR: Failed to abort: %s", 
0141                             tpstrerror(tperrno));
0142                     EXFAIL_OUT(ret);
0143                 }
0144             }
0145             
0146             /* Check the counts, must be 0 */
0147             if (0!=(ret=(int)sql_count()))
0148             {
0149                 NDRX_LOG(log_error, "TESTERROR: Invalid count expected 0 got: %d", ret);
0150                 EXFAIL_OUT(ret);
0151             }
0152             
0153             /* dequeue shall fail... / no msg */
0154             
0155             memset(&qc, 0, sizeof(qc));
0156 
0157             if (EXSUCCEED==tpdequeue("MYSPACE", "STATQ", &qc, (char **)pp_ub, 
0158                     &len, TPNOTRAN))
0159             {
0160                 /*
0161                 NDRX_LOG(log_error, "TESTERROR: tpdequeue() failed %s diag: %d:%s", 
0162                         tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0163                  * */
0164                 NDRX_LOG(log_error, "TESTERROR: Queue must be empty but it is not!");
0165                 EXFAIL_OUT(ret);
0166             }
0167             
0168             if (TPEDIAGNOSTIC!=tperrno)
0169             {
0170                 NDRX_LOG(log_error, "TESTERROR: Expected TPEDIAGNOSTIC, but got %d", tperrno);
0171                 EXFAIL_OUT(ret);
0172             }
0173             
0174             if (QMENOMSG!=qc.diagnostic)
0175             {
0176                 NDRX_LOG(log_error, "TESTERROR: Expected QMENOMSG, but got %d", 
0177                         qc.diagnostic);
0178                 EXFAIL_OUT(ret);
0179             }
0180         }
0181         else
0182         {
0183             if (EXSUCCEED!=tpcommit(0L))
0184             {
0185                 NDRX_LOG(log_error, "TESTERROR: Failed to commit: %s", 
0186                         tpstrerror(tperrno));
0187                 EXFAIL_OUT(ret);
0188             }
0189             
0190             /* Check the counts, must be 0 */
0191             if (100!=(ret=(int)sql_count()))
0192             {
0193                 NDRX_LOG(log_error, "TESTERROR: Invalid count expected 100 got: %d", 
0194                         ret);
0195                 EXFAIL_OUT(ret);
0196             }
0197             ret = EXSUCCEED;
0198             
0199             for (j=0; j<100; j++)
0200             {
0201                 /* there must be 100 msgs in queue */
0202 
0203                 memset(&qc, 0, sizeof(qc));
0204 
0205                 if (EXSUCCEED!=tpdequeue("MYSPACE", "STATQ", &qc, (char **)pp_ub, 
0206                         &len, TPNOTRAN))
0207                 {
0208                     NDRX_LOG(log_error, "TESTERROR: tpdequeue() failed at %d %s diag: %d:%s", 
0209                             i, tpstrerror(tperrno), qc.diagnostic, qc.diagmsg);
0210                     EXFAIL_OUT(ret);
0211                 }
0212 
0213                 /* test the buffer */
0214 
0215                 if (EXSUCCEED!=Bget(*pp_ub, T_LONG_FLD, 0, (char *)&tmp, 0L))
0216                 {
0217                     NDRX_LOG(log_error, "TESTERROR: Failed to get T_LONG_FLD at %d: %s",
0218                             i, Bstrerror(Berror));
0219                     EXFAIL_OUT(ret);
0220                 }
0221                 
0222                 if (tmp!=j)
0223                 {
0224                     NDRX_LOG(log_error, "TESTERROR: Invalid msg from Q: expected %ld got %ld",
0225                             j, tmp);
0226                     EXFAIL_OUT(ret);
0227                 }
0228             }
0229         } /* if i=1 (this is case for commit) */
0230         
0231         
0232     } /* for test case */
0233 out:
0234     return ret;
0235 }
0236 
0237 /* vim: set ts=4 sw=4 et smartindent: */