Bitcoin Core 28.99.0
P2P Digital Currency
raii_event_tests.cpp
Go to the documentation of this file.
1// Copyright (c) 2016-2022 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <event2/event.h>
6
7#include <cstdlib>
8#include <map>
9
10#include <support/events.h>
11
13
14#include <boost/test/unit_test.hpp>
15
17
18#ifdef EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
19
20static std::map<void*, short> tags;
21static std::map<void*, uint16_t> orders;
22static uint16_t tagSequence = 0;
23
24static void* tag_malloc(size_t sz) {
25 void* mem = malloc(sz);
26 if (!mem) return mem;
27 tags[mem]++;
28 orders[mem] = tagSequence++;
29 return mem;
30}
31
32static void tag_free(void* mem) {
33 tags[mem]--;
34 orders[mem] = tagSequence++;
35 free(mem);
36}
37
38BOOST_AUTO_TEST_CASE(raii_event_creation)
39{
40 event_set_mem_functions(tag_malloc, realloc, tag_free);
41
42 void* base_ptr = nullptr;
43 {
44 auto base = obtain_event_base();
45 base_ptr = (void*)base.get();
46 BOOST_CHECK(tags[base_ptr] == 1);
47 }
48 BOOST_CHECK(tags[base_ptr] == 0);
49
50 void* event_ptr = nullptr;
51 {
52 auto base = obtain_event_base();
53 auto event = obtain_event(base.get(), -1, 0, nullptr, nullptr);
54
55 base_ptr = (void*)base.get();
56 event_ptr = (void*)event.get();
57
58 BOOST_CHECK(tags[base_ptr] == 1);
59 BOOST_CHECK(tags[event_ptr] == 1);
60 }
61 BOOST_CHECK(tags[base_ptr] == 0);
62 BOOST_CHECK(tags[event_ptr] == 0);
63
64 event_set_mem_functions(malloc, realloc, free);
65}
66
67BOOST_AUTO_TEST_CASE(raii_event_order)
68{
69 event_set_mem_functions(tag_malloc, realloc, tag_free);
70
71 void* base_ptr = nullptr;
72 void* event_ptr = nullptr;
73 {
74 auto base = obtain_event_base();
75 auto event = obtain_event(base.get(), -1, 0, nullptr, nullptr);
76
77 base_ptr = (void*)base.get();
78 event_ptr = (void*)event.get();
79
80 // base should have allocated before event
81 BOOST_CHECK(orders[base_ptr] < orders[event_ptr]);
82 }
83 // base should be freed after event
84 BOOST_CHECK(orders[base_ptr] > orders[event_ptr]);
85
86 event_set_mem_functions(malloc, realloc, free);
87}
88
89#endif // EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
90
BOOST_AUTO_TEST_CASE(addrman_simple)
BOOST_FIXTURE_TEST_SUITE(cuckoocache_tests, BasicTestingSetup)
Test Suite for CuckooCache.
BOOST_AUTO_TEST_SUITE_END()
raii_event obtain_event(struct event_base *base, evutil_socket_t s, short events, event_callback_fn cb, void *arg)
Definition: events.h:37
raii_event_base obtain_event_base()
Definition: events.h:30
#define BOOST_CHECK(expr)
Definition: object.cpp:17
Basic testing setup.
Definition: setup_common.h:63