2012-10-12 19:44:18 -07:00
|
|
|
// Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice appear in all copies.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
// AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
// PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef TEST_DATASRC_CLIENTS_MGR_H
|
|
|
|
#define TEST_DATASRC_CLIENTS_MGR_H 1
|
|
|
|
|
|
|
|
#include <auth/datasrc_clients_mgr.h>
|
|
|
|
|
|
|
|
#include <boost/function.hpp>
|
|
|
|
|
|
|
|
#include <list>
|
|
|
|
|
|
|
|
// Below we extend the isc::auth::internal namespace to specialize
|
|
|
|
// the doNoop() method.
|
|
|
|
namespace isc {
|
|
|
|
namespace auth {
|
|
|
|
namespace internal {
|
|
|
|
class TestMutex {
|
|
|
|
public:
|
|
|
|
TestMutex() : lock_count(0), unlock_count(0), noop_count(0) {}
|
|
|
|
class Locker {
|
|
|
|
public:
|
|
|
|
Locker(TestMutex& mutex) : mutex_(mutex) {
|
|
|
|
++mutex.lock_count;
|
|
|
|
}
|
|
|
|
~Locker() {
|
|
|
|
++mutex_.unlock_count;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
TestMutex& mutex_;
|
|
|
|
};
|
|
|
|
size_t lock_count;
|
|
|
|
size_t unlock_count;
|
|
|
|
size_t noop_count; // allow doNoop() to modify this
|
|
|
|
};
|
|
|
|
|
|
|
|
class TestCondVar {
|
|
|
|
public:
|
2012-10-15 15:06:21 -07:00
|
|
|
TestCondVar() : wait_count(0), signal_count(0), command_queue_(NULL),
|
2012-10-15 14:30:11 -07:00
|
|
|
delayed_command_queue_(NULL)
|
|
|
|
{}
|
2012-10-12 19:44:18 -07:00
|
|
|
TestCondVar(std::list<Command>& command_queue,
|
|
|
|
std::list<Command>& delayed_command_queue) :
|
|
|
|
wait_count(0),
|
2012-10-15 14:30:11 -07:00
|
|
|
command_queue_(&command_queue),
|
|
|
|
delayed_command_queue_(&delayed_command_queue)
|
2012-10-12 19:44:18 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
void wait(TestMutex& mutex) {
|
|
|
|
// bookkeeping
|
|
|
|
++mutex.unlock_count;
|
|
|
|
++wait_count;
|
|
|
|
++mutex.lock_count;
|
|
|
|
|
|
|
|
// make the delayed commands available
|
2012-10-15 14:30:11 -07:00
|
|
|
command_queue_->splice(command_queue_->end(), *delayed_command_queue_);
|
2012-10-12 19:44:18 -07:00
|
|
|
}
|
2012-10-15 15:06:21 -07:00
|
|
|
void signal() {
|
|
|
|
++signal_count;
|
|
|
|
}
|
2012-10-12 19:44:18 -07:00
|
|
|
size_t wait_count;
|
2012-10-15 15:06:21 -07:00
|
|
|
size_t signal_count;
|
2012-10-12 19:44:18 -07:00
|
|
|
private:
|
2012-10-15 14:30:11 -07:00
|
|
|
std::list<Command>* command_queue_;
|
|
|
|
std::list<Command>* delayed_command_queue_;
|
2012-10-12 19:44:18 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Convenient shortcut
|
|
|
|
typedef DataSrcClientsBuilderBase<TestMutex, TestCondVar>
|
|
|
|
TestDataSrcClientsBuilder;
|
|
|
|
|
|
|
|
// We specialize this command handler for the convenience of tests.
|
|
|
|
// It abuses our specialized Mutex to count the number of calls of this method.
|
|
|
|
template<>
|
|
|
|
void
|
|
|
|
TestDataSrcClientsBuilder::doNoop();
|
|
|
|
|
|
|
|
} // namespace internal
|
2012-10-15 14:30:11 -07:00
|
|
|
|
|
|
|
// A specialization of DataSrcClientsBuilder that allows tests to inspect
|
|
|
|
// its internal states via static class variables. Using static is suboptimal,
|
|
|
|
// but DataSrcClientsMgr is highly encapsulated, this seems to be the best
|
|
|
|
// possible compromise.
|
|
|
|
class FakeDataSrcClientsBuilder {
|
|
|
|
public:
|
|
|
|
FakeDataSrcClientsBuilder(std::list<internal::Command>* command_queue,
|
|
|
|
internal::TestCondVar* cond,
|
|
|
|
internal::TestMutex* queue_mutex)
|
|
|
|
{
|
|
|
|
FakeDataSrcClientsBuilder::started = false;
|
|
|
|
FakeDataSrcClientsBuilder::command_queue = command_queue;
|
|
|
|
FakeDataSrcClientsBuilder::cond = cond;
|
|
|
|
FakeDataSrcClientsBuilder::queue_mutex = queue_mutex;
|
2012-10-15 15:06:21 -07:00
|
|
|
FakeDataSrcClientsBuilder::thread_waited = false;
|
2012-10-15 14:30:11 -07:00
|
|
|
}
|
|
|
|
void run() {
|
|
|
|
FakeDataSrcClientsBuilder::started = true;
|
|
|
|
}
|
|
|
|
|
2012-10-15 15:06:21 -07:00
|
|
|
// true iff a builder has started.
|
2012-10-15 14:30:11 -07:00
|
|
|
static bool started;
|
2012-10-15 15:06:21 -07:00
|
|
|
|
|
|
|
// These three correspond to the resource shared with the manager.
|
2012-10-15 14:30:11 -07:00
|
|
|
static std::list<internal::Command>* command_queue;
|
|
|
|
static internal::TestCondVar* cond;
|
|
|
|
static internal::TestMutex* queue_mutex;
|
2012-10-15 15:06:21 -07:00
|
|
|
|
|
|
|
// true iff the manager waited on the thread running the builder.
|
|
|
|
static bool thread_waited;
|
2012-10-15 14:30:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
class TestThread {
|
|
|
|
public:
|
|
|
|
TestThread(const boost::function<void()>& main) {
|
|
|
|
main();
|
|
|
|
}
|
2012-10-15 15:06:21 -07:00
|
|
|
void wait() {
|
|
|
|
FakeDataSrcClientsBuilder::thread_waited = true;
|
|
|
|
}
|
2012-10-15 14:30:11 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Convenient shortcut
|
|
|
|
typedef DataSrcClientsMgrBase<TestThread, FakeDataSrcClientsBuilder,
|
|
|
|
internal::TestMutex, internal::TestCondVar>
|
|
|
|
TestDataSrcClientsMgr;
|
|
|
|
|
|
|
|
} // namespace auth
|
|
|
|
} // namespace isc
|
2012-10-12 19:44:18 -07:00
|
|
|
|
|
|
|
#endif // TEST_DATASRC_CLIENTS_MGR_H
|
|
|
|
|
|
|
|
// Local Variables:
|
|
|
|
// mode: c++
|
|
|
|
// End:
|