summaryrefslogtreecommitdiffstats
path: root/src/lib/asiolink/tests/io_service_mgr_unittest.cc
blob: d180f668337db6e274f25257eba28291cd2bdc60 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (C) 2024 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 <asiolink/io_service_mgr.h>

#include <gtest/gtest.h>

using namespace isc::asiolink;

namespace {

TEST(IOServiceMgr, testIOServiceMgr) {
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 0);
    int one_io_callback_count = 0;
    auto one_f = [&one_io_callback_count] () {
        one_io_callback_count++;
    };
    int two_io_callback_count = 0;
    auto two_f = [&two_io_callback_count] () {
        two_io_callback_count++;
    };
    {
        IOServicePtr one_io_service(new IOService());
        one_io_service->post(one_f);

        IOServicePtr two_io_service(new IOService());
        two_io_service->post(two_f);

        IOServiceMgr::instance().pollIOServices();
        EXPECT_EQ(one_io_callback_count, 0);
        EXPECT_EQ(two_io_callback_count, 0);

        IOServiceMgr::instance().registerIOService(one_io_service);
        EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 1);

        IOServiceMgr::instance().registerIOService(two_io_service);
        EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 2);

        IOServiceMgr::instance().pollIOServices();
        EXPECT_EQ(one_io_callback_count, 1);
        EXPECT_EQ(two_io_callback_count, 1);
        one_io_service->post(one_f);
        two_io_service->post(two_f);
    }
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 2);
    IOServiceMgr::instance().pollIOServices();
    EXPECT_EQ(one_io_callback_count, 2);
    EXPECT_EQ(two_io_callback_count, 2);

    IOServiceMgr::instance().clearIOServices();
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 0);

    IOServicePtr one_io_service(new IOService());
    one_io_service->post(one_f);

    IOServicePtr two_io_service(new IOService());
    two_io_service->post(two_f);

    IOServiceMgr::instance().pollIOServices();
    EXPECT_EQ(one_io_callback_count, 2);
    EXPECT_EQ(two_io_callback_count, 2);

    IOServiceMgr::instance().registerIOService(one_io_service);
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 1);

    IOServiceMgr::instance().pollIOServices();
    EXPECT_EQ(one_io_callback_count, 3);
    EXPECT_EQ(two_io_callback_count, 2);
    one_io_service->post(one_f);
    two_io_service->post(two_f);

    IOServiceMgr::instance().registerIOService(two_io_service);
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 2);

    IOServiceMgr::instance().pollIOServices();
    EXPECT_EQ(one_io_callback_count, 4);
    EXPECT_EQ(two_io_callback_count, 4);
    one_io_service->post(one_f);
    two_io_service->post(two_f);

    IOServiceMgr::instance().unregisterIOService(one_io_service);
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 1);

    IOServiceMgr::instance().pollIOServices();
    EXPECT_EQ(one_io_callback_count, 4);
    EXPECT_EQ(two_io_callback_count, 5);
    one_io_service->post(one_f);
    two_io_service->post(two_f);

    IOServiceMgr::instance().unregisterIOService(two_io_service);
    EXPECT_EQ(IOServiceMgr::instance().getIOServiceCount(), 0);

    IOServiceMgr::instance().pollIOServices();
    EXPECT_EQ(one_io_callback_count, 4);
    EXPECT_EQ(two_io_callback_count, 5);
}

} // namespace