Skip to content

Commit 8bc7ffe

Browse files
committed
Baremetal: Enable Semaphore greentea test
1 parent a252b07 commit 8bc7ffe

File tree

1 file changed

+107
-14
lines changed
  • TESTS/mbedmicro-rtos-mbed/semaphore

1 file changed

+107
-14
lines changed

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

Lines changed: 107 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17-
#if defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)
18-
#error [NOT_SUPPORTED] Semaphore test cases require RTOS with multithread to run
19-
#else
20-
2117
#if !DEVICE_USTICKER
2218
#error [NOT_SUPPORTED] UsTicker need to be enabled for this test.
2319
#else
@@ -30,6 +26,12 @@
3026

3127
using namespace utest::v1;
3228

29+
struct test_data {
30+
Semaphore *sem;
31+
uint32_t data;
32+
};
33+
34+
#if defined(MBED_CONF_RTOS_PRESENT)
3335
#define THREAD_DELAY 30
3436
#define SEMAPHORE_SLOTS 2
3537
#define SEM_CHANGES 100
@@ -90,12 +92,7 @@ void test_multi()
9092
}
9193
}
9294

93-
struct thread_data {
94-
Semaphore *sem;
95-
uint32_t data;
96-
};
97-
98-
void single_thread(struct thread_data *data)
95+
void single_thread(struct test_data *data)
9996
{
10097
data->sem->acquire();
10198
data->data++;
@@ -115,7 +112,7 @@ void test_single_thread()
115112
{
116113
Thread t(osPriorityNormal, THREAD_STACK_SIZE);
117114
Semaphore sem(0);
118-
struct thread_data data;
115+
struct test_data data;
119116
osStatus res;
120117

121118
data.sem = &sem;
@@ -167,6 +164,97 @@ void test_timeout()
167164
t.join();
168165
TEST_ASSERT_UINT32_WITHIN(5000, 30000, timer.read_us());
169166
}
167+
#endif
168+
169+
void test_ticker_release(struct test_data *data)
170+
{
171+
osStatus res;
172+
data->data++;
173+
res = data->sem->release();
174+
TEST_ASSERT_EQUAL(osOK, res);
175+
}
176+
177+
/** Test semaphore acquire
178+
179+
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.
182+
*/
183+
void test_semaphore_acquire()
184+
{
185+
Semaphore sem(0);
186+
struct test_data data;
187+
188+
data.sem = &sem;
189+
data.data = 0;
190+
Ticker t1;
191+
t1.attach_us(callback(test_ticker_release, &data), 3000);
192+
sem.acquire();
193+
t1.detach();
194+
195+
TEST_ASSERT_EQUAL(1, data.data);
196+
}
197+
198+
void test_ticker_try_acquire(Semaphore *sem)
199+
{
200+
osStatus res;
201+
res = sem->try_acquire();
202+
TEST_ASSERT_FALSE(res);
203+
}
204+
205+
/** Test semaphore try acquire
206+
207+
Given a semaphore with no tokens available and ticker with the callback registered
208+
when callbacks try to acquire the semaphore will fail.
209+
*/
210+
void test_semaphore_try_acquire()
211+
{
212+
Semaphore sem(0);
213+
Ticker t1;
214+
t1.attach_us(callback(test_ticker_try_acquire, &sem), 3000);
215+
ThisThread::sleep_for(3);
216+
t1.detach();
217+
}
218+
219+
220+
/** Test semaphore try timeout
221+
222+
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
225+
*/
226+
void test_semaphore_try_timeout()
227+
{
228+
Semaphore sem(0);
229+
bool res;
230+
res = sem.try_acquire_for(5);
231+
TEST_ASSERT_FALSE(res);
232+
}
233+
234+
235+
void test_ticker_semaphore_release(struct Semaphore *sem)
236+
{
237+
osStatus res;
238+
res = sem->release();
239+
TEST_ASSERT_EQUAL(osOK, res);
240+
}
241+
242+
/** Test semaphore try acquire timeout
243+
244+
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.
247+
*/
248+
void test_semaphore_try_acquire_timeout()
249+
{
250+
Semaphore sem(0);
251+
bool res;
252+
Ticker t1;
253+
t1.attach_us(callback(test_ticker_semaphore_release, &sem), 3000);
254+
res = sem.try_acquire_for(30);
255+
t1.detach();
256+
TEST_ASSERT_TRUE(res);
257+
}
170258

171259
/** Test no timeouts
172260
@@ -235,13 +323,19 @@ utest::v1::status_t test_setup(const size_t number_of_cases)
235323
}
236324

237325
Case cases[] = {
238-
Case("Test single thread", test_single_thread),
239-
Case("Test timeout", test_timeout),
240326
Case("Test 1 token no timeout", test_no_timeout<1>),
241327
Case("Test 0 tokens no timeout", test_no_timeout<0>),
242328
Case("Test multiple tokens wait", test_multiple_tokens_wait),
243329
Case("Test multiple tokens release", test_multiple_tokens_release),
330+
Case("Test semaphore acquire", test_semaphore_acquire),
331+
Case("Test semaphore try acquire", test_semaphore_try_acquire),
332+
Case("Test semaphore try timeout", test_semaphore_try_timeout),
333+
Case("Test semaphore try acquire timeout", test_semaphore_try_acquire_timeout),
334+
#if defined(MBED_CONF_RTOS_PRESENT)
335+
Case("Test single thread", test_single_thread),
336+
Case("Test timeout", test_timeout),
244337
Case("Test multiple threads", test_multi)
338+
#endif
245339
};
246340

247341
Specification specification(test_setup, cases);
@@ -252,4 +346,3 @@ int main()
252346
}
253347

254348
#endif // !DEVICE_USTICKER
255-
#endif // defined(MBED_RTOS_SINGLE_THREAD) || !defined(MBED_CONF_RTOS_PRESENT)

0 commit comments

Comments
 (0)