Skip to content

Commit 41ebf0c

Browse files
authored
test(s2n-quic-core): add microwave state example (#2222)
1 parent 391068e commit 41ebf0c

8 files changed

+310
-10
lines changed

quic/s2n-quic-core/src/state.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,12 @@ macro_rules! __state_event__ {
190190

191191
/// Generates a dot graph of all state transitions
192192
pub fn dot() -> impl ::core::fmt::Display {
193-
struct Dot;
193+
struct Dot(&'static str);
194194

195195
impl ::core::fmt::Display for Dot {
196196
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
197197
writeln!(f, "digraph {{")?;
198+
writeln!(f, " label = {:?};", self.0)?;
198199

199200
let mut all_states = [
200201
// collect all of the states we've observed
@@ -230,7 +231,7 @@ macro_rules! __state_event__ {
230231
}
231232
}
232233

233-
Dot
234+
Dot(::core::any::type_name::<Self>())
234235
}
235236
}
236237
}

quic/s2n-quic-core/src/state/snapshots/s2n_quic_core__state__tests__dot_test.snap renamed to quic/s2n-quic-core/src/state/snapshots/s2n_quic_core__state__tests__lr_dot_test.snap

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
---
22
source: quic/s2n-quic-core/src/state/tests.rs
3-
expression: "State::dot()"
3+
expression: "Lr::dot()"
44
---
55
digraph {
6+
label = "s2n_quic_core::state::tests::Lr";
67
Init;
78
Left;
89
LeftLeft;

quic/s2n-quic-core/src/state/snapshots/s2n_quic_core__state__tests__snapshots.snap renamed to quic/s2n-quic-core/src/state/snapshots/s2n_quic_core__state__tests__lr_snapshots.snap

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
source: quic/s2n-quic-core/src/state/tests.rs
3-
expression: "State::test_transitions()"
3+
expression: "Lr::test_transitions()"
44
---
55
{
66
Init: {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
source: quic/s2n-quic-core/src/state/tests.rs
3+
expression: "Microwave::dot()"
4+
---
5+
digraph {
6+
label = "s2n_quic_core::state::tests::Microwave";
7+
Idle;
8+
OpenIdle;
9+
OpenPaused;
10+
OpenSettingTime;
11+
Paused;
12+
Running;
13+
SettingTime;
14+
Idle -> SettingTime [label = "on_number"];
15+
SettingTime -> SettingTime [label = "on_number"];
16+
OpenSettingTime -> OpenSettingTime [label = "on_number"];
17+
Idle -> Idle [label = "on_cancel"];
18+
SettingTime -> Idle [label = "on_cancel"];
19+
Paused -> Idle [label = "on_cancel"];
20+
Running -> Idle [label = "on_cancel"];
21+
OpenIdle -> OpenIdle [label = "on_cancel"];
22+
OpenSettingTime -> OpenIdle [label = "on_cancel"];
23+
OpenPaused -> OpenIdle [label = "on_cancel"];
24+
SettingTime -> Running [label = "on_start"];
25+
Paused -> Running [label = "on_start"];
26+
Running -> Running [label = "on_start"];
27+
Idle -> OpenIdle [label = "on_door_open"];
28+
SettingTime -> OpenSettingTime [label = "on_door_open"];
29+
Paused -> OpenPaused [label = "on_door_open"];
30+
Running -> OpenPaused [label = "on_door_open"];
31+
OpenIdle -> Idle [label = "on_door_close"];
32+
OpenSettingTime -> SettingTime [label = "on_door_close"];
33+
OpenPaused -> Paused [label = "on_door_close"];
34+
Running -> Idle [label = "on_time_finished"];
35+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
---
2+
source: quic/s2n-quic-core/src/state/tests.rs
3+
expression: "Microwave::test_transitions()"
4+
---
5+
{
6+
Idle: {
7+
on_number: Ok(
8+
SettingTime,
9+
),
10+
on_cancel: Ok(
11+
Idle,
12+
),
13+
on_start: Err(
14+
InvalidTransition {
15+
current: Idle,
16+
event: "on_start",
17+
},
18+
),
19+
on_door_open: Ok(
20+
OpenIdle,
21+
),
22+
on_door_close: Err(
23+
InvalidTransition {
24+
current: Idle,
25+
event: "on_door_close",
26+
},
27+
),
28+
on_time_finished: Err(
29+
NoOp {
30+
current: Idle,
31+
},
32+
),
33+
},
34+
OpenIdle: {
35+
on_number: Err(
36+
InvalidTransition {
37+
current: OpenIdle,
38+
event: "on_number",
39+
},
40+
),
41+
on_cancel: Ok(
42+
OpenIdle,
43+
),
44+
on_start: Err(
45+
InvalidTransition {
46+
current: OpenIdle,
47+
event: "on_start",
48+
},
49+
),
50+
on_door_open: Err(
51+
InvalidTransition {
52+
current: OpenIdle,
53+
event: "on_door_open",
54+
},
55+
),
56+
on_door_close: Ok(
57+
Idle,
58+
),
59+
on_time_finished: Err(
60+
InvalidTransition {
61+
current: OpenIdle,
62+
event: "on_time_finished",
63+
},
64+
),
65+
},
66+
OpenPaused: {
67+
on_number: Err(
68+
InvalidTransition {
69+
current: OpenPaused,
70+
event: "on_number",
71+
},
72+
),
73+
on_cancel: Ok(
74+
OpenIdle,
75+
),
76+
on_start: Err(
77+
InvalidTransition {
78+
current: OpenPaused,
79+
event: "on_start",
80+
},
81+
),
82+
on_door_open: Err(
83+
InvalidTransition {
84+
current: OpenPaused,
85+
event: "on_door_open",
86+
},
87+
),
88+
on_door_close: Ok(
89+
Paused,
90+
),
91+
on_time_finished: Err(
92+
InvalidTransition {
93+
current: OpenPaused,
94+
event: "on_time_finished",
95+
},
96+
),
97+
},
98+
OpenSettingTime: {
99+
on_number: Ok(
100+
OpenSettingTime,
101+
),
102+
on_cancel: Ok(
103+
OpenIdle,
104+
),
105+
on_start: Err(
106+
InvalidTransition {
107+
current: OpenSettingTime,
108+
event: "on_start",
109+
},
110+
),
111+
on_door_open: Err(
112+
InvalidTransition {
113+
current: OpenSettingTime,
114+
event: "on_door_open",
115+
},
116+
),
117+
on_door_close: Ok(
118+
SettingTime,
119+
),
120+
on_time_finished: Err(
121+
InvalidTransition {
122+
current: OpenSettingTime,
123+
event: "on_time_finished",
124+
},
125+
),
126+
},
127+
Paused: {
128+
on_number: Err(
129+
InvalidTransition {
130+
current: Paused,
131+
event: "on_number",
132+
},
133+
),
134+
on_cancel: Ok(
135+
Idle,
136+
),
137+
on_start: Ok(
138+
Running,
139+
),
140+
on_door_open: Ok(
141+
OpenPaused,
142+
),
143+
on_door_close: Err(
144+
InvalidTransition {
145+
current: Paused,
146+
event: "on_door_close",
147+
},
148+
),
149+
on_time_finished: Err(
150+
InvalidTransition {
151+
current: Paused,
152+
event: "on_time_finished",
153+
},
154+
),
155+
},
156+
Running: {
157+
on_number: Err(
158+
InvalidTransition {
159+
current: Running,
160+
event: "on_number",
161+
},
162+
),
163+
on_cancel: Ok(
164+
Idle,
165+
),
166+
on_start: Ok(
167+
Running,
168+
),
169+
on_door_open: Ok(
170+
OpenPaused,
171+
),
172+
on_door_close: Err(
173+
InvalidTransition {
174+
current: Running,
175+
event: "on_door_close",
176+
},
177+
),
178+
on_time_finished: Ok(
179+
Idle,
180+
),
181+
},
182+
SettingTime: {
183+
on_number: Ok(
184+
SettingTime,
185+
),
186+
on_cancel: Ok(
187+
Idle,
188+
),
189+
on_start: Ok(
190+
Running,
191+
),
192+
on_door_open: Ok(
193+
OpenSettingTime,
194+
),
195+
on_door_close: Err(
196+
InvalidTransition {
197+
current: SettingTime,
198+
event: "on_door_close",
199+
},
200+
),
201+
on_time_finished: Err(
202+
InvalidTransition {
203+
current: SettingTime,
204+
event: "on_time_finished",
205+
},
206+
),
207+
},
208+
}

quic/s2n-quic-core/src/state/tests.rs

Lines changed: 59 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::*;
55
use insta::{assert_debug_snapshot, assert_snapshot};
66

77
#[derive(Clone, Debug, Default, PartialEq, Eq)]
8-
enum State {
8+
enum Lr {
99
#[default]
1010
Init,
1111
Left,
@@ -16,7 +16,7 @@ enum State {
1616
RightRight,
1717
}
1818

19-
impl State {
19+
impl Lr {
2020
event! {
2121
on_left(
2222
Init => Left,
@@ -33,12 +33,65 @@ impl State {
3333

3434
#[test]
3535
#[cfg_attr(miri, ignore)]
36-
fn snapshots() {
37-
assert_debug_snapshot!(State::test_transitions());
36+
fn lr_snapshots() {
37+
assert_debug_snapshot!(Lr::test_transitions());
3838
}
3939

4040
#[test]
4141
#[cfg_attr(miri, ignore)]
42-
fn dot_test() {
43-
assert_snapshot!(State::dot());
42+
fn lr_dot_test() {
43+
assert_snapshot!(Lr::dot());
44+
}
45+
46+
#[derive(Clone, Debug, Default, PartialEq, Eq)]
47+
enum Microwave {
48+
#[default]
49+
Idle,
50+
OpenIdle,
51+
SettingTime,
52+
OpenSettingTime,
53+
Paused,
54+
OpenPaused,
55+
Running,
56+
}
57+
58+
impl Microwave {
59+
event! {
60+
on_number(
61+
Idle | SettingTime => SettingTime,
62+
OpenSettingTime => OpenSettingTime,
63+
);
64+
on_cancel(
65+
Idle | SettingTime | Paused | Running => Idle,
66+
OpenIdle | OpenSettingTime | OpenPaused => OpenIdle,
67+
);
68+
on_start(
69+
SettingTime | Paused | Running => Running,
70+
);
71+
on_door_open(
72+
Idle => OpenIdle,
73+
SettingTime => OpenSettingTime,
74+
Paused | Running => OpenPaused,
75+
);
76+
on_door_close(
77+
OpenIdle => Idle,
78+
OpenSettingTime => SettingTime,
79+
OpenPaused => Paused,
80+
);
81+
on_time_finished(
82+
Running => Idle,
83+
);
84+
}
85+
}
86+
87+
#[test]
88+
#[cfg_attr(miri, ignore)]
89+
fn microwave_snapshots() {
90+
assert_debug_snapshot!(Microwave::test_transitions());
91+
}
92+
93+
#[test]
94+
#[cfg_attr(miri, ignore)]
95+
fn microwave_dot_test() {
96+
assert_snapshot!(Microwave::dot());
4497
}

quic/s2n-quic-core/src/stream/state/snapshots/s2n_quic_core__stream__state__recv__tests__dot_test.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source: quic/s2n-quic-core/src/stream/state/recv.rs
33
expression: "Receiver::dot()"
44
---
55
digraph {
6+
label = "s2n_quic_core::stream::state::recv::Receiver";
67
DataRead;
78
DataRecvd;
89
Recv;

quic/s2n-quic-core/src/stream/state/snapshots/s2n_quic_core__stream__state__send__tests__dot_test.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ source: quic/s2n-quic-core/src/stream/state/send.rs
33
expression: "Sender::dot()"
44
---
55
digraph {
6+
label = "s2n_quic_core::stream::state::send::Sender";
67
DataRecvd;
78
DataSent;
89
Ready;

0 commit comments

Comments
 (0)