aboutsummaryrefslogtreecommitdiff
path: root/src/test/timeoffsets_tests.cpp
blob: 008f1a9db9595910b9719794d49f4401a7e25238 (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
// Copyright (c) 2024-present The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
//

#include <node/timeoffsets.h>
#include <test/util/setup_common.h>

#include <boost/test/unit_test.hpp>

#include <chrono>
#include <vector>

using namespace std::chrono_literals;

static void AddMulti(TimeOffsets& offsets, const std::vector<std::chrono::seconds>& to_add)
{
    for (auto offset : to_add) {
        offsets.Add(offset);
    }
}

BOOST_FIXTURE_TEST_SUITE(timeoffsets_tests, BasicTestingSetup)

BOOST_AUTO_TEST_CASE(timeoffsets)
{
    TimeOffsets offsets{};
    BOOST_CHECK(offsets.Median() == 0s);

    AddMulti(offsets, {{0s, -1s, -2s, -3s}});
    // median should be zero for < 5 offsets
    BOOST_CHECK(offsets.Median() == 0s);

    offsets.Add(-4s);
    // we now have 5 offsets: [-4, -3, -2, -1, 0]
    BOOST_CHECK(offsets.Median() == -2s);

    AddMulti(offsets, {4, 5s});
    // we now have 9 offsets: [-4, -3, -2, -1, 0, 5, 5, 5, 5]
    BOOST_CHECK(offsets.Median() == 0s);

    AddMulti(offsets, {41, 10s});
    // the TimeOffsets is now at capacity with 50 offsets, oldest offsets is discarded for any additional offset
    BOOST_CHECK(offsets.Median() == 10s);

    AddMulti(offsets, {25, 15s});
    // we now have 25 offsets of 10s followed by 25 offsets of 15s
    BOOST_CHECK(offsets.Median() == 15s);
}

static bool IsWarningRaised(const std::vector<std::chrono::seconds>& check_offsets)
{
    TimeOffsets offsets{};
    AddMulti(offsets, check_offsets);
    return offsets.WarnIfOutOfSync();
}


BOOST_AUTO_TEST_CASE(timeoffsets_warning)
{
    BOOST_CHECK(IsWarningRaised({{-60min, -40min, -30min, 0min, 10min}}));
    BOOST_CHECK(IsWarningRaised({5, 11min}));

    BOOST_CHECK(!IsWarningRaised({4, 60min}));
    BOOST_CHECK(!IsWarningRaised({100, 3min}));
}


BOOST_AUTO_TEST_SUITE_END()