[libcamera-devel,4/4] tests: event-dispatcher: Add processEvents() interruption test

Message ID 20190123085923.12524-5-laurent.pinchart@ideasonboard.com
State Accepted
Commit eb1ecc92ce2fac649f7cbaecd58be29d23602713
Headers show
Series
  • Event dispatcher enhancements
Related show

Commit Message

Laurent Pinchart Jan. 23, 2019, 8:59 a.m. UTC
Test that the EventDispatcher::interrupt() function correctly interrupts
the processEvents() function, both when called before processEvents()
and when called while it is running.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/event-dispatcher.cpp | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

Comments

Kieran Bingham Jan. 23, 2019, 11:50 a.m. UTC | #1
Hi Laurent,

On 23/01/2019 08:59, Laurent Pinchart wrote:
> Test that the EventDispatcher::interrupt() function correctly interrupts
> the processEvents() function, both when called before processEvents()
> and when called while it is running.
> 

LGTM

Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>

> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

While testing this, it occurs to me - is there any way we can call into
the Logger instance from an application (or specifically a *test*
application) to raise the default log level to DEBUG?

It's about the only time where I'd want LIBCAMERA_LOG_LEVELS=*:DEBUG by
default.

I tried adding a call to putenv("LIBCAMERA_LOG_LEVELS=*:DEBUG"); in
Test::init(); but of course this only gets executed after the static
Logger->instance() is already created.


I wonder if we might expose a static method on the internal Logger()
instance so that we can call

   libcamera::Logger::SetLogLevel(Debug);

Only an idea... and not directly related to this patch, so don't put too
much thought into it just yet.

--
Kieran


> ---
>  test/event-dispatcher.cpp | 32 +++++++++++++++++++++++++++++++-
>  1 file changed, 31 insertions(+), 1 deletion(-)
> 
> diff --git a/test/event-dispatcher.cpp b/test/event-dispatcher.cpp
> index 06c2657f09d6..e8818dcab4ad 100644
> --- a/test/event-dispatcher.cpp
> +++ b/test/event-dispatcher.cpp
> @@ -18,16 +18,23 @@
>  using namespace std;
>  using namespace libcamera;
>  
> +static EventDispatcher *dispatcher;
> +static bool interrupt;
> +
>  class EventDispatcherTest : public Test
>  {
>  protected:
>  	static void sigAlarmHandler(int)
>  	{
>  		cout << "SIGALARM received" << endl;
> +		if (interrupt)
> +			dispatcher->interrupt();
>  	}
>  
>  	int init()
>  	{
> +		dispatcher = CameraManager::instance()->eventDispatcher();
> +
>  		struct sigaction sa = {};
>  		sa.sa_handler = &sigAlarmHandler;
>  		sigaction(SIGALRM, &sa, nullptr);
> @@ -37,7 +44,6 @@ protected:
>  
>  	int run()
>  	{
> -		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
>  		Timer timer;
>  
>  		/* Event processing interruption by signal. */
> @@ -48,6 +54,7 @@ protected:
>  
>  		struct itimerval itimer = {};
>  		itimer.it_value.tv_usec = 500000;
> +		interrupt = false;
>  		setitimer(ITIMER_REAL, &itimer, nullptr);
>  
>  		dispatcher->processEvents();
> @@ -62,6 +69,29 @@ protected:
>  			return TestFail;
>  		}
>  
> +		/* Event processing interruption. */
> +		timer.start(1000);
> +		dispatcher->interrupt();
> +
> +		dispatcher->processEvents();
> +
> +		if (!timer.isRunning()) {
> +			cout << "Event processing immediate interruption failed" << endl;
> +			return TestFail;
> +		}
> +
> +		timer.start(1000);
> +		itimer.it_value.tv_usec = 500000;
> +		interrupt = true;
> +		setitimer(ITIMER_REAL, &itimer, nullptr);
> +
> +		dispatcher->processEvents();
> +
> +		if (!timer.isRunning()) {
> +			cout << "Event processing delayed interruption failed" << endl;
> +			return TestFail;
> +		}
> +
>  		return TestPass;
>  	}
>  
>
Laurent Pinchart Jan. 23, 2019, 4:51 p.m. UTC | #2
Hi Kieran,

On Wed, Jan 23, 2019 at 11:50:57AM +0000, Kieran Bingham wrote:
> On 23/01/2019 08:59, Laurent Pinchart wrote:
> > Test that the EventDispatcher::interrupt() function correctly interrupts
> > the processEvents() function, both when called before processEvents()
> > and when called while it is running.
> > 
> 
> LGTM
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> While testing this, it occurs to me - is there any way we can call into
> the Logger instance from an application (or specifically a *test*
> application) to raise the default log level to DEBUG?
> 
> It's about the only time where I'd want LIBCAMERA_LOG_LEVELS=*:DEBUG by
> default.
> 
> I tried adding a call to putenv("LIBCAMERA_LOG_LEVELS=*:DEBUG"); in
> Test::init(); but of course this only gets executed after the static
> Logger->instance() is already created.
> 
> 
> I wonder if we might expose a static method on the internal Logger()
> instance so that we can call
> 
>    libcamera::Logger::SetLogLevel(Debug);
> 
> Only an idea... and not directly related to this patch, so don't put too
> much thought into it just yet.

I think should would make sense, possibly even exposed by the
CameraManager class as non-test applications could have a use for this.
Patches are welcome :-) We need to think about the API though, as we may
want to enable per-category log level control (that doesn't have to be
included in the code, but needs to be considered to design the API).

> > ---
> >  test/event-dispatcher.cpp | 32 +++++++++++++++++++++++++++++++-
> >  1 file changed, 31 insertions(+), 1 deletion(-)
> > 
> > diff --git a/test/event-dispatcher.cpp b/test/event-dispatcher.cpp
> > index 06c2657f09d6..e8818dcab4ad 100644
> > --- a/test/event-dispatcher.cpp
> > +++ b/test/event-dispatcher.cpp
> > @@ -18,16 +18,23 @@
> >  using namespace std;
> >  using namespace libcamera;
> >  
> > +static EventDispatcher *dispatcher;
> > +static bool interrupt;
> > +
> >  class EventDispatcherTest : public Test
> >  {
> >  protected:
> >  	static void sigAlarmHandler(int)
> >  	{
> >  		cout << "SIGALARM received" << endl;
> > +		if (interrupt)
> > +			dispatcher->interrupt();
> >  	}
> >  
> >  	int init()
> >  	{
> > +		dispatcher = CameraManager::instance()->eventDispatcher();
> > +
> >  		struct sigaction sa = {};
> >  		sa.sa_handler = &sigAlarmHandler;
> >  		sigaction(SIGALRM, &sa, nullptr);
> > @@ -37,7 +44,6 @@ protected:
> >  
> >  	int run()
> >  	{
> > -		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
> >  		Timer timer;
> >  
> >  		/* Event processing interruption by signal. */
> > @@ -48,6 +54,7 @@ protected:
> >  
> >  		struct itimerval itimer = {};
> >  		itimer.it_value.tv_usec = 500000;
> > +		interrupt = false;
> >  		setitimer(ITIMER_REAL, &itimer, nullptr);
> >  
> >  		dispatcher->processEvents();
> > @@ -62,6 +69,29 @@ protected:
> >  			return TestFail;
> >  		}
> >  
> > +		/* Event processing interruption. */
> > +		timer.start(1000);
> > +		dispatcher->interrupt();
> > +
> > +		dispatcher->processEvents();
> > +
> > +		if (!timer.isRunning()) {
> > +			cout << "Event processing immediate interruption failed" << endl;
> > +			return TestFail;
> > +		}
> > +
> > +		timer.start(1000);
> > +		itimer.it_value.tv_usec = 500000;
> > +		interrupt = true;
> > +		setitimer(ITIMER_REAL, &itimer, nullptr);
> > +
> > +		dispatcher->processEvents();
> > +
> > +		if (!timer.isRunning()) {
> > +			cout << "Event processing delayed interruption failed" << endl;
> > +			return TestFail;
> > +		}
> > +
> >  		return TestPass;
> >  	}
> >

Patch

diff --git a/test/event-dispatcher.cpp b/test/event-dispatcher.cpp
index 06c2657f09d6..e8818dcab4ad 100644
--- a/test/event-dispatcher.cpp
+++ b/test/event-dispatcher.cpp
@@ -18,16 +18,23 @@ 
 using namespace std;
 using namespace libcamera;
 
+static EventDispatcher *dispatcher;
+static bool interrupt;
+
 class EventDispatcherTest : public Test
 {
 protected:
 	static void sigAlarmHandler(int)
 	{
 		cout << "SIGALARM received" << endl;
+		if (interrupt)
+			dispatcher->interrupt();
 	}
 
 	int init()
 	{
+		dispatcher = CameraManager::instance()->eventDispatcher();
+
 		struct sigaction sa = {};
 		sa.sa_handler = &sigAlarmHandler;
 		sigaction(SIGALRM, &sa, nullptr);
@@ -37,7 +44,6 @@  protected:
 
 	int run()
 	{
-		EventDispatcher *dispatcher = CameraManager::instance()->eventDispatcher();
 		Timer timer;
 
 		/* Event processing interruption by signal. */
@@ -48,6 +54,7 @@  protected:
 
 		struct itimerval itimer = {};
 		itimer.it_value.tv_usec = 500000;
+		interrupt = false;
 		setitimer(ITIMER_REAL, &itimer, nullptr);
 
 		dispatcher->processEvents();
@@ -62,6 +69,29 @@  protected:
 			return TestFail;
 		}
 
+		/* Event processing interruption. */
+		timer.start(1000);
+		dispatcher->interrupt();
+
+		dispatcher->processEvents();
+
+		if (!timer.isRunning()) {
+			cout << "Event processing immediate interruption failed" << endl;
+			return TestFail;
+		}
+
+		timer.start(1000);
+		itimer.it_value.tv_usec = 500000;
+		interrupt = true;
+		setitimer(ITIMER_REAL, &itimer, nullptr);
+
+		dispatcher->processEvents();
+
+		if (!timer.isRunning()) {
+			cout << "Event processing delayed interruption failed" << endl;
+			return TestFail;
+		}
+
 		return TestPass;
 	}