[libcamera-devel,2/2] test: threads: Add wait() timeout test

Message ID 20200123025520.12149-2-laurent.pinchart@ideasonboard.com
State Superseded
Headers show
Series
  • [libcamera-devel,1/2] libcamera: thread: Support timeout in wait() function
Related show

Commit Message

Laurent Pinchart Jan. 23, 2020, 2:55 a.m. UTC
Add a test case to wait with a timeout, testing both a too short and a
long enough duration.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 test/threads.cpp | 33 ++++++++++++++++++++++++++-------
 1 file changed, 26 insertions(+), 7 deletions(-)

Comments

Kieran Bingham Jan. 23, 2020, 10:19 a.m. UTC | #1
Hi Laurent

On 23/01/2020 02:55, Laurent Pinchart wrote:
> Add a test case to wait with a timeout, testing both a too short and a
> long enough duration.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Some discussion below, but this is fine stand-alone anyway.

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

> ---
>  test/threads.cpp | 33 ++++++++++++++++++++++++++-------
>  1 file changed, 26 insertions(+), 7 deletions(-)
> 
> diff --git a/test/threads.cpp b/test/threads.cpp
> index 9a2d39dfd106..fc4b07cc6428 100644
> --- a/test/threads.cpp
> +++ b/test/threads.cpp
> @@ -15,24 +15,22 @@
>  using namespace std;
>  using namespace libcamera;
>  
> -class InstrumentedThread : public Thread
> +class DelayThread : public Thread
>  {
>  public:
> -	InstrumentedThread(unsigned int iterations)
> -		: iterations_(iterations)
> +	DelayThread(chrono::steady_clock::duration duration)
> +		: duration_(duration)
>  	{
>  	}
>  
>  protected:
>  	void run()
>  	{
> -		for (unsigned int i = 0; i < iterations_; ++i) {
> -			this_thread::sleep_for(chrono::milliseconds(50));
> -		}
> +		this_thread::sleep_for(duration_);
>  	}
>  
>  private:
> -	unsigned int iterations_;
> +	chrono::steady_clock::duration duration_;
>  };
>  
>  class ThreadTest : public Test
> @@ -82,6 +80,27 @@ protected:
>  
>  		delete thread;
>  
> +		/* Test waiting for completion with a timeout. */
> +		thread = new DelayThread(chrono::milliseconds(500));
> +		thread->start();
> +		thread->exit(0);
> +
> +		bool timeout = thread->wait(chrono::milliseconds(100));
> +
> +		if (!timeout) {
> +			cout << "Waiting for thread didn't time out" << endl;
> +			return TestFail;
> +		}
> +
> +		timeout = thread->wait(chrono::milliseconds(1000));
> +
> +		if (timeout) {
> +			cout << "Waiting for thread timed out" << endl;
> +			return TestFail;
> +		}
> +
> +		delete thread;
> +

How about a test for waiting on a thread that isn't running? (Perhaps
put in an thread->wait(utils::duration::max()); after we know the thread
has already stopped here ?


What happens if the wait receives a signal while it is waiting?


>  		return TestPass;
>  	}
>  
>
Laurent Pinchart Jan. 23, 2020, 1:10 p.m. UTC | #2
Hi Kieran,

On Thu, Jan 23, 2020 at 10:19:23AM +0000, Kieran Bingham wrote:
> On 23/01/2020 02:55, Laurent Pinchart wrote:
> > Add a test case to wait with a timeout, testing both a too short and a
> > long enough duration.
> > 
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> 
> Some discussion below, but this is fine stand-alone anyway.
> 
> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>
> 
> > ---
> >  test/threads.cpp | 33 ++++++++++++++++++++++++++-------
> >  1 file changed, 26 insertions(+), 7 deletions(-)
> > 
> > diff --git a/test/threads.cpp b/test/threads.cpp
> > index 9a2d39dfd106..fc4b07cc6428 100644
> > --- a/test/threads.cpp
> > +++ b/test/threads.cpp
> > @@ -15,24 +15,22 @@
> >  using namespace std;
> >  using namespace libcamera;
> >  
> > -class InstrumentedThread : public Thread
> > +class DelayThread : public Thread
> >  {
> >  public:
> > -	InstrumentedThread(unsigned int iterations)
> > -		: iterations_(iterations)
> > +	DelayThread(chrono::steady_clock::duration duration)
> > +		: duration_(duration)
> >  	{
> >  	}
> >  
> >  protected:
> >  	void run()
> >  	{
> > -		for (unsigned int i = 0; i < iterations_; ++i) {
> > -			this_thread::sleep_for(chrono::milliseconds(50));
> > -		}
> > +		this_thread::sleep_for(duration_);
> >  	}
> >  
> >  private:
> > -	unsigned int iterations_;
> > +	chrono::steady_clock::duration duration_;
> >  };
> >  
> >  class ThreadTest : public Test
> > @@ -82,6 +80,27 @@ protected:
> >  
> >  		delete thread;
> >  
> > +		/* Test waiting for completion with a timeout. */
> > +		thread = new DelayThread(chrono::milliseconds(500));
> > +		thread->start();
> > +		thread->exit(0);
> > +
> > +		bool timeout = thread->wait(chrono::milliseconds(100));
> > +
> > +		if (!timeout) {
> > +			cout << "Waiting for thread didn't time out" << endl;
> > +			return TestFail;
> > +		}
> > +
> > +		timeout = thread->wait(chrono::milliseconds(1000));
> > +
> > +		if (timeout) {
> > +			cout << "Waiting for thread timed out" << endl;
> > +			return TestFail;
> > +		}
> > +
> > +		delete thread;
> > +
> 
> How about a test for waiting on a thread that isn't running? (Perhaps
> put in an thread->wait(utils::duration::max()); after we know the thread
> has already stopped here ?

That's a good idea. I'll add it as a separate patch.

> What happens if the wait receives a signal while it is waiting?

As in a POSIX signal ? As far as I can tell, nothing.

> >  		return TestPass;
> >  	}
> >
Niklas Söderlund Feb. 10, 2020, 9:13 p.m. UTC | #3
Hi Laurent,

Thanks for your work.

On 2020-01-23 04:55:20 +0200, Laurent Pinchart wrote:
> Add a test case to wait with a timeout, testing both a too short and a
> long enough duration.
> 
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>

Reviewed-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>

> ---
>  test/threads.cpp | 33 ++++++++++++++++++++++++++-------
>  1 file changed, 26 insertions(+), 7 deletions(-)
> 
> diff --git a/test/threads.cpp b/test/threads.cpp
> index 9a2d39dfd106..fc4b07cc6428 100644
> --- a/test/threads.cpp
> +++ b/test/threads.cpp
> @@ -15,24 +15,22 @@
>  using namespace std;
>  using namespace libcamera;
>  
> -class InstrumentedThread : public Thread
> +class DelayThread : public Thread
>  {
>  public:
> -	InstrumentedThread(unsigned int iterations)
> -		: iterations_(iterations)
> +	DelayThread(chrono::steady_clock::duration duration)
> +		: duration_(duration)
>  	{
>  	}
>  
>  protected:
>  	void run()
>  	{
> -		for (unsigned int i = 0; i < iterations_; ++i) {
> -			this_thread::sleep_for(chrono::milliseconds(50));
> -		}
> +		this_thread::sleep_for(duration_);
>  	}
>  
>  private:
> -	unsigned int iterations_;
> +	chrono::steady_clock::duration duration_;
>  };
>  
>  class ThreadTest : public Test
> @@ -82,6 +80,27 @@ protected:
>  
>  		delete thread;
>  
> +		/* Test waiting for completion with a timeout. */
> +		thread = new DelayThread(chrono::milliseconds(500));
> +		thread->start();
> +		thread->exit(0);
> +
> +		bool timeout = thread->wait(chrono::milliseconds(100));
> +
> +		if (!timeout) {
> +			cout << "Waiting for thread didn't time out" << endl;
> +			return TestFail;
> +		}
> +
> +		timeout = thread->wait(chrono::milliseconds(1000));
> +
> +		if (timeout) {
> +			cout << "Waiting for thread timed out" << endl;
> +			return TestFail;
> +		}
> +
> +		delete thread;
> +
>  		return TestPass;
>  	}
>  
> -- 
> Regards,
> 
> Laurent Pinchart
> 
> _______________________________________________
> libcamera-devel mailing list
> libcamera-devel@lists.libcamera.org
> https://lists.libcamera.org/listinfo/libcamera-devel

Patch

diff --git a/test/threads.cpp b/test/threads.cpp
index 9a2d39dfd106..fc4b07cc6428 100644
--- a/test/threads.cpp
+++ b/test/threads.cpp
@@ -15,24 +15,22 @@ 
 using namespace std;
 using namespace libcamera;
 
-class InstrumentedThread : public Thread
+class DelayThread : public Thread
 {
 public:
-	InstrumentedThread(unsigned int iterations)
-		: iterations_(iterations)
+	DelayThread(chrono::steady_clock::duration duration)
+		: duration_(duration)
 	{
 	}
 
 protected:
 	void run()
 	{
-		for (unsigned int i = 0; i < iterations_; ++i) {
-			this_thread::sleep_for(chrono::milliseconds(50));
-		}
+		this_thread::sleep_for(duration_);
 	}
 
 private:
-	unsigned int iterations_;
+	chrono::steady_clock::duration duration_;
 };
 
 class ThreadTest : public Test
@@ -82,6 +80,27 @@  protected:
 
 		delete thread;
 
+		/* Test waiting for completion with a timeout. */
+		thread = new DelayThread(chrono::milliseconds(500));
+		thread->start();
+		thread->exit(0);
+
+		bool timeout = thread->wait(chrono::milliseconds(100));
+
+		if (!timeout) {
+			cout << "Waiting for thread didn't time out" << endl;
+			return TestFail;
+		}
+
+		timeout = thread->wait(chrono::milliseconds(1000));
+
+		if (timeout) {
+			cout << "Waiting for thread timed out" << endl;
+			return TestFail;
+		}
+
+		delete thread;
+
 		return TestPass;
 	}