|
22 | 22 |
|
23 | 23 | -export([test/0]). |
24 | 24 |
|
| 25 | +-include("etest.hrl"). |
| 26 | + |
25 | 27 | test() -> |
26 | 28 | ok = test_timer(), |
27 | | - ok = test_timer_interrupt(), |
28 | 29 | ok = test_timer_loop(), |
| 30 | + ok = test_timer_badargs(), |
| 31 | + ok = test_infinity(), |
29 | 32 | ok. |
30 | 33 |
|
31 | | --include("etest.hrl"). |
32 | | - |
33 | 34 | test_timer() -> |
34 | | - T0 = erlang:timestamp(), |
| 35 | + T0 = erlang:system_time(millisecond), |
35 | 36 | ok = timer:sleep(101), |
36 | | - T1 = erlang:timestamp(), |
37 | | - ok = etest:assert_true((to_ms(T1) - to_ms(T0)) >= 101), |
| 37 | + T1 = erlang:system_time(millisecond), |
| 38 | + ok = etest:assert_true((T1 - T0) >= 101), |
38 | 39 | ok. |
39 | 40 |
|
40 | | -test_timer_interrupt() -> |
41 | | - Self = self(), |
42 | | - Pid = spawn(fun() -> do_test_interrupt(Self) end), |
43 | | - receive |
44 | | - ready -> ok |
45 | | - end, |
46 | | - |
47 | | - %% this message should not interrupt the timer |
48 | | - Pid ! try_to_interrupt, |
49 | | - |
50 | | - Pid ! '$atomvm_timer_interrupt', |
51 | | - ?ASSERT_MATCH(pop_mailbox(), ok), |
52 | | - ok. |
53 | | - |
54 | | -pop_mailbox() -> |
55 | | - receive |
56 | | - X -> X |
57 | | - end. |
58 | | - |
59 | | -do_test_interrupt(Pid) -> |
60 | | - Pid ! ready, |
61 | | - case timer:sleep(infinity) of |
62 | | - {error, unexpected_interrupt} -> |
63 | | - Pid ! ok; |
64 | | - _ -> |
65 | | - Pid ! error |
66 | | - end. |
67 | | - |
68 | 41 | test_timer_loop() -> |
69 | 42 | Self = self(), |
70 | 43 | spawn(fun() -> |
71 | | - timer:sleep(220), |
72 | | - Self ! ping |
| 44 | + Self ! ready, |
| 45 | + timer:sleep(50), |
| 46 | + Self ! noise |
73 | 47 | end), |
| 48 | + receive |
| 49 | + ready -> |
| 50 | + ok |
| 51 | + end, |
74 | 52 | ok = timer_loop(5). |
75 | 53 |
|
76 | 54 | timer_loop(0) -> |
77 | | - ok; |
| 55 | + receive |
| 56 | + noise -> |
| 57 | + ok; |
| 58 | + SomethingElse -> |
| 59 | + {error, SomethingElse} |
| 60 | + end; |
78 | 61 | timer_loop(I) -> |
79 | | - T0 = erlang:timestamp(), |
| 62 | + T0 = erlang:system_time(millisecond), |
80 | 63 | ok = timer:sleep(101), |
81 | | - T1 = erlang:timestamp(), |
82 | | - ok = etest:assert_true((to_ms(T1) - to_ms(T0)) >= 101), |
| 64 | + T1 = erlang:system_time(millisecond), |
| 65 | + ok = etest:assert_true((T1 - T0) >= 101), |
83 | 66 | timer_loop(I - 1). |
84 | 67 |
|
85 | | -to_ms({MegaSecs, Secs, MicroSecs}) -> |
86 | | - ((MegaSecs * 1000000 + Secs) * 1000 + MicroSecs div 1000). |
| 68 | +test_timer_badargs() -> |
| 69 | + {'EXIT', {timeout_value, _}} = (catch timer:sleep(-1)), |
| 70 | + {'EXIT', {timeout_value, _}} = (catch timer:sleep(not_infinity)), |
| 71 | + ok. |
| 72 | + |
| 73 | +test_infinity() -> |
| 74 | + Self = self(), |
| 75 | + Pid = spawn(fun() -> |
| 76 | + Self ! ok, |
| 77 | + timer:sleep(infinity) |
| 78 | + end), |
| 79 | + receive |
| 80 | + ok -> |
| 81 | + ok = etest:assert_true(erlang:is_process_alive(Pid)) |
| 82 | + end. |
0 commit comments