Skip to content

Commit aea1237

Browse files
committed
Incorporated the review comments
1 parent 8bc7ffe commit aea1237

File tree

1 file changed

+28
-26
lines changed
  • TESTS/mbedmicro-rtos-mbed/semaphore

1 file changed

+28
-26
lines changed

TESTS/mbedmicro-rtos-mbed/semaphore/main.cpp

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ void test_thread(int const *delay)
6464

6565
/* Test multiple threads
6666
67-
Given 3 threads started with different delays and a semaphore with 2 tokens
68-
when each thread runs it tries to acquire a token
69-
then no more than two threads should be able to access protected region
67+
Given 3 child threads started with different delays and a semaphore with 2 tokens available
68+
when each child thread calls @a acquire
69+
then no more than two child threads should be able to access protected region
7070
*/
7171
void test_multi()
7272
{
@@ -100,13 +100,11 @@ void single_thread(struct test_data *data)
100100

101101
/** Test single thread
102102
103-
Given a two threads A & B and a semaphore (with count of 0) and a counter (equals to 0)
104-
when thread B calls @a wait
105-
then thread B waits for a token to become available
106-
then the counter is equal to 0
107-
when thread A calls @a release on the semaphore
108-
then thread B acquires a token and increments the counter
109-
then the counter equals to 1
103+
Given a 2(main and child) threads and a semaphore with no tokens available
104+
when the child thread calls @a acquire
105+
then the child thread is blocked
106+
when main thread calls @a release on the semaphore
107+
then the child thread is unblocked.
110108
*/
111109
void test_single_thread()
112110
{
@@ -143,9 +141,9 @@ void timeout_thread(Semaphore *sem)
143141

144142
/** Test timeout
145143
146-
Given thread and a semaphore with no tokens available
147-
when thread calls @a wait on the semaphore with timeout of 10ms
148-
then the thread waits for 10ms and timeouts after
144+
Given a 2(main and child) threads and a semaphore with no tokens available
145+
when a child thread calls @a try_acquire_for with 30ms timeout
146+
then the child thread is blocked for 30ms and timeouts after
149147
*/
150148
void test_timeout()
151149
{
@@ -177,8 +175,10 @@ void test_ticker_release(struct test_data *data)
177175
/** Test semaphore acquire
178176
179177
Given a semaphore with no tokens available and ticker with the callback registered
180-
when callbacks update the test data and release semaphore
181-
which will unblock the main thread which is blocked on semaphore acquire.
178+
when the main thread calls @a acquire
179+
then the main thread is blocked
180+
when callback calls @a release on the semaphore
181+
then the main thread is unblocked
182182
*/
183183
void test_semaphore_acquire()
184184
{
@@ -205,29 +205,29 @@ void test_ticker_try_acquire(Semaphore *sem)
205205
/** Test semaphore try acquire
206206
207207
Given a semaphore with no tokens available and ticker with the callback registered
208-
when callbacks try to acquire the semaphore will fail.
208+
when callback tries to acquire the semaphore, it fails.
209209
*/
210210
void test_semaphore_try_acquire()
211211
{
212212
Semaphore sem(0);
213213
Ticker t1;
214214
t1.attach_us(callback(test_ticker_try_acquire, &sem), 3000);
215-
ThisThread::sleep_for(3);
215+
ThisThread::sleep_for(4);
216216
t1.detach();
217217
}
218218

219219

220220
/** Test semaphore try timeout
221221
222222
Given a semaphore with no tokens available
223-
when the main thread calls @a wait on the semaphore with try timeout of 5ms
224-
then the main thread waits for 5ms and timeouts after
223+
when the main thread calls @a try_acquire_for with 3ms timeout
224+
then the main thread is blocked for 3ms and timeouts after
225225
*/
226226
void test_semaphore_try_timeout()
227227
{
228228
Semaphore sem(0);
229229
bool res;
230-
res = sem.try_acquire_for(5);
230+
res = sem.try_acquire_for(3);
231231
TEST_ASSERT_FALSE(res);
232232
}
233233

@@ -242,16 +242,18 @@ void test_ticker_semaphore_release(struct Semaphore *sem)
242242
/** Test semaphore try acquire timeout
243243
244244
Given a semaphore with no tokens available and ticker with the callback registered
245-
when callbacks release the semaphore will unblock the main thread
246-
which is waiting for semaphore with a timeout.
245+
when the main thread calls @a try_acquire_for with 10ms timeout
246+
then the main thread is blocked for 10ms
247+
when callbacks calls @a release on the semaphore
248+
then the main thread is unblocked.
247249
*/
248250
void test_semaphore_try_acquire_timeout()
249251
{
250252
Semaphore sem(0);
251253
bool res;
252254
Ticker t1;
253255
t1.attach_us(callback(test_ticker_semaphore_release, &sem), 3000);
254-
res = sem.try_acquire_for(30);
256+
res = sem.try_acquire_for(10);
255257
t1.detach();
256258
TEST_ASSERT_TRUE(res);
257259
}
@@ -260,12 +262,12 @@ void test_semaphore_try_acquire_timeout()
260262
261263
Test 1 token no timeout
262264
Given thread and a semaphore with one token available
263-
when thread calls @a wait on the semaphore with timeout of 0ms
265+
when thread calls @a try_acquire with timeout of 0ms
264266
then the thread acquires the token immediately
265267
266268
Test 0 tokens no timeout
267269
Given thread and a semaphore with no tokens available
268-
when thread calls @a wait on the semaphore with timeout of 0ms
270+
when thread calls @a try_acquire with timeout of 0ms
269271
then the thread returns immediately without acquiring a token
270272
*/
271273
template<int T>
@@ -285,7 +287,7 @@ void test_no_timeout()
285287
/** Test multiple tokens wait
286288
287289
Given a thread and a semaphore initialized with 5 tokens
288-
when thread calls @a wait 6 times on the semaphore
290+
when thread calls @a try_acquire 6 times in a loop
289291
then the token counts goes to zero
290292
*/
291293
void test_multiple_tokens_wait()

0 commit comments

Comments
 (0)