Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief Basic test client
0003  *
0004  * @file atmiclt31.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 #include <unistd.h>
0040 
0041 #include <atmi.h>
0042 #include <ubf.h>
0043 #include <ndebug.h>
0044 #include <ndebugcmn.h>
0045 #include <test.fd.h>
0046 #include <ndrstandard.h>
0047 #include <nstdutil.h>
0048 #include <exassert.h>
0049 #include <nstd_int.h>
0050 /*---------------------------Externs------------------------------------*/
0051 /*---------------------------Macros-------------------------------------*/
0052 /*---------------------------Enums--------------------------------------*/
0053 /*---------------------------Typedefs-----------------------------------*/
0054 /*---------------------------Globals------------------------------------*/
0055 /*---------------------------Statics------------------------------------*/
0056 /*---------------------------Prototypes---------------------------------*/
0057 
0058 
0059 void do_thread_work1 ( void *ptr )
0060 {
0061     /* 
0062      * Enduro/X logging 
0063      * But can be done at thread level
0064      */
0065     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_NDRX_THREAD|LOG_FACILITY_UBF_THREAD, 
0066             EXFAIL, "ndrx=5 ubf=0", "TEST", "./clt-endurox.log"))
0067     {
0068         NDRX_LOG(log_error, "TESTERROR: Failed to configure Enduro/X logger: %s", 
0069                 Nstrerror(Nerror));
0070         return;
0071     }
0072     
0073     /* write some stuff in user Enduro/X log */
0074     NDRX_LOG(log_error, "Hello from NDRX!");
0075     
0076     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_TP_THREAD, EXFAIL, "file=./clt-tp-th1.log tp=5", "TEST", NULL))
0077     {
0078         NDRX_LOG(log_error, "TESTERROR: Failed to configure user TP logger: %s", 
0079                 Nstrerror(Nerror));
0080     }
0081     else
0082     {
0083         sleep(1);
0084         tplog(5, "hello from thread 1");
0085     }
0086     tplogclosethread();
0087     tplog(5, "Thread 1 logs to main");
0088 }
0089 
0090 void do_thread_work2 ( void *ptr )
0091 {
0092     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_TP_THREAD, EXFAIL, "file=./clt-tp-th2.log tp=5", "TEST", NULL))
0093     {
0094         NDRX_LOG(log_error, "TESTERROR: Failed to configure user TP logger: %s", 
0095                 Nstrerror(Nerror));
0096     }
0097     else
0098     {
0099         sleep(1);
0100         tplog(5, "hello from thread 2");
0101     }
0102     tplogclosethread();
0103     tplog(5, "Thread 2 logs to main");
0104 }
0105 
0106 /**
0107  * Test the request file processing....
0108  * @return 
0109  */
0110 int test_request_file(void)
0111 {
0112     int ret = EXSUCCEED;
0113     UBFH *p_ub = (UBFH *)tpalloc("UBF", NULL, 8192);
0114     int i;
0115     long rsplen;
0116     char testfname[PATH_MAX+1];
0117     char testfname_should_be[PATH_MAX+1];
0118 
0119     if (EXSUCCEED==p_ub)
0120     {
0121         NDRX_LOG(log_error, "TESTERROR: Failed to allocate p_ub: %s", tpstrerror(tperrno));
0122         EXFAIL_OUT(ret);
0123     }
0124     
0125     for (i=0; i<1000; i++)
0126     {   
0127         /* let SETREQFILE service to set the filename... 
0128          * It shall close the previous file (thus if we have a problem there
0129          * will be memory leak of file descriptors...
0130          */
0131         if (EXSUCCEED!=tplogsetreqfile((char **)&p_ub, NULL, "SETREQFILE"))
0132         {
0133             NDRX_LOG(log_error, "TESTERROR: Failed to set request file:%s", 
0134                     tpstrerror(tperrno));
0135             EXFAIL_OUT(ret);
0136         }
0137         
0138         tplog(log_debug, "Hello from atmicl31!");
0139         
0140         /* test tploggetbufreqfile() must match the name we know */
0141         testfname[0] = EXEOS;
0142         
0143         if (EXSUCCEED!=tploggetbufreqfile((char *)p_ub, testfname, sizeof(testfname)))
0144         {
0145             NDRX_LOG(log_error, "TESTERROR: Failed to get current logger: %s", 
0146                     tpstrerror(tperrno));
0147             EXFAIL_OUT(ret);
0148         }
0149         
0150         snprintf(testfname_should_be, sizeof(testfname_should_be), 
0151                 "./logs/request_%d.log", i+1);
0152         
0153         TP_LOG(log_debug, "Request file should be [%s] got [%s]", 
0154                 testfname_should_be, testfname);
0155         
0156         if (0!=strcmp(testfname_should_be, testfname))
0157         {
0158             TP_LOG(log_error, "TESTERROR: Request file should be [%s] but got [%s]!!!", 
0159                 testfname_should_be, testfname);
0160             EXFAIL_OUT(ret);
0161         }
0162 
0163         /* Test tploggetreqfile() */
0164         
0165         if (!tploggetreqfile(testfname, sizeof(testfname)))
0166         {
0167             NDRX_LOG(log_error, "TESTERROR: Failed to get current request log file: %s", 
0168                     tpstrerror(tperrno));
0169             EXFAIL_OUT(ret);
0170         }
0171         
0172         if (0!=strcmp(testfname_should_be, testfname))
0173         {
0174             TP_LOG(log_error, "TESTERROR: Request file should be [%s] but got [%s]!!!", 
0175                 testfname_should_be, testfname);
0176             EXFAIL_OUT(ret);
0177         }
0178         
0179         /* Add some data to buffer */
0180         if (0==(i % 100))
0181         {
0182             if (EXSUCCEED!=Badd(p_ub, T_STRING_FLD, "HELLO WORLD!", 0L))
0183             {
0184                 NDRX_LOG(log_error, "TESTERROR: Failed to add T_STRING_FLD:%s", 
0185                     Bstrerror(Berror));
0186                 EXFAIL_OUT(ret);
0187             }
0188         }
0189         
0190         /* About to call service */
0191         tplog(log_warn, "Calling TEST31_1ST!");
0192         if (EXFAIL == tpcall("TEST31_1ST", (char *)p_ub, 0L, (char **)&p_ub, &rsplen,0))
0193         {
0194             NDRX_LOG(log_error, "TEST31_1ST failed: %s", tpstrerror(tperrno));
0195             EXFAIL_OUT(ret);
0196         }
0197         
0198         tplog(log_warn, "back from TEST31_1ST call!");     
0199         
0200         NDRX_LOG(log_always, "THIS IS NDRX IN REQLOG!");
0201         UBF_LOG(0, "THIS IS UBF IN REQLOG!");
0202         
0203         tplogprintubf(log_info, "Buffer before cleanup", p_ub);
0204         
0205         /* delete the request file from buffer */
0206         if (EXSUCCEED!=tplogdelbufreqfile((char *)p_ub))
0207         {
0208             NDRX_LOG(log_error, "tplogdelbufreqfile() failed: %s", tpstrerror(tperrno));
0209             EXFAIL_OUT(ret);
0210         }
0211         
0212         tplogprintubf(log_info, "Buffer after cleanup", p_ub);
0213     }
0214     
0215 out:
0216     /* close the logger
0217      * Now sutff should go to endurox and tp
0218      */
0219     tplogclosereqfile();
0220 
0221     NDRX_LOG(log_always, "THIS IS NDRX IN PROCLOG!");
0222     UBF_LOG(0, "THIS IS UBF IN PROCLOG!");
0223     TP_LOG(0, "THIS IS TP IN PROCLOG!");
0224         
0225 
0226     return ret;
0227     
0228 }
0229 
0230 /*
0231  * Do the test call to the server
0232  */
0233 int main(int argc, char** argv)
0234 {
0235     int ret = EXSUCCEED;
0236     int i;
0237     int sinks, refs;
0238     ndrx_debug_t *dbg = NULL;
0239     pthread_t thread1, thread2;  /* thread variables */
0240     
0241 #if 0
0242     - this does not work at process level, due to fact that logger sinks cannot be swapped
0243     - instead this can be done on thread logger bases
0244             
0245     /* 
0246      * Enduro/X logging 
0247      * ()
0248      */
0249     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_NDRX|LOG_FACILITY_UBF, 
0250             EXFAIL, "ndrx=5 ubf=0", "TEST", "./clt-endurox.log"))
0251     {
0252         NDRX_LOG(log_error, "TESTERROR: Failed to configure Enduro/X logger: %s", 
0253                 Nstrerror(Nerror));
0254         EXFAIL_OUT(ret);
0255     }
0256 #endif
0257     /*
0258      * User TP log 
0259      */
0260     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_TP, EXFAIL, "file=./clt-tp.log tp=6", "TEST", NULL))
0261     {
0262         NDRX_LOG(log_error, "TESTERROR: Failed to configure user TP logger: %s", 
0263                 Nstrerror(Nerror));
0264         EXFAIL_OUT(ret);
0265     }
0266     
0267     /* write some stuff in user log */
0268     tplog(log_error, "Hello from tp!");
0269 
0270     dbg = tplogfplock(-1, 0);
0271     fprintf(tplogfpget(dbg, 0), "Hello from fprintf\n");
0272     fflush(tplogfpget(dbg, 0));
0273     tplogfpunlock(dbg);
0274 
0275     dbg = tplogfplock(7, 0);
0276     NDRX_ASSERT_TP_OUT(NULL==dbg, "Got dbg, expected NULL");
0277     
0278     dbg = tplogfplock(2, 0);
0279     NDRX_ASSERT_TP_OUT(NULL!=dbg, "Got null dbg");
0280     tplogfpunlock(dbg);
0281 
0282 #if 0
0283     /* write some stuff in user Enduro/X log */
0284     NDRX_LOG(log_error, "Hello from NDRX!");
0285 #endif
0286     tplog(6, "hello from level 6");
0287     
0288     /* run off two threads... (each will have it's own log file) */
0289     pthread_create (&thread1, NULL, (void *) &do_thread_work1, NULL);
0290     pthread_create (&thread2, NULL, (void *) &do_thread_work2, NULL);
0291     
0292     pthread_join(thread1, NULL);
0293     pthread_join(thread2, NULL);
0294     
0295     tplog(6, "hello from main thread");
0296     
0297     if (EXSUCCEED!=test_request_file())
0298     {
0299         tplog(5, "TESTERROR: test_request_file() failed!");
0300     }
0301     
0302 #define NO_DIR_LOG "./non_exist/folder/is/missing/test.log"
0303     
0304     /* we do not have permissions to root */
0305 #define MKFAIL_DIR_LOG "./non_write/folder/is/missing/test.log"
0306     /* open new log file in non existing folder */
0307     
0308     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_TP, EXFAIL, NULL, 
0309             "TEST", NO_DIR_LOG))
0310     {
0311         NDRX_LOG(log_error, "TESTERROR: Failed to open log to missing folder: %s", 
0312                 Nstrerror(Nerror));
0313         EXFAIL_OUT(ret);
0314     }
0315 
0316     tplog(1, "Hello to missing folder");
0317 
0318     /* check that file exists */
0319     if (ndrx_file_exists(NO_DIR_LOG))
0320     {
0321         NDRX_LOG(log_error, "TESTERROR! [%s] must be missing!", NO_DIR_LOG);
0322         EXFAIL_OUT(ret);
0323     }
0324 
0325     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_TP, EXFAIL, "tp=6 mkdir=y",
0326             "TEST", NO_DIR_LOG))
0327     {
0328         NDRX_LOG(log_error, "TESTERROR: Failed to open log to missing folder: %s", 
0329                 Nstrerror(Nerror));
0330         EXFAIL_OUT(ret);
0331     }
0332 
0333     tplog(1, "Hello to missing folder");
0334 
0335     /* check that file exists */
0336     if (!ndrx_file_exists(NO_DIR_LOG))
0337     {
0338         NDRX_LOG(log_error, "TESTERROR! [%s] must be present!", NO_DIR_LOG);
0339         EXFAIL_OUT(ret);
0340     }
0341 
0342     /* check that fails to open log file */
0343     if (EXSUCCEED!=tplogconfig(LOG_FACILITY_TP, EXFAIL, "tp=6 mkdir=y",
0344             "TEST", MKFAIL_DIR_LOG))
0345     {
0346         NDRX_LOG(log_error, "TESTERROR: Failed to open log to non-write folder: %s [%s]", 
0347                 Nstrerror(Nerror), MKFAIL_DIR_LOG);
0348         EXFAIL_OUT(ret);
0349     }
0350 
0351     if (ndrx_file_exists(MKFAIL_DIR_LOG))
0352     {
0353         NDRX_LOG(log_error, "TESTERROR! [%s] must be missing!", MKFAIL_DIR_LOG);
0354         EXFAIL_OUT(ret);
0355     }
0356     
0357     tplogconfig(LOG_FACILITY_TP, EXFAIL, "file=./clt-tp.log tp=6", "TEST", NULL);
0358     
0359     
0360     /* Ensure only specific number loggers has left:
0361      * if we set all to 1 x file.
0362      * 
0363      *  */
0364     
0365     ndrx_debug_refcount(&sinks, &refs);
0366     
0367     /* stdout and process file */
0368     NDRX_ASSERT_TP_OUT((sinks==2), "There shall be only 2 process level sink, but got: %d", sinks);
0369     
0370     /* there shall be only 4 refs*/
0371     NDRX_ASSERT_TP_OUT((refs==4), "There shall be only 4 refs (ubf, stdout, tp, ndrx): got %d", refs);
0372     
0373 out:
0374 
0375     tplog(1, "Finishing off");
0376 
0377     return ret;
0378 }
0379 
0380 /* vim: set ts=4 sw=4 et smartindent: */