summaryrefslogtreecommitdiffstats
path: root/src/lib/dhcpsrv/tests/allocation_state_unittest.cc
blob: 749f75763e9903746c07c315958845d8600e9f61 (plain)
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
67
68
69
70
71
72
73
74
75
76
77
// Copyright (C) 2022-2023 Internet Systems Consortium, Inc. ("ISC")
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#include <config.h>
#include <dhcpsrv/allocation_state.h>
#include <dhcpsrv/lease.h>
#include <testutils/multi_threading_utils.h>
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <gtest/gtest.h>

using namespace boost::gregorian;
using namespace boost::posix_time;
using namespace isc::dhcp;
using namespace isc::test;

namespace {

/// @brief Derivation of the @c SubnetAllocationState with protected
/// members and method exposed.
class NakedSubnetAllocationState : public SubnetAllocationState {
public:

    using SubnetAllocationState::setCurrentAllocatedTimeInternal;
    using SubnetAllocationState::last_allocated_time_;

};

// Tests initialization of the SubnetAllocationState.
TEST(SubnetAllocationState, constructor) {
    SubnetAllocationState state;
    EXPECT_TRUE(state.getLastAllocatedTime().is_neg_infinity());
}

// Tests that the explicitly initialized allocation time can be retrieved.
TEST(SubnetAllocationState, getLastAllocatedTime) {
    NakedSubnetAllocationState state;
    date gdate(greg_year(2002), greg_month(1), greg_day(20));

    time_duration t(22, 59, 59, 0);
    ptime pt = ptime(gdate, t);

    state.last_allocated_time_= pt;

    EXPECT_EQ(pt, state.getLastAllocatedTime());
}

// Tests that the explicitly initialized allocation time can be retrieved.
// Multi threaded variant.
TEST(SubnetAllocationState, getLastAllocatedTimeMultiThreading) {
    MultiThreadingTest mt(true);

    NakedSubnetAllocationState state;
    date gdate(greg_year(2002), greg_month(1), greg_day(20));

    time_duration t(22, 59, 59, 0);
    ptime pt = ptime(gdate, t);

    state.last_allocated_time_ = pt;

    EXPECT_EQ(pt, state.getLastAllocatedTime());
}

// Test that current allocation time is set.
TEST(SubnetAllocationState, setCurrentAllocatedTime) {
    NakedSubnetAllocationState state;

    state.setCurrentAllocatedTimeInternal();
    auto pt = state.getLastAllocatedTime();
    auto duration = pt - microsec_clock::universal_time();
    EXPECT_LT(duration.seconds(), 10);
}

} // end of anonymous namespace