Import/Merge of libtest latest.
[m6w6/libmemcached] / libtest / framework.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Data Differential YATL (i.e. libtest) library
4 *
5 * Copyright (C) 2012 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <config.h>
38
39 #include <libtest/common.h>
40 #include <libtest/collection.h>
41 #include <libtest/signal.h>
42
43 #include <fnmatch.h>
44 #include <iostream>
45
46 using namespace libtest;
47
48 Framework::Framework(libtest::SignalThread& signal,
49 const std::string& only_run_arg,
50 const std::string& wildcard_arg) :
51 _collections(NULL),
52 _total(0),
53 _success(0),
54 _skipped(0),
55 _failed(0),
56 _create(NULL),
57 _destroy(NULL),
58 _on_error(NULL),
59 _runner(NULL),
60 _socket(false),
61 _creators_ptr(NULL),
62 _signal(signal),
63 _only_run(only_run_arg),
64 _wildcard(wildcard_arg)
65 {
66 get_world(this);
67
68 for (collection_st *next= _collections; next and next->name; next++)
69 {
70 _collection.push_back(new Collection(this, next));
71 }
72 }
73
74 Framework::~Framework()
75 {
76 if (_destroy and _destroy(_creators_ptr))
77 {
78 Error << "Failure in _destroy(), some resources may not have been cleaned up.";
79 }
80
81 _servers.shutdown();
82
83 delete _runner;
84
85 for (std::vector<Collection*>::iterator iter= _collection.begin();
86 iter != _collection.end();
87 ++iter)
88 {
89 delete *iter;
90 }
91 }
92
93 bool Framework::match(const char* arg)
94 {
95 if (_wildcard.empty() == false and fnmatch(_wildcard.c_str(), arg, 0))
96 {
97 return true;
98 }
99
100 return false;
101 }
102
103 void Framework::exec()
104 {
105 for (std::vector<Collection*>::iterator iter= _collection.begin();
106 iter != _collection.end() and (_signal.is_shutdown() == false);
107 ++iter)
108 {
109 if (_only_run.empty() == false and
110 fnmatch(_only_run.c_str(), (*iter)->name(), 0))
111 {
112 continue;
113 }
114
115 _total++;
116
117 try {
118 switch ((*iter)->exec())
119 {
120 case TEST_FAILURE:
121 _failed++;
122 break;
123
124 case TEST_SKIPPED:
125 _skipped++;
126 break;
127
128 // exec() can return SUCCESS, but that doesn't mean that some tests did
129 // not fail or get skipped.
130 case TEST_SUCCESS:
131 _success++;
132 break;
133 }
134 }
135 catch (libtest::fatal& e)
136 {
137 _failed++;
138 stream::cerr(e.file(), e.line(), e.func()) << e.mesg();
139 }
140 catch (libtest::disconnected& e)
141 {
142 _failed++;
143 Error << "Unhandled disconnection occurred:" << e.what();
144 throw;
145 }
146 catch (...)
147 {
148 _failed++;
149 throw;
150 }
151
152 Outn();
153 }
154 }
155
156 uint32_t Framework::sum_total()
157 {
158 uint32_t count= 0;
159 for (std::vector<Collection*>::iterator iter= _collection.begin();
160 iter != _collection.end();
161 ++iter)
162 {
163 count+= (*iter)->total();
164 }
165
166 return count;
167 }
168
169 uint32_t Framework::sum_success()
170 {
171 uint32_t count= 0;
172 for (std::vector<Collection*>::iterator iter= _collection.begin();
173 iter != _collection.end();
174 ++iter)
175 {
176 count+= (*iter)->success();
177 }
178
179 return count;
180 }
181
182 uint32_t Framework::sum_skipped()
183 {
184 uint32_t count= 0;
185 for (std::vector<Collection*>::iterator iter= _collection.begin();
186 iter != _collection.end();
187 ++iter)
188 {
189 count+= (*iter)->skipped();
190 }
191
192 return count;
193 }
194
195 uint32_t Framework::sum_failed()
196 {
197 uint32_t count= 0;
198 for (std::vector<Collection*>::iterator iter= _collection.begin();
199 iter != _collection.end();
200 ++iter)
201 {
202 count+= (*iter)->failed();
203 }
204
205 return count;
206 }
207
208 libtest::Runner *Framework::runner()
209 {
210 if (_runner == NULL)
211 {
212 _runner= new Runner;
213 }
214 _runner->set_servers(_servers);
215
216 return _runner;
217 }
218
219 test_return_t Framework::create()
220 {
221 test_return_t rc= TEST_SUCCESS;
222 if (_create)
223 {
224 _creators_ptr= _create(_servers, rc);
225 }
226
227 return rc;
228 }