Back to home page

Enduro/X

 
 

    


0001 /**
0002  * @brief sys_fsync unit tests
0003  *
0004  * @file test_nstd_fsync.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 
0035 #include <stdio.h>
0036 #include <stdlib.h>
0037 #include <cgreen/cgreen.h>
0038 #include <ubf.h>
0039 #include <ndrstandard.h>
0040 #include <nstopwatch.h>
0041 #include <string.h>
0042 #include <ndebug.h>
0043 #include <exbase64.h>
0044 #include <nstdutil.h>
0045 
0046 /*---------------------------Externs------------------------------------*/
0047 /*---------------------------Macros-------------------------------------*/
0048 /*---------------------------Enums--------------------------------------*/
0049 /*---------------------------Typedefs-----------------------------------*/
0050 /*---------------------------Globals------------------------------------*/
0051 /*---------------------------Statics------------------------------------*/
0052 /*---------------------------Prototypes---------------------------------*/
0053 
0054 
0055 /**
0056  * Basic fsync tests
0057  */
0058 Ensure(test_nstd_fsync)
0059 {
0060     long flags=0;
0061     FILE *f=NULL;
0062     char filename[]="/tmp/ubf-test-XXXXXX";
0063     char buf[128];
0064     assert_not_equal(mkstemp(filename), EXFAIL);
0065     assert_not_equal((f=fopen(filename, "w")), NULL);
0066     
0067     /* should parse OK */
0068     NDRX_STRCPY_SAFE(buf, "fsync,nosync");
0069     assert_equal(ndrx_fsync_parse(buf, &flags), EXFAIL);
0070     
0071     /* parse fail */
0072     NDRX_STRCPY_SAFE(buf, "fdatasync,nosync");
0073     assert_equal(ndrx_fsync_parse(buf, &flags), EXFAIL);
0074 
0075     assert_equal(ndrx_fsync_fsync(f, flags), EXSUCCEED);
0076     assert_equal(ndrx_fsync_fsync(NULL, flags), EXFAIL);
0077 
0078     NDRX_STRCPY_SAFE(buf, "fsync,dsync,fdatasync");
0079     assert_equal(ndrx_fsync_parse(buf, &flags), EXSUCCEED);
0080     assert_equal(!!(flags & NDRX_FSYNC_FSYNC), EXTRUE);
0081     assert_equal(!!(flags & NDRX_FSYNC_FDATASYNC), EXTRUE);
0082     assert_equal(!!(flags & NDRX_FSYNC_DSYNC), EXTRUE);
0083 
0084     flags=0;
0085     NDRX_STRCPY_SAFE(buf, "fsync,dsync,fdatasync");
0086     assert_equal(ndrx_fsync_parse(buf, &flags), EXSUCCEED);
0087     assert_equal(!!(flags & NDRX_FSYNC_FSYNC), EXTRUE);
0088     assert_equal(!!(flags & NDRX_FSYNC_FDATASYNC), EXTRUE);
0089     assert_equal(!!(flags & NDRX_FSYNC_DSYNC), EXTRUE);
0090 
0091 
0092     assert_equal(ndrx_fsync_fsync(f, flags), EXSUCCEED);
0093     assert_equal(ndrx_fsync_fsync(NULL, flags), EXFAIL);
0094     
0095     /* clean-up... */
0096     fclose(f);
0097     unlink(filename);
0098 }
0099 
0100 /**
0101  * Basic directory-sync tests
0102  */
0103 Ensure(test_nstd_dsync)
0104 {
0105     ndrx_stopwatch_t w;
0106     long cnt=0, err=EXSUCCEED;
0107 
0108     assert_equal(ndrx_fsync_dsync("/tmp/", NDRX_FSYNC_DSYNC), EXSUCCEED);
0109     assert_equal(ndrx_fsync_dsync("/no/such/directory/endurox", NDRX_FSYNC_DSYNC), EXFAIL);
0110     assert_equal(ndrx_fsync_dsync("/no/such/directory/endurox", 0), EXSUCCEED);
0111 
0112     ndrx_stopwatch_reset(&w);
0113     do
0114     {
0115         if (ndrx_fsync_dsync("/tmp", NDRX_FSYNC_DSYNC)!=EXSUCCEED)
0116         {
0117             err=EXFAIL;
0118             break;
0119         }
0120 
0121         cnt++;
0122     } while (ndrx_stopwatch_get_delta_sec(&w) < 1);
0123 
0124     assert_equal(err, EXSUCCEED);
0125     fprintf(stderr, "dsync per second: %ld\n", cnt);
0126 
0127 }
0128 
0129 /**
0130  * Standard library tests
0131  * @return
0132  */
0133 TestSuite *ubf_nstd_fsync(void)
0134 {
0135     TestSuite *suite = create_test_suite();
0136 
0137     add_test(suite, test_nstd_fsync);
0138     add_test(suite, test_nstd_dsync);
0139 
0140     return suite;
0141 }
0142 
0143 /* vim: set ts=4 sw=4 et smartindent: */