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  * test ndrx_fadvise_donotneed
0056  */
0057 Ensure(test_nstd_fadvise)
0058 {
0059     FILE *f=NULL;
0060     char filename[]="/tmp/ubf-test-XXXXXX";
0061     char buf[128];
0062     assert_not_equal(mkstemp(filename), EXFAIL);
0063     assert_not_equal((f=fopen(filename, "w")), NULL);
0064 
0065     assert_equal(ndrx_fadvise_donotneed(fileno(f), 0), EXSUCCEED);
0066 
0067     /* clean-up... */
0068     fclose(f);
0069     unlink(filename);
0070 }
0071 
0072 /**
0073  * Basic fsync tests
0074  */
0075 Ensure(test_nstd_fsync)
0076 {
0077     long flags=0;
0078     FILE *f=NULL;
0079     char filename[]="/tmp/ubf-test-XXXXXX";
0080     char buf[128];
0081     assert_not_equal(mkstemp(filename), EXFAIL);
0082     assert_not_equal((f=fopen(filename, "w")), NULL);
0083     
0084     /* should parse OK */
0085     NDRX_STRCPY_SAFE(buf, "fsync,nosync");
0086     assert_equal(ndrx_fsync_parse(buf, &flags), EXFAIL);
0087     
0088     /* parse fail */
0089     NDRX_STRCPY_SAFE(buf, "fdatasync,nosync");
0090     assert_equal(ndrx_fsync_parse(buf, &flags), EXFAIL);
0091 
0092     assert_equal(ndrx_fsync_fsync(f, flags), EXSUCCEED);
0093     assert_equal(ndrx_fsync_fsync(NULL, flags), EXFAIL);
0094 
0095     NDRX_STRCPY_SAFE(buf, "fsync,dsync,fdatasync");
0096     assert_equal(ndrx_fsync_parse(buf, &flags), EXSUCCEED);
0097     assert_equal(!!(flags & NDRX_FSYNC_FSYNC), EXTRUE);
0098     assert_equal(!!(flags & NDRX_FSYNC_FDATASYNC), EXTRUE);
0099     assert_equal(!!(flags & NDRX_FSYNC_DSYNC), EXTRUE);
0100 
0101     flags=0;
0102     NDRX_STRCPY_SAFE(buf, "fsync,dsync,fdatasync");
0103     assert_equal(ndrx_fsync_parse(buf, &flags), EXSUCCEED);
0104     assert_equal(!!(flags & NDRX_FSYNC_FSYNC), EXTRUE);
0105     assert_equal(!!(flags & NDRX_FSYNC_FDATASYNC), EXTRUE);
0106     assert_equal(!!(flags & NDRX_FSYNC_DSYNC), EXTRUE);
0107 
0108     assert_equal(ndrx_fsync_fsync(f, flags), EXSUCCEED);
0109     assert_equal(ndrx_fsync_fsync(NULL, flags), EXFAIL);
0110 
0111     /* clean-up... */
0112     fclose(f);
0113     unlink(filename);
0114 }
0115 
0116 /**
0117  * Basic directory-sync tests
0118  */
0119 Ensure(test_nstd_dsync)
0120 {
0121     ndrx_stopwatch_t w;
0122     long cnt=0, err=EXSUCCEED;
0123 
0124     assert_equal(ndrx_fsync_dsync("/tmp/", NDRX_FSYNC_DSYNC), EXSUCCEED);
0125     assert_equal(ndrx_fsync_dsync("/no/such/directory/endurox", NDRX_FSYNC_DSYNC), EXFAIL);
0126     assert_equal(ndrx_fsync_dsync("/no/such/directory/endurox", 0), EXSUCCEED);
0127 
0128     ndrx_stopwatch_reset(&w);
0129     do
0130     {
0131         if (ndrx_fsync_dsync("/tmp", NDRX_FSYNC_DSYNC)!=EXSUCCEED)
0132         {
0133             err=EXFAIL;
0134             break;
0135         }
0136 
0137         cnt++;
0138     } while (ndrx_stopwatch_get_delta_sec(&w) < 1);
0139 
0140     assert_equal(err, EXSUCCEED);
0141     fprintf(stderr, "dsync per second: %ld\n", cnt);
0142 
0143 }
0144 
0145 /**
0146  * Standard library tests
0147  * @return
0148  */
0149 TestSuite *ubf_nstd_fsync(void)
0150 {
0151     TestSuite *suite = create_test_suite();
0152 
0153     add_test(suite, test_nstd_fsync);
0154     add_test(suite, test_nstd_dsync);
0155     add_test(suite, test_nstd_fadvise);
0156 
0157     return suite;
0158 }
0159 
0160 /* vim: set ts=4 sw=4 et smartindent: */