Skip to content

Commit 3d2f2b7

Browse files
committed
add ab protocol to ci
1 parent b94df07 commit 3d2f2b7

File tree

5 files changed

+56
-44
lines changed

5 files changed

+56
-44
lines changed

.github/workflows/rust.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,16 @@ jobs:
7070
echo "make job failed"
7171
exit $status
7272
fi
73+
74+
- name: Run AB
75+
run: |
76+
cd examples/ab
77+
make node &
78+
sleep 1
79+
80+
timeout 6s make job || echo "make job terminated after timeout"
81+
status=$?
82+
if [ $status -ne 0 ] && [ $status -ne 124 ]; then
83+
echo "make job failed"
84+
exit $status
85+
fi

Cargo.lock

Lines changed: 0 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/ab/Cargo.toml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ crate-type = ["cdylib", "rlib"]
1313
[dependencies]
1414
tokio = { version = "1", features = ["full"] }
1515
bincode = { version = "2.0.0", features = ["serde"] }
16-
tokio-util = { version = "0.7", features = ["full"] }
1716
reactor-actor = { path = "../../actor" }
1817
reactor-macros = { path = "../../macros" }
1918
lazy_static = "1.5.0"
20-
env_logger = "0.11"
2119
serde_json = "1.0.140"
22-
rand = "0.9.1"
23-
log = "0.4.27"
24-
25-
[features]
26-
chaos = []
20+
log = "0.4.27"

examples/ab/Makefile

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ GENERIC_NCTRL = ../../generic_nctrl
22
GENERIC_JCTRL = ../../generic_jctrl
33
TARGET_SO = ../../target/debug/lib_ab_actor.so
44

5-
.PHONY: install install_node install_jobc chaos kill_node node job clean TARGET_SO
5+
.PHONY: install install_node install_jobc kill_node node job clean TARGET_SO
66

77

88
install_node: $(GENERIC_NCTRL)
@@ -11,9 +11,6 @@ install_node: $(GENERIC_NCTRL)
1111
install_jobc: $(GENERIC_JCTRL)
1212
cargo install --debug --path $(GENERIC_JCTRL)
1313

14-
chaos:
15-
cargo build --features chaos
16-
1714
kill_node:
1815
@echo "Killing process on port 3000 if any..."
1916
@lsof -ti :3000 | xargs --no-run-if-empty kill

examples/ab/src/lib.rs

Lines changed: 41 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,37 @@ use std::collections::HashMap;
88
use std::time::Duration;
99
use std::vec;
1010

11-
type Data = char;
12-
type Bit = bool;
11+
#[derive(Encode, Decode, Debug, Clone, Copy)]
12+
struct Data(char);
13+
#[derive(Encode, Decode, Debug, Clone, Eq, PartialEq, Copy)]
14+
struct Bit(bool);
15+
16+
impl Data {
17+
const MIN: Data = Data('A');
18+
const MAX: Data = Data('Z');
19+
}
20+
21+
impl Iterator for Data {
22+
type Item = Data;
23+
24+
fn next(&mut self) -> Option<Self::Item> {
25+
if self.0 < Data::MAX.0 {
26+
self.0 = ((self.0 as u8) + 1) as char;
27+
} else {
28+
self.0 = Data::MIN.0;
29+
}
30+
Some(self.clone())
31+
}
32+
}
33+
34+
impl Bit {
35+
const INIT: Bit = Bit(true);
36+
37+
fn negate(&mut self) -> Bit {
38+
self.0 = !self.0;
39+
self.clone()
40+
}
41+
}
1342

1443
#[derive(Encode, Decode, Debug, Clone, DefaultPrio, DeriveMsg)]
1544
enum ABMsg {
@@ -18,31 +47,13 @@ enum ABMsg {
1847
GeneratorMsg,
1948
}
2049

21-
struct GeneratorIter {
22-
current: u8,
23-
}
24-
impl GeneratorIter {
25-
const MIN: u8 = 0;
26-
const MAX: u8 = 25;
27-
28-
fn new() -> Self {
29-
GeneratorIter {
30-
current: GeneratorIter::MIN,
31-
}
32-
}
33-
}
50+
struct GeneratorIter;
3451
impl Iterator for GeneratorIter {
3552
type Item = ABMsg;
3653

3754
fn next(&mut self) -> Option<Self::Item> {
3855
std::thread::sleep(Duration::from_secs(4));
39-
40-
if self.current <= GeneratorIter::MAX {
41-
self.current += 1;
42-
Some(ABMsg::GeneratorMsg)
43-
} else {
44-
None
45-
}
56+
Some(ABMsg::GeneratorMsg)
4657
}
4758
}
4859

@@ -53,8 +64,8 @@ struct Writer {
5364
impl Writer {
5465
fn new() -> Self {
5566
Writer {
56-
data: 'A',
57-
bit: true,
67+
data: Data::MIN,
68+
bit: Bit::INIT,
5869
}
5970
}
6071
}
@@ -73,8 +84,8 @@ impl reactor_actor::ActorProcess for Writer {
7384
ABMsg::Ack(bit) => {
7485
info!("Writer: Recv: {input:?}");
7586
if bit == self.bit {
76-
self.data = ((self.data as u8) + 1) as char;
77-
self.bit = !self.bit;
87+
self.data.next();
88+
self.bit.negate();
7889
}
7990
vec![]
8091
}
@@ -90,8 +101,8 @@ struct Reader {
90101
impl Reader {
91102
fn new() -> Self {
92103
Reader {
93-
data: 'A',
94-
bit: true,
104+
data: Data::MIN,
105+
bit: Bit::INIT,
95106
}
96107
}
97108
}
@@ -151,7 +162,7 @@ pub fn writer(ctx: RuntimeCtx, mut payload: HashMap<String, serde_json::Value>)
151162
RUNTIME.spawn(async move {
152163
BehaviourBuilder::new(Writer::new(), BincodeCodec::default())
153164
.send(Sender::new(other_addr))
154-
.generator(GeneratorIter::new())
165+
.generator(GeneratorIter {})
155166
.build()
156167
.run(ctx)
157168
.await
@@ -171,7 +182,7 @@ pub fn reader(ctx: RuntimeCtx, mut payload: HashMap<String, serde_json::Value>)
171182
RUNTIME.spawn(async move {
172183
BehaviourBuilder::new(Reader::new(), BincodeCodec::default())
173184
.send(Sender::new(other_addr))
174-
.generator(GeneratorIter::new())
185+
.generator(GeneratorIter {})
175186
.build()
176187
.run(ctx)
177188
.await

0 commit comments

Comments
 (0)