1 /** @file
2 
3   Catch based unit tests for EventSystem
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 #define CATCH_CONFIG_MAIN
25 #include "catch.hpp"
26 
27 #include "I_EventSystem.h"
28 #include "tscore/I_Layout.h"
29 #include "tscore/TSSystemState.h"
30 
31 #include "diags.i"
32 
33 #define TEST_TIME_SECOND 60
34 #define TEST_THREADS 2
35 
36 TEST_CASE("EventSystem", "[iocore]")
37 {
38   static int count;
39 
40   struct alarm_printer : public Continuation {
41     alarm_printer(ProxyMutex *m) : Continuation(m) { SET_HANDLER(&alarm_printer::dummy_function); }
42 
43     int
44     dummy_function(int /* event ATS_UNUSED */, Event * /* e ATS_UNUSED */)
45     {
46       ink_atomic_increment((int *)&count, 1);
47 
48       EThread *e = this_ethread();
49       printf("thread=%d (%p) count = %d\n", e->id, e, count);
50 
51       return 0;
52     }
53   };
54 
55   struct process_killer : public Continuation {
56     process_killer(ProxyMutex *m) : Continuation(m) { SET_HANDLER(&process_killer::kill_function); }
57 
58     int
59     kill_function(int /* event ATS_UNUSED */, Event * /* e ATS_UNUSED */)
60     {
61       EThread *e = this_ethread();
62       printf("thread=%d (%p) count is %d\n", e->id, e, count);
63 
64       REQUIRE(count > 0);
65       REQUIRE(count <= TEST_TIME_SECOND * TEST_THREADS);
66 
67       TSSystemState::shut_down_event_system();
68 
69       return 0;
70     }
71   };
72 
73   alarm_printer *alrm    = new alarm_printer(new_ProxyMutex());
74   process_killer *killer = new process_killer(new_ProxyMutex());
75   eventProcessor.schedule_in(killer, HRTIME_SECONDS(10));
76   eventProcessor.schedule_every(alrm, HRTIME_SECONDS(1));
77 
78   while (!TSSystemState::is_event_system_shut_down()) {
79     sleep(1);
80   }
81 }
82 
83 struct EventProcessorListener : Catch::TestEventListenerBase {
84   using TestEventListenerBase::TestEventListenerBase;
85 
86   void
87   testRunStarting(Catch::TestRunInfo const &testRunInfo) override
88   {
89     Layout::create();
90     init_diags("", nullptr);
91     RecProcessInit(RECM_STAND_ALONE);
92 
93     ink_event_system_init(EVENT_SYSTEM_MODULE_PUBLIC_VERSION);
94     eventProcessor.start(TEST_THREADS, 1048576); // Hardcoded stacksize at 1MB
95 
96     EThread *main_thread = new EThread;
97     main_thread->set_specific();
98   }
99 };
100 
101 CATCH_REGISTER_LISTENER(EventProcessorListener);
102