Skip to content

Commit

Permalink
Unit test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
gansm committed Aug 21, 2023
1 parent 55b4cee commit c546fba
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 17 deletions.
2 changes: 1 addition & 1 deletion final/eventloop/eventloop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ auto EventLoop::run() -> int

// private methods of EventLoop
//----------------------------------------------------------------------
inline void EventLoop::nonPollWaiting()
inline void EventLoop::nonPollWaiting() const
{
// Saves cpu time when polling fails
std::this_thread::sleep_for(std::chrono::milliseconds(1)); // Wait 1 ms
Expand Down
2 changes: 1 addition & 1 deletion final/eventloop/eventloop.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class EventLoop
static constexpr int WAIT_INDEFINITELY{-1};

// Methods
void nonPollWaiting();
void nonPollWaiting() const;
auto processNextEvents() -> bool;
void dispatcher (nfds_t, int);
void addMonitor (Monitor*);
Expand Down
33 changes: 18 additions & 15 deletions test/eventloop-monitor-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,19 +176,10 @@ void EventloopMonitorTest::classNameTest()
{
finalcut::EventLoop eloop{};
const finalcut::Monitor monitor(&eloop);
const finalcut::IoMonitor io_monitor(&eloop);
const finalcut::SignalMonitor signal_monitor(&eloop);
const finalcut::TimerMonitor timer_monitor(&eloop);
const finalcut::FString& eloop_classname = eloop.getClassName();
const finalcut::FString& monitor_classname = monitor.getClassName();
const finalcut::FString& io_monitor_classname = io_monitor.getClassName();
const finalcut::FString& signal_monitor_classname = signal_monitor.getClassName();
const finalcut::FString& timer_monitor_classname = timer_monitor.getClassName();
CPPUNIT_ASSERT ( eloop_classname == "EventLoop" );
CPPUNIT_ASSERT ( monitor_classname == "Monitor" );
CPPUNIT_ASSERT ( io_monitor_classname == "IoMonitor" );
CPPUNIT_ASSERT ( signal_monitor_classname == "SignalMonitor" );
CPPUNIT_ASSERT ( timer_monitor_classname == "TimerMonitor" );
}

//----------------------------------------------------------------------
Expand Down Expand Up @@ -247,6 +238,8 @@ void EventloopMonitorTest::eventLoopTest()
CPPUNIT_ASSERT ( mon.getFileDescriptor() == pipe_fd[0] );
CPPUNIT_ASSERT ( mon.getUserContext() == nullptr );
CPPUNIT_ASSERT ( mon.isActive() );
signal(SIGALRM, SIG_DFL);
signal_handler = [] (int) { }; // Do nothing
}

//----------------------------------------------------------------------
Expand Down Expand Up @@ -297,7 +290,9 @@ void EventloopMonitorTest::IoMonitorTest()
keyboard_input("A");
};
signal(SIGALRM, sigHandler); // Register signal handler
finalcut::IoMonitor stdin_monitor{&eloop};
finalcut::IoMonitor io_monitor{&eloop};
const finalcut::FString& io_monitor_classname = io_monitor.getClassName();
CPPUNIT_ASSERT ( io_monitor_classname == "IoMonitor" );
auto callback_handler = [&eloop] (const finalcut::Monitor* mon, short)
{
std::cout << "\nIoMonitor callback handle";
Expand All @@ -307,11 +302,13 @@ void EventloopMonitorTest::IoMonitorTest()
CPPUNIT_ASSERT ( buf == 'A' );
eloop.leave();
};
stdin_monitor.init (STDIN_FILENO, POLLIN, callback_handler, nullptr);
io_monitor.init (STDIN_FILENO, POLLIN, callback_handler, nullptr);
std::cout << "\n";
alarm(1); // Schedule a alarm after 1 seconds
stdin_monitor.resume();
io_monitor.resume();
CPPUNIT_ASSERT ( eloop.run() == 0 );
signal(SIGALRM, SIG_DFL);
signal_handler = [] (int) { }; // Do nothing
}

//----------------------------------------------------------------------
Expand All @@ -323,24 +320,30 @@ void EventloopMonitorTest::SignalMonitorTest()
std::raise(SIGABRT); // Send abort signal
};
signal(SIGALRM, sigHandler); // Register signal handler
finalcut::SignalMonitor sig_abrt_monitor{&eloop};
finalcut::SignalMonitor signal_monitor{&eloop};
const finalcut::FString& signal_monitor_classname = signal_monitor.getClassName();
CPPUNIT_ASSERT ( signal_monitor_classname == "SignalMonitor" );
auto callback_handler = [&eloop] (const finalcut::Monitor*, short)
{
std::cout << "SignalMonitor callback handle";
eloop.leave();
};
sig_abrt_monitor.init(SIGABRT, callback_handler, nullptr);
signal_monitor.init(SIGABRT, callback_handler, nullptr);
std::cout << "\n";
alarm(1); // Schedule a alarm after 1 seconds
sig_abrt_monitor.resume();
signal_monitor.resume();
CPPUNIT_ASSERT ( eloop.run() == 0 );
signal(SIGALRM, SIG_DFL);
signal_handler = [] (int) { }; // Do nothing
}

//----------------------------------------------------------------------
void EventloopMonitorTest::TimerMonitorTest()
{
finalcut::EventLoop eloop{};
finalcut::TimerMonitor timer_monitor{&eloop};
const finalcut::FString& timer_monitor_classname = timer_monitor.getClassName();
CPPUNIT_ASSERT ( timer_monitor_classname == "TimerMonitor" );
int num{0};
auto callback_handler = [&eloop, &num] (const finalcut::Monitor*, short)
{
Expand Down

0 comments on commit c546fba

Please sign in to comment.