Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Test of tpsblktime(), tpgblktime()
0003  *
0004  * @file atmiclt51.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 "test51.h"
0050 #include "atmi_int.h"
0051 #include <exassert.h>
0052 /*---------------------------Externs------------------------------------*/
0053 /*---------------------------Macros-------------------------------------*/
0054 /*---------------------------Enums--------------------------------------*/
0055 /*---------------------------Typedefs-----------------------------------*/
0056 /*---------------------------Globals------------------------------------*/
0057 /*---------------------------Statics------------------------------------*/
0058 /*---------------------------Prototypes---------------------------------*/
0059 
0060 /**
0061  * Validate timeout response for dead server
0062  * @param tout expected value
0063  * @return EXSUCCEED/EXFAIL
0064  */
0065 int chk_tpcall(int tout)
0066 {
0067     /* validate the tout response */
0068     ndrx_stopwatch_t w;
0069     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0070     long rsplen, delta;
0071     int ret = EXSUCCEED;
0072     
0073     ndrx_stopwatch_reset(&w);
0074     
0075     if (EXSUCCEED == tpcall("SLEEPSV", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0076     {
0077         NDRX_LOG(log_error, "TESTERROR!  Call succeed but must fail!");
0078         ret=EXFAIL;
0079         goto out;
0080     }
0081     
0082     /* the time shall be more or less as in boundries */
0083     delta = ndrx_stopwatch_get_delta_sec(&w);
0084     
0085     if (delta <tout-2 || delta > tout+2)
0086     {
0087         NDRX_LOG(log_error, "TESTERROR! Expected +-2 of %d got %ld",
0088                 tout, delta);
0089         ret=EXFAIL;
0090         goto out;
0091     }
0092     
0093 out:
0094                 
0095     if (NULL!=p_ub)
0096     {
0097         tpfree((char *)p_ub);
0098     }
0099     return ret;
0100 }
0101 
0102 /**
0103  * Fill up the queue.
0104  * Active the block service
0105  * @param msgs number of messages to load
0106  * @return EXSUCCEED/EXFAIL
0107  */
0108 int full_load(int msgs)
0109 {
0110     int i;
0111     int ret = EXSUCCEED;
0112     
0113     for (i=0; i<msgs; i++)
0114     {
0115         if (EXSUCCEED!=tpacall("SLEEPSV", NULL, 0, TPNOREPLY))
0116         {
0117             EXFAIL_OUT(ret);
0118         }
0119     }
0120     
0121 out:
0122     return ret;    
0123 }
0124 
0125 /**
0126  * Basic call tests
0127  * @return 
0128  */
0129 int call_tests(void)
0130 {
0131     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 56000);
0132     long rsplen;
0133     int cd;
0134     int ret=EXSUCCEED, tret;
0135     int err;
0136     
0137     if (EXSUCCEED!=tpsblktime(1, TPBLK_NEXT))
0138     {
0139         NDRX_LOG(log_debug, "TESTERROR: Failed to set timeout to 1: %s", 
0140                 tpstrerror(tperrno));
0141         EXFAIL_OUT(ret);
0142     }
0143     
0144     if (EXFAIL==CBchg(p_ub, T_STRING_FLD, 0, VALUE_EXPECTED, 0, BFLD_STRING))
0145     {
0146         NDRX_LOG(log_debug, "Failed to set T_STRING_FLD[0]: %s", Bstrerror(Berror));
0147         ret=EXFAIL;
0148         goto out;
0149     }    
0150 
0151     /* get standard timeout due to 1 sec... */
0152     if (EXSUCCEED == tpcall("TESTSV", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0153     {
0154         NDRX_LOG(log_error, "TESTERROR!  Call succeed but must fail!");
0155         ret=EXFAIL;
0156         goto out;
0157     }
0158     
0159     err = tperrno;
0160     
0161     if (tperrno!=TPETIME)
0162     {
0163         NDRX_LOG(log_error, "TESTERROR! Expected TPETIME, but got: %d!", err);
0164         ret=EXFAIL;
0165         goto out;
0166     }
0167 
0168     /* value form ALL */
0169     NDRX_ASSERT_TP_OUT(99==(tret=tpgblktime(0)), "Failed to get 0 timeout %d", tret);
0170 
0171     /* invoke now with full wait first 2 sec + 3 sec, we get 6-7 sec 
0172      * thus receive normal response...
0173      */
0174     if (EXSUCCEED!=tpsblktime(6, TPBLK_NEXT))
0175     {
0176         NDRX_LOG(log_debug, "TESTERROR: Failed to set timeout to 6: %s", 
0177                 tpstrerror(tperrno));
0178         EXFAIL_OUT(ret);
0179     }
0180 
0181     /* now wait fully for response, the first call shall be dropped.. */
0182     if (EXFAIL == tpcall("TESTSV", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0183     {
0184         NDRX_LOG(log_error, "TESTERROR!  Second call failed: %s", 
0185                 tpstrerror(tperrno));
0186         ret=EXFAIL;
0187         goto out;
0188     }
0189     
0190     /* also this will make assumtion that if we receive message
0191      * we do not check was it actually expired by previous timeout settings
0192      * this applies on waiting on reply queue and not actual msg age.
0193      * call age is checked by dest server.
0194      */ 
0195     if (EXSUCCEED!=tpsblktime(2, TPBLK_ALL))
0196     {
0197         NDRX_LOG(log_debug, "TESTERROR: Failed to set timeout to 2: %s", 
0198                 tpstrerror(tperrno));
0199         EXFAIL_OUT(ret);
0200     }
0201 
0202     /* shall test the case when message sits in queue for too long by custom
0203      * timeout setting, thus it must be dropped, thus lets do three async calls
0204      * for atleast one we shall get the dropped message in server log.
0205      * We will receive only one message... as issued in the same time
0206      */
0207     
0208     if (EXFAIL == tpacall("TESTSV", (char *)p_ub, 0L, 0))
0209     {
0210         NDRX_LOG(log_error, "TESTERROR! tpacall failed (1): %s", 
0211                 tpstrerror(tperrno));
0212         ret=EXFAIL;
0213         goto out;
0214     }
0215     
0216     if (EXFAIL == tpacall("TESTSV", (char *)p_ub, 0L, 0))
0217     {
0218         NDRX_LOG(log_error, "TESTERROR! tpacall failed (2): %s", 
0219                 tpstrerror(tperrno));
0220         ret=EXFAIL;
0221         goto out;
0222     }
0223     
0224     if (EXFAIL == tpacall("TESTSV", (char *)p_ub, 0L, 0))
0225     {
0226         NDRX_LOG(log_error, "TESTERROR! tpacall failed (3): %s", 
0227                 tpstrerror(tperrno));
0228         ret=EXFAIL;
0229         goto out;
0230     }
0231 
0232     /* wait 5 sec for each... */
0233     if (EXSUCCEED!=tpsblktime(5, TPBLK_ALL))
0234     {
0235         NDRX_LOG(log_debug, "TESTERROR: Failed to set timeout to 5: %s", 
0236                 tpstrerror(tperrno));
0237         EXFAIL_OUT(ret);
0238     }
0239     
0240     /* wait for reply
0241      * this one shall come back, as first processed in 3 sec.
0242      * The others must be dropped due to expired.. as the server requires wait for 3 sec
0243      * but we marked packets to live only for 2 sec
0244      */
0245     if (EXSUCCEED!=tpgetrply(&cd, (char **)&p_ub, &rsplen, TPGETANY))
0246     {
0247         NDRX_LOG(log_error, "TESTERROR! tpgetrply (1) failed: %s", 
0248                 tpstrerror(tperrno));
0249         ret=EXFAIL;
0250         goto out;
0251     }
0252     
0253     /* these shall be zapped by dest server due to expired */
0254     /* the other shall fail as total 4+4 > 4 tout setting */
0255     if (EXSUCCEED==tpgetrply(&cd, (char **)&p_ub, &rsplen, TPGETANY))
0256     {
0257         NDRX_LOG(log_error, "TESTERROR! tpgetrply (2) SUCCEED but must fail!");
0258         ret=EXFAIL;
0259         goto out;
0260     }
0261     
0262     err = tperrno;
0263     if (tperrno!=TPETIME)
0264     {
0265         NDRX_LOG(log_error, "TESTERROR! Expected TPETIME (2), but got: %d!", err);
0266         ret=EXFAIL;
0267         goto out;
0268     }
0269     
0270     if (EXSUCCEED==tpgetrply(&cd, (char **)&p_ub, &rsplen, TPGETANY))
0271     {
0272         NDRX_LOG(log_error, "TESTERROR! tpgetrply (3) SUCCEED but must fail!");
0273         ret=EXFAIL;
0274         goto out;
0275     }
0276     
0277     err = tperrno;
0278     if (tperrno!=TPETIME)
0279     {
0280         NDRX_LOG(log_error, "TESTERROR! Expected TPETIME (3), but got: %d!", err);
0281         ret=EXFAIL;
0282         goto out;
0283     }
0284   
0285 out:
0286     
0287     if (NULL!=p_ub)
0288     {
0289         tpfree((char *)p_ub);
0290     }
0291 
0292     return ret;
0293     
0294 }
0295 /**
0296  * Do the test call to the server
0297  */
0298 int main(int argc, char** argv)
0299 {
0300     int ret=EXSUCCEED;
0301     int tret;
0302     TPCONTEXT_T context, ctxt2;
0303 
0304     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpinit(NULL), "failed to init ctxt 1");
0305     NDRX_ASSERT_TP_OUT(EXSUCCEED==tptoutset(11), "Failed to set global timeout");
0306     
0307     /* read default values*/
0308     NDRX_ASSERT_TP_OUT(0==(tret=tpgblktime(TPBLK_ALL)), "Failed to get TPBLK_ALL %d", tret);
0309     NDRX_ASSERT_TP_OUT(0==(tret=tpgblktime(TPBLK_NEXT)), "Failed to get TPBLK_NEXT %d", tret);
0310     NDRX_ASSERT_TP_OUT(11==(tret=tpgblktime(0)), "Failed to get 0 blktime %d %d", tret, G_atmi_env.time_out);
0311 
0312     /* set + reset */
0313     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(3,TPBLK_ALL), "Failed to set TPBLK_ALL");
0314     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(4,TPBLK_NEXT), "Failed to set TPBLK_NEXT)");
0315 
0316     NDRX_ASSERT_TP_OUT(3==(tret=tpgblktime(TPBLK_ALL)), "Failed to get TPBLK_ALL %d", tret);
0317     NDRX_ASSERT_TP_OUT(4==(tret=tpgblktime(TPBLK_NEXT)), "Failed to get TPBLK_NEXT %d", tret);
0318    
0319     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(0,TPBLK_ALL), "Failed to set TPBLK_ALL");
0320     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(0,TPBLK_NEXT), "Failed to set TPBLK_NEXT)");
0321 
0322     NDRX_ASSERT_TP_OUT(0==(tret=tpgblktime(TPBLK_ALL)), "Failed to get TPBLK_ALL %d", tret);
0323     NDRX_ASSERT_TP_OUT(0==(tret=tpgblktime(TPBLK_NEXT)), "Failed to get TPBLK_NEXT %d", tret);
0324  
0325     
0326     /* set next call thread specific tout ... */
0327     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(3,TPBLK_ALL), "Failed to set TPBLK_ALL");
0328     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(4,TPBLK_NEXT), "Failed to set TPBLK_NEXT)");
0329 
0330     NDRX_ASSERT_TP_OUT(3==(tret=tpgblktime(TPBLK_ALL)), "Failed to get TPBLK_ALL %d", tret);
0331     NDRX_ASSERT_TP_OUT(4==(tret=tpgblktime(TPBLK_NEXT)), "Failed to get TPBLK_NEXT %d", tret);
0332     NDRX_ASSERT_TP_OUT(4==(tret=tpgblktime(0)), "Failed to get 0 blktime %d", tret);
0333 
0334     /* save ctx 1 */
0335     NDRX_ASSERT_TP_OUT(EXFAIL!=tpgetctxt(&context, 0), "Failed to get context");
0336 
0337     /* check new context 2 */
0338     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpinit(NULL), "failed to init ctxt 2");
0339     NDRX_ASSERT_TP_OUT(0==(tret=tpgblktime(TPBLK_ALL)), "Failed to get TPBLK_ALL %d / ctxt2", tret);
0340     NDRX_ASSERT_TP_OUT(0==(tret=tpgblktime(TPBLK_NEXT)), "Failed to get TPBLK_NEXT %d / ctxt2", tret);
0341     NDRX_ASSERT_TP_OUT(11==(tret=tpgblktime(0)), "Failed to get 0 blktime %d / ctxt2", tret);
0342     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpterm(), "failed to term ctxt 2");
0343 
0344     /* free ctxt 2*/
0345     NDRX_ASSERT_TP_OUT(EXFAIL!=tpgetctxt(&ctxt2, 0), "Failed to get context 2");
0346     tpfreectxt(ctxt2);
0347 
0348     /* restore org context */
0349     NDRX_ASSERT_TP_OUT(EXFAIL!=tpsetctxt(context, 0), "Failed to set context");
0350     NDRX_ASSERT_TP_OUT(3==(tret=tpgblktime(TPBLK_ALL)), "Failed to get TPBLK_ALL %d", tret);
0351     NDRX_ASSERT_TP_OUT(4==(tret=tpgblktime(TPBLK_NEXT)), "Failed to get TPBLK_NEXT %d", tret);
0352     NDRX_ASSERT_TP_OUT(4==(tret=tpgblktime(0)), "Failed to get 0 blktime %d", tret);
0353 
0354     /* validate errors: */
0355     NDRX_ASSERT_TP_OUT(EXFAIL==tpsblktime(-1,TPBLK_ALL) && TPEINVAL==tperrno, "Expected TPEINVAL");
0356     NDRX_ASSERT_TP_OUT(EXFAIL==tpsblktime(-1,9999) && TPEINVAL==tperrno, "Expected TPEINVAL");
0357     NDRX_ASSERT_TP_OUT(EXFAIL==tpgblktime(9999) && TPEINVAL==tperrno, "Expected TPEINVAL");
0358 
0359     
0360 
0361     NDRX_LOG(log_debug, "running of the case");
0362     
0363     
0364     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(99,TPBLK_ALL), "Failed to set TPBLK_ALL");
0365     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(99,TPBLK_NEXT), "Failed to set TPBLK_NEXT)");
0366     
0367             
0368     NDRX_ASSERT_VAL_OUT(EXSUCCEED==call_tests(), "Call tests failed");
0369     
0370 
0371     /* block the service */
0372     
0373     NDRX_ASSERT_VAL_OUT(EXSUCCEED==full_load(1), "Failed to active blocked service");
0374     
0375     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(5,TPBLK_ALL), "Failed to set TPBLK_ALL");
0376     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(2,TPBLK_NEXT), "Failed to set TPBLK_NEXT");
0377     NDRX_ASSERT_VAL_OUT(EXSUCCEED==chk_tpcall(2), "tout failed");
0378     /* next is reset */
0379     NDRX_ASSERT_VAL_OUT(EXSUCCEED==chk_tpcall(5), "tout failed");
0380    
0381     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(0,TPBLK_ALL), "Failed to reset TPBLK_ALL");
0382     NDRX_ASSERT_TP_OUT(EXSUCCEED==tptoutset(9), "Failed to default tout");
0383     NDRX_ASSERT_VAL_OUT(EXSUCCEED==chk_tpcall(9), "tout failed (default)");
0384    
0385     
0386 #ifdef EX_USE_EPOLL
0387     
0388     /* block the dest service / we get full Q */
0389     while (EXSUCCEED==full_load(1)){};
0390     
0391     /* now lets test caller send timtouts */
0392     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(5,TPBLK_ALL), "Failed to set TPBLK_ALL");
0393     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(2,TPBLK_NEXT), "Failed to set TPBLK_NEXT");
0394     NDRX_ASSERT_VAL_OUT(EXSUCCEED==chk_tpcall(2), "tout failed");
0395     /* next is reset */
0396     NDRX_ASSERT_VAL_OUT(EXSUCCEED==chk_tpcall(5), "tout failed");
0397    
0398     NDRX_ASSERT_TP_OUT(EXSUCCEED==tpsblktime(0,TPBLK_ALL), "Failed to reset TPBLK_ALL");
0399     NDRX_ASSERT_TP_OUT(EXSUCCEED==tptoutset(9), "Failed to default tout");
0400     NDRX_ASSERT_VAL_OUT(EXSUCCEED==chk_tpcall(9), "tout failed (default)");
0401     
0402 #endif
0403     
0404 out:
0405     tpterm();
0406     fprintf(stderr, "Exit with %d\n", ret);
0407 
0408     return ret;
0409 }
0410 /* vim: set ts=4 sw=4 et smartindent: */