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 static void
test_create(void)42 test_create (void) {
43 message_buffer msg_buffer;
44 msg_buffer.create();
45 msg_buffer.destroy();
46 }
47
buildkey(size_t len)48 static char *buildkey(size_t len) {
49 char *XMALLOC_N(len, k);
50 memset(k, 0, len);
51 return k;
52 }
53
buildval(size_t len)54 static char *buildval(size_t len) {
55 char *XMALLOC_N(len, v);
56 memset(v, ~len, len);
57 return v;
58 }
59
60 static void
test_enqueue(int n)61 test_enqueue(int n) {
62 MSN startmsn = ZERO_MSN;
63
64 message_buffer msg_buffer;
65 msg_buffer.create();
66
67 for (int i=0; i<n; i++) {
68 int thekeylen = i + 1;
69 int thevallen = i + 2;
70 char *thekey = buildkey(thekeylen);
71 char *theval = buildval(thevallen);
72 XIDS xids;
73 if (i == 0) {
74 xids = toku_xids_get_root_xids();
75 } else {
76 int r = toku_xids_create_child(toku_xids_get_root_xids(), &xids, (TXNID)i);
77 assert_zero(r);
78 }
79 MSN msn = next_dummymsn();
80 if (startmsn.msn == ZERO_MSN.msn)
81 startmsn = msn;
82 enum ft_msg_type type = (enum ft_msg_type) i;
83 DBT k, v;
84 ft_msg msg(toku_fill_dbt(&k, thekey, thekeylen), toku_fill_dbt(&v, theval, thevallen), type, msn, xids);
85 msg_buffer.enqueue(msg, true, nullptr);
86 toku_xids_destroy(&xids);
87 toku_free(thekey);
88 toku_free(theval);
89 }
90
91 struct checkit_fn {
92 MSN startmsn;
93 int verbose;
94 int i;
95 checkit_fn(MSN smsn, bool v)
96 : startmsn(smsn), verbose(v), i(0) {
97 }
98 int operator()(const ft_msg &msg, bool UU(is_fresh)) {
99 int thekeylen = i + 1;
100 int thevallen = i + 2;
101 char *thekey = buildkey(thekeylen);
102 char *theval = buildval(thevallen);
103
104 MSN msn = msg.msn();
105 enum ft_msg_type type = msg.type();
106 if (verbose) printf("checkit %d %d %" PRIu64 "\n", i, type, msn.msn);
107 assert(msn.msn == startmsn.msn + i);
108 assert((int) msg.kdbt()->size == thekeylen); assert(memcmp(msg.kdbt()->data, thekey, msg.kdbt()->size) == 0);
109 assert((int) msg.vdbt()->size == thevallen); assert(memcmp(msg.vdbt()->data, theval, msg.vdbt()->size) == 0);
110 assert(i % 256 == (int)type);
111 assert((TXNID)i == toku_xids_get_innermost_xid(msg.xids()));
112 i += 1;
113 toku_free(thekey);
114 toku_free(theval);
115 return 0;
116 }
117 } checkit(startmsn, verbose);
118 msg_buffer.iterate(checkit);
119 assert(checkit.i == n);
120
121 msg_buffer.destroy();
122 }
123
124 int
test_main(int argc,const char * argv[])125 test_main(int argc, const char *argv[]) {
126 default_parse_args(argc, argv);
127 initialize_dummymsn();
128 test_create();
129 test_enqueue(4);
130 test_enqueue(512);
131
132 return 0;
133 }
134