-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathworker_test.cc
More file actions
66 lines (50 loc) · 1.91 KB
/
worker_test.cc
File metadata and controls
66 lines (50 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <thread>
#include "external/envoy/source/common/common/random_generator.h"
#include "external/envoy/source/common/runtime/runtime_impl.h"
#include "external/envoy/source/common/stats/isolated_store_impl.h"
#include "external/envoy/test/mocks/local_info/mocks.h"
#include "external/envoy/test/mocks/protobuf/mocks.h"
#include "external/envoy/test/mocks/thread_local/mocks.h"
#include "source/common/worker_impl.h"
#include "gtest/gtest.h"
using namespace testing;
namespace Nighthawk {
class TestWorker : public WorkerImpl {
public:
TestWorker(Envoy::Api::Api& api, Envoy::ThreadLocal::Instance& tls)
: WorkerImpl(api, tls, test_store_), thread_id_(std::this_thread::get_id()) {}
void work() override {
EXPECT_NE(thread_id_, std::this_thread::get_id());
ran_ = true;
}
void shutdownThread() override {}
bool ran_{};
Envoy::Stats::IsolatedStoreImpl test_store_;
std::thread::id thread_id_;
};
class WorkerTest : public Test {
public:
WorkerTest() : api_(Envoy::Api::createApiForTest()) {}
Envoy::Api::ApiPtr api_;
Envoy::ThreadLocal::MockInstance tls_;
Envoy::Stats::IsolatedStoreImpl test_store_;
Envoy::Random::RandomGeneratorImpl rand_;
NiceMock<Envoy::LocalInfo::MockLocalInfo> local_info_;
NiceMock<Envoy::ProtobufMessage::MockValidationVisitor> validation_visitor_;
};
TEST_F(WorkerTest, WorkerExecutesOnThread) {
InSequence in_sequence;
EXPECT_CALL(tls_, registerThread(_, false));
EXPECT_CALL(tls_, allocateSlot());
TestWorker worker(*api_, tls_);
NiceMock<Envoy::Event::MockDispatcher> dispatcher;
absl::StatusOr<Envoy::Runtime::LoaderPtr> loader = Envoy::Runtime::LoaderImpl::create(
dispatcher, tls_, {}, local_info_, test_store_, rand_, validation_visitor_, *api_);
ASSERT_TRUE(loader.ok());
worker.start();
worker.waitForCompletion();
EXPECT_CALL(tls_, shutdownThread());
ASSERT_TRUE(worker.ran_);
worker.shutdown();
}
} // namespace Nighthawk