Merging bzr://gaz.tangent.org/libmemcached/build/ to Build branch
[awesomized/libmemcached] / libtest / framework.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * libtest
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 3 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 #include <config.h>
24 #include <libtest/common.h>
25 #include <iostream>
26
27 using namespace libtest;
28
29 static test_return_t _default_callback(void *p)
30 {
31 (void)p;
32
33 return TEST_SUCCESS;
34 }
35
36 static Runner defualt_runners;
37
38 Framework::Framework() :
39 collections(NULL),
40 _create(NULL),
41 _destroy(NULL),
42 collection_startup(_default_callback),
43 collection_shutdown(_default_callback),
44 _on_error(NULL),
45 _runner(NULL),
46 _socket(false),
47 _creators_ptr(NULL)
48 {
49 }
50
51 Framework::~Framework()
52 {
53 if (_destroy and _destroy(_creators_ptr))
54 {
55 Error << "Failure in _destroy(), some resources may not have been cleaned up.";
56 }
57
58 _servers.shutdown();
59 }
60
61 test_return_t Framework::Item::pre(void *arg)
62 {
63 if (pre_run)
64 {
65 return pre_run(arg);
66 }
67
68 return TEST_SUCCESS;
69 }
70
71 test_return_t Framework::Item::post(void *arg)
72 {
73 if (post_run)
74 {
75 return post_run(arg);
76 }
77
78 return TEST_SUCCESS;
79 }
80
81 test_return_t Framework::Item::flush(void* arg, test_st* run)
82 {
83 if (run->requires_flush and _flush)
84 {
85 return _flush(arg);
86 }
87
88 return TEST_SUCCESS;
89 }
90
91 test_return_t Framework::on_error(const test_return_t rc, void* arg)
92 {
93 if (_on_error and test_failed(_on_error(rc, arg)))
94 {
95 return TEST_FAILURE;
96 }
97
98 return TEST_SUCCESS;
99 }
100
101 test_return_t Framework::startup(void* arg)
102 {
103 if (collection_startup)
104 {
105 return collection_startup(arg);
106 }
107
108 return TEST_SUCCESS;
109 }
110
111 test_return_t Framework::Item::startup(void* arg)
112 {
113 if (_startup)
114 {
115 return _startup(arg);
116 }
117
118 return TEST_SUCCESS;
119 }
120
121 libtest::Runner *Framework::runner()
122 {
123 if (_runner == NULL)
124 {
125 _runner= &defualt_runners;
126 }
127 _runner->set_servers(_servers);
128
129 return _runner;
130 }
131
132 void* Framework::create(test_return_t& arg)
133 {
134 arg= TEST_SUCCESS;
135 if (_create)
136 {
137 return _creators_ptr= _create(_servers, arg);
138 }
139
140 return NULL;
141 }