1 // Copyright (C) 2016-2020 Internet Systems Consortium, Inc. ("ISC")
2 //
3 // This Source Code Form is subject to the terms of the Mozilla Public
4 // License, v. 2.0. If a copy of the MPL was not distributed with this
5 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6
7 #include <config.h>
8 #include <agent/ca_cfg_mgr.h>
9 #include <agent/ca_process.h>
10 #include <asiolink/interval_timer.h>
11 #include <asiolink/io_service.h>
12 #include <process/testutils/d_test_stubs.h>
13 #include <boost/date_time/posix_time/posix_time.hpp>
14 #include <gtest/gtest.h>
15 #include <functional>
16
17 using namespace boost::posix_time;
18 using namespace isc;
19 using namespace isc::agent;
20 using namespace isc::asiolink;
21 using namespace isc::process;
22
23 namespace {
24
25 /// @brief CtrlAgentProcess test fixture class.
26 class CtrlAgentProcessTest : public CtrlAgentProcess, public ::testing::Test {
27 public:
28 /// @brief Constructor
CtrlAgentProcessTest()29 CtrlAgentProcessTest() :
30 CtrlAgentProcess("agent-test",
31 IOServicePtr(new isc::asiolink::IOService())) {
32 CtrlAgentCfgContextPtr ctx = getCtrlAgentCfgMgr()->getCtrlAgentCfgContext();
33 ctx->setHttpHost("127.0.0.1");
34 ctx->setHttpPort(8081);
35 }
36
37 /// @brief Destructor
~CtrlAgentProcessTest()38 virtual ~CtrlAgentProcessTest() {
39 }
40
41 /// @brief Callback that will invoke shutdown method.
genShutdownCallback()42 void genShutdownCallback() {
43 shutdown(isc::data::ConstElementPtr());
44 }
45 };
46
47 // Test construction of the CtrlAgentProcess object.
TEST(CtrlAgentProcess,construction)48 TEST(CtrlAgentProcess, construction) {
49 // Verify that the constructor will fail if given an empty
50 // io service.
51 IOServicePtr lcl_io_service;
52 EXPECT_THROW(CtrlAgentProcess("TestProcess", lcl_io_service),
53 DProcessBaseError);
54
55 // Verify that the constructor succeeds with a valid io_service
56 lcl_io_service.reset(new IOService());
57 ASSERT_NO_THROW(CtrlAgentProcess("TestProcess", lcl_io_service));
58
59 // Verify tha the configuration is accessible after construction.
60 CtrlAgentProcess agent_process("TestProcess", lcl_io_service);
61 CtrlAgentCfgMgrPtr cfg_mgr = agent_process.getCtrlAgentCfgMgr();
62 ASSERT_TRUE(cfg_mgr);
63 }
64
65 // Verifies that en external call to shutdown causes the run method to
66 // exit gracefully.
TEST_F(CtrlAgentProcessTest,shutdown)67 TEST_F(CtrlAgentProcessTest, shutdown) {
68 // Use an asiolink IntervalTimer and callback to generate the
69 // shutdown invocation. (Note IntervalTimer setup is in milliseconds).
70 IntervalTimer timer(*getIoService());
71 timer.setup(std::bind(&CtrlAgentProcessTest::genShutdownCallback, this),
72 200);
73
74 // Record start time, and invoke run().
75 ptime start = microsec_clock::universal_time();
76 EXPECT_NO_THROW(run());
77
78 // Record stop time.
79 ptime stop = microsec_clock::universal_time();
80
81 // Verify that duration of the run invocation is the same as the
82 // timer duration. This demonstrates that the shutdown was driven
83 // by an io_service event and callback.
84 time_duration elapsed = stop - start;
85 EXPECT_TRUE(elapsed.total_milliseconds() >= 100 &&
86 elapsed.total_milliseconds() <= 400);
87 }
88
89
90 }
91