testEvent.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9#include "squid.h"
10#include "base/AsyncCallQueue.h"
11#include "compat/cppunit.h"
12#include "event.h"
13#include "MemBuf.h"
14#include "unitTestMain.h"
15
16/*
17 * test the event module.
18 */
19
20class TestEvent : public CPPUNIT_NS::TestFixture
21{
30
31protected:
32 void testCreate();
33 void testDump();
34 void testFind();
35 void testCheckEvents();
36 void testSingleton();
37 void testCancel();
38};
39
41
42/*
43 * Test creating a Scheduler
44 */
45void
47{
48 EventScheduler scheduler = EventScheduler();
49}
50
53{
54public:
55 static void Handler(void *data) {
56 static_cast<CalledEvent *>(data)->calls++;
57 }
58
59 int calls = 0;
60};
61
62/* submit two callbacks, and cancel one, then dispatch and only the other should run.
63 */
64void
66{
67 EventScheduler scheduler;
68 CalledEvent event;
69 CalledEvent event_to_cancel;
70 scheduler.schedule("test event", CalledEvent::Handler, &event, 0, 0, false);
71 scheduler.schedule("test event2", CalledEvent::Handler, &event_to_cancel, 0, 0, false);
72 scheduler.cancel(CalledEvent::Handler, &event_to_cancel);
73 scheduler.checkEvents(0);
75 CPPUNIT_ASSERT_EQUAL(1, event.calls);
76 CPPUNIT_ASSERT_EQUAL(0, event_to_cancel.calls);
77}
78
79// submit two callbacks, and then dump the queue.
80void
82{
83 EventScheduler scheduler;
84 CalledEvent event;
85 CalledEvent event2;
86 const char *expected = "Last event to run: last event\n"
87 "\n"
88 "Operation \tNext Execution \tWeight\tCallback Valid?\n"
89 "test event \t0.000 sec\t 0\t N/A\n"
90 "test event2 \t0.000 sec\t 0\t N/A\n";
91 MemBuf expect;
92 expect.init();
93 expect.append(expected, strlen(expected));
94
95 scheduler.schedule("last event", CalledEvent::Handler, &event, 0, 0, false);
96
97 /* schedule and dispatch to set the last run event */
98 scheduler.checkEvents(0);
100 scheduler.schedule("test event", CalledEvent::Handler, &event, 0, 0, false);
101 scheduler.schedule("test event2", CalledEvent::Handler, &event2, 0, 0, false);
102
103 MemBuf result;
104 result.init();
105 scheduler.dump(&result);
106
107 /* loop over the strings, showing exactly where they differ (if at all) */
108 printf("Actual Text:\n");
109 /* TODO: these should really be just [] lookups, but String doesn't have those here yet. */
110 for (size_t i = 0; i < size_t(result.contentSize()); ++i) {
111 CPPUNIT_ASSERT(expect.content()[i]);
112 CPPUNIT_ASSERT(result.content()[i]);
113
114 /* slight hack to make special chars visible */
115 switch (result.content()[i]) {
116 case '\t':
117 printf("\\t");
118 break;
119 default:
120 printf("%c", result.content()[i]);
121 }
122 /* make this an int comparison, so that we can see the ASCII code at failure */
123 CPPUNIT_ASSERT_EQUAL(int(expect.content()[i]), int(result.content()[i]));
124 }
125 printf("\n");
126 CPPUNIT_ASSERT_EQUAL(expect.contentSize(), result.contentSize());
127 CPPUNIT_ASSERT(strcmp(expect.content(), result.content()) == 0);
128}
129
130/* submit two callbacks, and find the right one.
131 */
132void
134{
135 EventScheduler scheduler;
136 CalledEvent event;
137 CalledEvent event_to_find;
138 scheduler.schedule("test event", CalledEvent::Handler, &event, 0, 0, false);
139 scheduler.schedule("test event2", CalledEvent::Handler, &event_to_find, 0, 0, false);
140 CPPUNIT_ASSERT_EQUAL(true, scheduler.find(CalledEvent::Handler, &event_to_find));
141}
142
143/* do a trivial test of invoking callbacks */
144void
146{
147 EventScheduler scheduler;
148 CalledEvent event;
149 /* with no events, its an idle engine */
150 CPPUNIT_ASSERT_EQUAL(int(AsyncEngine::EVENT_IDLE), scheduler.checkEvents(0));
151 /* event running now gets will get sent to the dispatcher and the
152 * engine becomes idle.
153 */
154 scheduler.schedule("test event", CalledEvent::Handler, &event, 0, 0, false);
155 CPPUNIT_ASSERT_EQUAL(int(AsyncEngine::EVENT_IDLE), scheduler.checkEvents(0));
157 /* event running later results in a delay of the time till it runs */
158 scheduler.schedule("test event", CalledEvent::Handler, &event, 2, 0, false);
159 CPPUNIT_ASSERT_EQUAL(2000, scheduler.checkEvents(0));
161 CPPUNIT_ASSERT_EQUAL(1, event.calls);
162}
163
164/* for convenience we have a singleton scheduler */
165void
167{
168 EventScheduler *scheduler = dynamic_cast<EventScheduler *>(EventScheduler::GetInstance());
169 CPPUNIT_ASSERT(nullptr != scheduler);
170}
171
173class MyTestProgram: public TestProgram
174{
175public:
176 /* TestProgram API */
177 void startup() override { Mem::Init(); }
178};
179
180int
181main(int argc, char *argv[])
182{
183 return MyTestProgram().run(argc, argv);
184}
185
static AsyncCallQueue & Instance()
Helper for tests - an event which records the number of calls it received.
Definition: testEvent.cc:53
static void Handler(void *data)
Definition: testEvent.cc:55
static EventScheduler * GetInstance()
Definition: event.cc:294
bool find(EVH *func, void *arg)
Definition: event.cc:280
void schedule(const char *name, EVH *func, void *arg, double when, int weight, bool cbdata=true)
Definition: event.cc:300
void cancel(EVH *func, void *arg)
Definition: event.cc:161
void dump(Packable *)
Definition: event.cc:261
int checkEvents(int timeout) override
Definition: event.cc:216
Definition: MemBuf.h:24
void append(const char *c, int sz) override
Definition: MemBuf.cc:209
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:93
char * content()
start of the added data
Definition: MemBuf.h:41
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
customizes our test setup
void startup() override
Definition: testEvent.cc:177
void testSingleton()
Definition: testEvent.cc:166
CPPUNIT_TEST(testCancel)
void testCheckEvents()
Definition: testEvent.cc:145
void testFind()
Definition: testEvent.cc:133
CPPUNIT_TEST(testCreate)
CPPUNIT_TEST_SUITE(TestEvent)
CPPUNIT_TEST(testSingleton)
void testCreate()
Definition: testEvent.cc:46
CPPUNIT_TEST(testFind)
CPPUNIT_TEST_SUITE_END()
void testDump()
Definition: testEvent.cc:81
CPPUNIT_TEST(testDump)
CPPUNIT_TEST(testCheckEvents)
void testCancel()
Definition: testEvent.cc:65
implements test program's main() function while enabling customization
Definition: unitTestMain.h:26
int run(int argc, char *argv[])
Definition: unitTestMain.h:44
void Init()
Definition: old_api.cc:425
int const char size_t
Definition: stub_liblog.cc:83
int main(int argc, char *argv[])
Definition: testEvent.cc:181
CPPUNIT_TEST_SUITE_REGISTRATION(TestEvent)

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors