17
17
#include " greentea-client/test_env.h"
18
18
#include " unity.h"
19
19
#include " utest.h"
20
- #include " rtos .h"
20
+ #include " timeout_tests .h"
21
21
22
22
using namespace utest ::v1;
23
23
24
- #define NUM_TIMEOUTS 64
25
- const float TEST_DELAY_S = 0.01 ;
26
- const uint32_t TEST_DELAY_MS = 1000 .0F * TEST_DELAY_S;
27
- const us_timestamp_t TEST_DELAY_US = 1000000 .0F * TEST_DELAY_S;
28
-
29
- void sem_callback (Semaphore *sem)
30
- {
31
- sem->release ();
32
- }
33
-
34
- void cnt_callback (volatile uint32_t *cnt)
35
- {
36
- (*cnt)++;
37
- }
38
-
39
- class TimeoutAttachUSTester : public Timeout {
40
- public:
41
- void attach_callback (Callback<void ()> func, us_timestamp_t delay_us)
42
- {
43
- attach_us (func, delay_us);
44
- }
45
- };
46
-
47
- class TimeoutAttachTester : public Timeout {
48
- public:
49
- void attach_callback (Callback<void ()> func, us_timestamp_t delay_us)
50
- {
51
- attach (func, (float ) delay_us / 1000000 .0f );
52
- }
53
- };
54
-
55
- /* * Template for tests: callback called once
56
- *
57
- * Test callback called once
58
- * Given a Timeout object with a callback attached with @a attach()
59
- * When given time elapses
60
- * Then the callback is called exactly one time
61
- *
62
- * Test callback called once
63
- * Given a Timeout object with a callback attached with @a attach_us()
64
- * When given time elapses
65
- * Then the callback is called exactly one time
66
- */
67
- template <typename T>
68
- void test_callback_fires_once (void )
69
- {
70
- Semaphore sem (0 , 1 );
71
- T timeout;
72
-
73
- timeout.attach_callback (mbed::callback (sem_callback, &sem), TEST_DELAY_US);
74
-
75
- int32_t sem_slots = sem.wait (0 );
76
- TEST_ASSERT_EQUAL (0 , sem_slots);
77
-
78
- sem_slots = sem.wait (TEST_DELAY_MS + 1 );
79
- TEST_ASSERT_EQUAL (1 , sem_slots);
80
-
81
- sem_slots = sem.wait (TEST_DELAY_MS + 1 );
82
- TEST_ASSERT_EQUAL (0 , sem_slots);
83
-
84
- timeout.detach ();
85
- }
86
-
87
- /* * Template for tests: callback not called when cancelled
88
- *
89
- * Test callback not called when cancelled
90
- * Given a Timeout object with a callback attached with @a attach()
91
- * When the callback is detached before being called
92
- * Then the callback is never called
93
- *
94
- * Test callback not called when cancelled
95
- * Given a Timeout object with a callback attached with @a attach_us()
96
- * When the callback is detached before being called
97
- * Then the callback is never called
98
- */
99
- template <typename T>
100
- void test_cancel (void )
101
- {
102
- Semaphore sem (0 , 1 );
103
- T timeout;
104
-
105
- timeout.attach_callback (mbed::callback (sem_callback, &sem), 2 .0f * TEST_DELAY_US);
106
-
107
- int32_t sem_slots = sem.wait (TEST_DELAY_MS);
108
- TEST_ASSERT_EQUAL (0 , sem_slots);
109
- timeout.detach ();
110
-
111
- sem_slots = sem.wait (TEST_DELAY_MS + 1 );
112
- TEST_ASSERT_EQUAL (0 , sem_slots);
113
- }
114
-
115
- /* * Template for tests: callback override
116
- *
117
- * Test callback override
118
- * Given a Timeout object with a callback attached with @a attach()
119
- * When another callback is attached before first one is called
120
- * and second callback's delay elapses
121
- * Then the second callback is called
122
- * and the first callback is never called
123
- *
124
- * Test callback override
125
- * Given a Timeout object with a callback attached with @a attach_us()
126
- * When another callback is attached before first one is called
127
- * and second callback's delay elapses
128
- * Then the second callback is called
129
- * and the first callback is never called
130
- */
131
- template <typename T>
132
- void test_override (void )
133
- {
134
- Semaphore sem1 (0 , 1 );
135
- Semaphore sem2 (0 , 1 );
136
- T timeout;
137
-
138
- timeout.attach_callback (mbed::callback (sem_callback, &sem1), 2 .0f * TEST_DELAY_US);
139
-
140
- int32_t sem_slots = sem1.wait (TEST_DELAY_MS);
141
- TEST_ASSERT_EQUAL (0 , sem_slots);
142
- timeout.attach_callback (mbed::callback (sem_callback, &sem2), 2 .0f * TEST_DELAY_US);
143
-
144
- sem_slots = sem2.wait (2 * TEST_DELAY_MS + 1 );
145
- TEST_ASSERT_EQUAL (1 , sem_slots);
146
- sem_slots = sem1.wait (0 );
147
- TEST_ASSERT_EQUAL (0 , sem_slots);
148
-
149
- timeout.detach ();
150
- }
151
-
152
- /* * Template for tests: multiple Timeouts
153
- *
154
- * Test multiple Timeouts
155
- * Given multiple separate Timeout objects
156
- * When a callback is attached to all of these Timeout objects with @a attach()
157
- * and delay for every Timeout elapses
158
- * Then all callbacks are called
159
- *
160
- * Test multiple Timeouts
161
- * Given multiple separate Timeout objects
162
- * When a callback is attached to all of these Timeout objects with @a attach_us()
163
- * and delay for every Timeout elapses
164
- * Then all callbacks are called
165
- */
166
- template <typename T>
167
- void test_multiple (void )
168
- {
169
- volatile uint32_t callback_count = 0 ;
170
- T timeouts[NUM_TIMEOUTS];
171
- for (size_t i = 0 ; i < NUM_TIMEOUTS; i++) {
172
- timeouts[i].attach_callback (mbed::callback (cnt_callback, &callback_count), TEST_DELAY_US);
173
- }
174
- Thread::wait (TEST_DELAY_MS + 1 );
175
- TEST_ASSERT_EQUAL (NUM_TIMEOUTS, callback_count);
176
- }
177
-
178
- /* * Template for tests: zero delay
179
- *
180
- * Test zero delay
181
- * Given a Timeout object
182
- * When a callback is attached with 0.0 s delay, with @a attach()
183
- * Then the callback is called instantly
184
- *
185
- * Test zero delay
186
- * Given a Timeout object
187
- * When a callback is attached with 0.0 s delay, with @a attach_us()
188
- * Then the callback is called instantly
189
- */
190
- template <typename T>
191
- void test_no_wait (void )
192
- {
193
- Semaphore sem (0 , 1 );
194
- T timeout;
195
- timeout.attach_callback (mbed::callback (sem_callback, &sem), 0ULL );
196
-
197
- int32_t sem_slots = sem.wait (0 );
198
- TEST_ASSERT_EQUAL (1 , sem_slots);
199
- timeout.detach ();
200
- }
201
-
202
24
#define PERIOD_US 10000
203
25
volatile uint32_t callback_trigger_count = 0 ;
204
26
static const int test_timeout = 240 ;
@@ -224,7 +46,6 @@ void set_increment_count()
224
46
*/
225
47
void test_case_timeout ()
226
48
{
227
-
228
49
char _key[11 ] = { };
229
50
char _value[128 ] = { };
230
51
int expected_key = 1 ;
@@ -251,20 +72,20 @@ void test_case_timeout()
251
72
}
252
73
253
74
Case cases[] = {
254
- Case (" Test callback called once (with attach)" , test_callback_fires_once<TimeoutAttachTester >),
255
- Case (" Test callback called once (with attach_us)" , test_callback_fires_once<TimeoutAttachUSTester >),
75
+ Case (" Callback called once (with attach)" , test_single_call<AttachTester<Timeout> >),
76
+ Case (" Callback called once (with attach_us)" , test_single_call<AttachUSTester<Timeout> >),
256
77
257
- Case (" Test callback not called when cancelled (with attach)" , test_cancel<TimeoutAttachTester >),
258
- Case (" Test callback not called when cancelled (with attach_us)" , test_cancel<TimeoutAttachUSTester >),
78
+ Case (" Callback not called when cancelled (with attach)" , test_cancel<AttachUSTester<Timeout> >),
79
+ Case (" Callback not called when cancelled (with attach_us)" , test_cancel<AttachUSTester<Timeout> >),
259
80
260
- Case (" Test callback override (with attach)" , test_override<TimeoutAttachTester >),
261
- Case (" Test callback override (with attach_us)" , test_override<TimeoutAttachUSTester >),
81
+ Case (" Callback override (with attach)" , test_override<AttachTester<Timeout> >),
82
+ Case (" Callback override (with attach_us)" , test_override<AttachUSTester<Timeout> >),
262
83
263
- Case (" Test multiple timeouts running in parallel (with attach)" , test_multiple<TimeoutAttachTester >),
264
- Case (" Test multiple timeouts running in parallel (with attach_us)" , test_multiple<TimeoutAttachUSTester >),
84
+ Case (" Multiple timeouts running in parallel (with attach)" , test_multiple<AttachTester<Timeout> >),
85
+ Case (" Multiple timeouts running in parallel (with attach_us)" , test_multiple<AttachUSTester<Timeout> >),
265
86
266
- Case (" Test zero delay (with attach)" , test_no_wait<TimeoutAttachTester >),
267
- Case (" Test zero delay (with attach_us)" , test_no_wait<TimeoutAttachUSTester >),
87
+ Case (" Zero delay (with attach)" , test_no_wait<AttachTester<Timeout> >),
88
+ Case (" Zero delay (with attach_us)" , test_no_wait<AttachUSTester<Timeout> >),
268
89
269
90
Case (" Timers: toggle on/off" , test_case_timeout)
270
91
};
0 commit comments