1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "test.h"
40 
41 // create and close, making sure that everything is deallocated properly.
42 
43 #define LSIZE 100
44 #define NUM_LOGGERS 10
45 TOKULOGGER logger[NUM_LOGGERS];
46 
setup_logger(int which)47 static void setup_logger(int which) {
48     char logname[10];
49     snprintf(logname, sizeof(logname), "log%d", which);
50     char dnamewhich[TOKU_PATH_MAX+1];
51     int r;
52     toku_path_join(dnamewhich, 2, TOKU_TEST_FILENAME, logname);
53     r = toku_os_mkdir(dnamewhich, S_IRWXU);
54     if (r!=0) {
55         int er = get_error_errno();
56         printf("file %s error (%d) %s\n", dnamewhich, er, strerror(er));
57         assert(r==0);
58     }
59     r = toku_logger_create(&logger[which]);
60     assert(r == 0);
61     r = toku_logger_set_lg_max(logger[which], LSIZE);
62     {
63 	uint32_t n;
64 	r = toku_logger_get_lg_max(logger[which], &n);
65 	assert(n==LSIZE);
66     }
67     r = toku_logger_open(dnamewhich, logger[which]);
68     assert(r == 0);
69 }
70 
play_with_logger(int which)71 static void play_with_logger(int which) {
72     {
73 	ml_lock(&logger[which]->input_lock);
74 	int lsize=LSIZE-12-2;
75 	toku_logger_make_space_in_inbuf(logger[which], lsize);
76 	snprintf(logger[which]->inbuf.buf+logger[which]->inbuf.n_in_buf, lsize, "a%*d", lsize-1, 0);
77 	logger[which]->inbuf.n_in_buf += lsize;
78 	logger[which]->lsn.lsn++;
79 	logger[which]->inbuf.max_lsn_in_buf = logger[which]->lsn;
80 	ml_unlock(&logger[which]->input_lock);
81     }
82 
83     {
84 	ml_lock(&logger[which]->input_lock);
85 	toku_logger_make_space_in_inbuf(logger[which], 2);
86 	memcpy(logger[which]->inbuf.buf+logger[which]->inbuf.n_in_buf, "b1", 2);
87 	logger[which]->inbuf.n_in_buf += 2;
88 	logger[which]->lsn.lsn++;
89 	logger[which]->inbuf.max_lsn_in_buf = logger[which]->lsn;
90 	ml_unlock(&logger[which]->input_lock);
91     }
92 }
93 
tear_down_logger(int which)94 static void tear_down_logger(int which) {
95     int r;
96     r = toku_logger_close(&logger[which]);
97     assert(r == 0);
98 }
99 
100 int
test_main(int argc,const char * argv[])101 test_main (int argc __attribute__((__unused__)),
102 	  const char *argv[] __attribute__((__unused__))) {
103     int i;
104     int loop;
105     const int numloops = 100;
106     for (loop = 0; loop < numloops; loop++) {
107         toku_os_recursive_delete(TOKU_TEST_FILENAME);
108         int r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU);
109         assert_zero(r);
110         for (i = 0; i < NUM_LOGGERS; i++) setup_logger(i);
111         for (i = 0; i < NUM_LOGGERS; i++) play_with_logger(i);
112         for (i = 0; i < NUM_LOGGERS; i++) tear_down_logger(i);
113     }
114     toku_os_recursive_delete(TOKU_TEST_FILENAME);
115 
116     return 0;
117 }
118