2014-12-18 18:50:47 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2014, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
2015-04-27 09:12:58 +00:00
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
2014-12-18 18:50:47 +00:00
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*
|
|
|
|
*/
|
2014-09-12 04:44:10 +00:00
|
|
|
|
2015-02-24 07:15:04 +00:00
|
|
|
#include <boost/make_shared.hpp>
|
|
|
|
|
2014-09-12 04:44:10 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
2015-02-19 23:19:00 +00:00
|
|
|
#include "osquery/dispatcher/dispatcher.h"
|
2014-09-12 04:44:10 +00:00
|
|
|
|
|
|
|
namespace osquery {
|
|
|
|
|
|
|
|
class DispatcherTests : public testing::Test {};
|
|
|
|
|
|
|
|
TEST_F(DispatcherTests, test_singleton) {
|
2015-05-04 03:02:01 +00:00
|
|
|
auto& one = Dispatcher::instance();
|
|
|
|
auto& two = Dispatcher::instance();
|
2014-09-30 02:06:33 +00:00
|
|
|
EXPECT_EQ(one.getThreadManager().get(), two.getThreadManager().get());
|
2014-09-12 04:44:10 +00:00
|
|
|
}
|
|
|
|
|
2015-02-04 03:55:16 +00:00
|
|
|
class TestRunnable : public InternalRunnable {
|
2014-09-12 04:44:10 +00:00
|
|
|
public:
|
|
|
|
int* i;
|
2015-02-24 11:47:12 +00:00
|
|
|
explicit TestRunnable(int* i) : i(i) {}
|
2015-05-06 00:09:07 +00:00
|
|
|
virtual void start() { ++*i; }
|
2014-09-12 04:44:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(DispatcherTests, test_add_work) {
|
2015-05-04 03:02:01 +00:00
|
|
|
auto& dispatcher = Dispatcher::instance();
|
2014-09-12 04:44:10 +00:00
|
|
|
int base = 5;
|
2014-09-13 21:18:54 +00:00
|
|
|
int repetitions = 1;
|
2014-09-12 04:44:10 +00:00
|
|
|
|
|
|
|
int i = base;
|
2014-09-13 21:18:54 +00:00
|
|
|
for (int c = 0; c < repetitions; ++c) {
|
2015-02-24 07:15:04 +00:00
|
|
|
dispatcher.add(OSQUERY_THRIFT_POINTER::make_shared<TestRunnable>(&i));
|
2014-09-12 04:44:10 +00:00
|
|
|
}
|
2014-09-30 02:06:33 +00:00
|
|
|
while (dispatcher.totalTaskCount() > 0) {
|
2014-09-12 04:44:10 +00:00
|
|
|
}
|
|
|
|
|
2014-09-13 21:18:54 +00:00
|
|
|
EXPECT_EQ(i, base + repetitions);
|
2014-09-12 04:44:10 +00:00
|
|
|
}
|
|
|
|
}
|