Incorporated the review comments

pull/12816/head
Rajkumar Kanagaraj 2020-04-17 13:01:25 +01:00
parent 8bc7ffe6e3
commit a24419bde6
1 changed files with 20 additions and 16 deletions

View File

@ -101,7 +101,7 @@ void single_thread(struct test_data *data)
/** Test single thread /** Test single thread
Given a two threads A & B and a semaphore (with count of 0) and a counter (equals to 0) Given a two threads A & B and a semaphore (with count of 0) and a counter (equals to 0)
when thread B calls @a wait when thread B calls @a acquire
then thread B waits for a token to become available then thread B waits for a token to become available
then the counter is equal to 0 then the counter is equal to 0
when thread A calls @a release on the semaphore when thread A calls @a release on the semaphore
@ -144,8 +144,8 @@ void timeout_thread(Semaphore *sem)
/** Test timeout /** Test timeout
Given thread and a semaphore with no tokens available Given thread and a semaphore with no tokens available
when thread calls @a wait on the semaphore with timeout of 10ms when a thread calls @a try_acquire_for with a timeout of 30ms
then the thread waits for 10ms and timeouts after then the thread is blocked for up to 30ms and timeouts after.
*/ */
void test_timeout() void test_timeout()
{ {
@ -177,8 +177,10 @@ void test_ticker_release(struct test_data *data)
/** Test semaphore acquire /** Test semaphore acquire
Given a semaphore with no tokens available and ticker with the callback registered Given a semaphore with no tokens available and ticker with the callback registered
when callbacks update the test data and release semaphore when the main thread calls @a acquire
which will unblock the main thread which is blocked on semaphore acquire. then the main thread is blocked
when callback calls @a release on the semaphore
then the main thread is unblocked
*/ */
void test_semaphore_acquire() void test_semaphore_acquire()
{ {
@ -205,14 +207,14 @@ void test_ticker_try_acquire(Semaphore *sem)
/** Test semaphore try acquire /** Test semaphore try acquire
Given a semaphore with no tokens available and ticker with the callback registered Given a semaphore with no tokens available and ticker with the callback registered
when callbacks try to acquire the semaphore will fail. when callback tries to acquire the semaphore, it fails.
*/ */
void test_semaphore_try_acquire() void test_semaphore_try_acquire()
{ {
Semaphore sem(0); Semaphore sem(0);
Ticker t1; Ticker t1;
t1.attach_us(callback(test_ticker_try_acquire, &sem), 3000); t1.attach_us(callback(test_ticker_try_acquire, &sem), 3000);
ThisThread::sleep_for(3); ThisThread::sleep_for(4);
t1.detach(); t1.detach();
} }
@ -220,14 +222,14 @@ void test_semaphore_try_acquire()
/** Test semaphore try timeout /** Test semaphore try timeout
Given a semaphore with no tokens available Given a semaphore with no tokens available
when the main thread calls @a wait on the semaphore with try timeout of 5ms when the main thread calls @a try_acquire_for with 3ms timeout
then the main thread waits for 5ms and timeouts after then the main thread is blocked for 3ms and timeouts after
*/ */
void test_semaphore_try_timeout() void test_semaphore_try_timeout()
{ {
Semaphore sem(0); Semaphore sem(0);
bool res; bool res;
res = sem.try_acquire_for(5); res = sem.try_acquire_for(3);
TEST_ASSERT_FALSE(res); TEST_ASSERT_FALSE(res);
} }
@ -242,8 +244,10 @@ void test_ticker_semaphore_release(struct Semaphore *sem)
/** Test semaphore try acquire timeout /** Test semaphore try acquire timeout
Given a semaphore with no tokens available and ticker with the callback registered Given a semaphore with no tokens available and ticker with the callback registered
when callbacks release the semaphore will unblock the main thread when the main thread calls @a try_acquire_for with 10ms timeout
which is waiting for semaphore with a timeout. then the main thread is blocked for up to 10ms
when callback call @a release on the semaphore after 3ms
then the main thread is unblocked.
*/ */
void test_semaphore_try_acquire_timeout() void test_semaphore_try_acquire_timeout()
{ {
@ -251,7 +255,7 @@ void test_semaphore_try_acquire_timeout()
bool res; bool res;
Ticker t1; Ticker t1;
t1.attach_us(callback(test_ticker_semaphore_release, &sem), 3000); t1.attach_us(callback(test_ticker_semaphore_release, &sem), 3000);
res = sem.try_acquire_for(30); res = sem.try_acquire_for(10);
t1.detach(); t1.detach();
TEST_ASSERT_TRUE(res); TEST_ASSERT_TRUE(res);
} }
@ -260,12 +264,12 @@ void test_semaphore_try_acquire_timeout()
Test 1 token no timeout Test 1 token no timeout
Given thread and a semaphore with one token available Given thread and a semaphore with one token available
when thread calls @a wait on the semaphore with timeout of 0ms when thread calls @a try_acquire with timeout of 0ms
then the thread acquires the token immediately then the thread acquires the token immediately
Test 0 tokens no timeout Test 0 tokens no timeout
Given thread and a semaphore with no tokens available Given thread and a semaphore with no tokens available
when thread calls @a wait on the semaphore with timeout of 0ms when thread calls @a try_acquire with timeout of 0ms
then the thread returns immediately without acquiring a token then the thread returns immediately without acquiring a token
*/ */
template<int T> template<int T>
@ -285,7 +289,7 @@ void test_no_timeout()
/** Test multiple tokens wait /** Test multiple tokens wait
Given a thread and a semaphore initialized with 5 tokens Given a thread and a semaphore initialized with 5 tokens
when thread calls @a wait 6 times on the semaphore when thread calls @a try_acquire 6 times in a loop
then the token counts goes to zero then the token counts goes to zero
*/ */
void test_multiple_tokens_wait() void test_multiple_tokens_wait()