Skip to content

Commit a813544

Browse files
authored
Run example as test (#16)
Run example as test
1 parent 16ad72f commit a813544

File tree

4 files changed

+95
-5
lines changed

4 files changed

+95
-5
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ jobs:
2323
with:
2424
rust-version: ${{ matrix.rust }}
2525
- run: cargo test --verbose --workspace
26+
- run: cargo run --example simulation
2627
cargo-check:
2728
name: Check for warnings
2829
runs-on: ubuntu-latest
@@ -76,6 +77,7 @@ jobs:
7677
- run: echo "PATH=/home/runner/.cargo/bin:$PATH" >> $GITHUB_ENV
7778
- run: curl -L https://github.com/mozilla/grcov/releases/latest/download/grcov-linux-x86_64.tar.bz2 | tar jxf -
7879
- run: cargo test --verbose --workspace
80+
- run: cargo run --example simulation
7981
- run: mkdir ./coverage
8082
- run: ./grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "*cargo*" --ignore "*example*" -o ./coverage/lcov.info
8183
- name: Coveralls

codecov.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ format="$1"
1414

1515
cargo clean
1616
cargo test
17+
cargo run --example simulation
1718
rm -rf "$output"
1819
grcov . --binary-path ./target/debug/ -s . -t "$format" --branch --ignore-not-existing \
1920
--ignore "*cargo*" \

examples/simulation.rs

Lines changed: 84 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
use simrs::{Component, ComponentId, Fifo, Key, QueueId, Scheduler, Simulation, State};
2+
3+
use std::cell::RefCell;
4+
use std::rc::Rc;
25
use std::time::Duration;
36

47
#[derive(Debug)]
@@ -8,11 +11,13 @@ struct Producer {
811
outgoing: QueueId<Fifo<Product>>,
912
consumer: ComponentId<ConsumerEvent>,
1013
produced_count: Key<usize>,
14+
messages: Rc<RefCell<Vec<String>>>,
1115
}
1216

1317
struct Consumer {
1418
incoming: QueueId<Fifo<Product>>,
1519
working_on: Key<Option<Product>>,
20+
messages: Rc<RefCell<Vec<String>>>,
1621
}
1722

1823
#[derive(Debug)]
@@ -31,14 +36,17 @@ impl Producer {
3136
fn interval(&self) -> Duration {
3237
Duration::from_secs(1)
3338
}
39+
fn log(&self) {
40+
self.messages.borrow_mut().push(String::from("Produced"));
41+
}
3442
}
3543

3644
impl Consumer {
3745
fn interval(&self) -> Duration {
3846
Duration::from_secs(1)
3947
}
40-
fn log(&self, product: Product) {
41-
println!("{:?}", product)
48+
fn log(&self, _: Product) {
49+
self.messages.borrow_mut().push(String::from("Consumed"));
4250
}
4351
}
4452

@@ -55,6 +63,7 @@ impl Component for Producer {
5563
let count = *state.get(self.produced_count).unwrap();
5664
if count < 10 {
5765
let _ = state.send(self.outgoing, self.produce());
66+
self.log();
5867
scheduler.schedule(self.interval(), self_id, ProducerEvent);
5968
scheduler.schedule(Duration::default(), self.consumer, ConsumerEvent::Received);
6069
*state.get_mut(self.produced_count).unwrap() = count + 1;
@@ -95,24 +104,94 @@ impl Component for Consumer {
95104
}
96105
}
97106

107+
const EXPECTED: &str = "Produced
108+
0ns
109+
0ns
110+
Produced
111+
1s
112+
Consumed
113+
1s
114+
1s
115+
1s
116+
Produced
117+
2s
118+
Consumed
119+
2s
120+
2s
121+
2s
122+
Produced
123+
3s
124+
Consumed
125+
3s
126+
3s
127+
3s
128+
Produced
129+
4s
130+
Consumed
131+
4s
132+
4s
133+
4s
134+
Produced
135+
5s
136+
Consumed
137+
5s
138+
5s
139+
5s
140+
Produced
141+
6s
142+
Consumed
143+
6s
144+
6s
145+
6s
146+
Produced
147+
7s
148+
Consumed
149+
7s
150+
7s
151+
7s
152+
Produced
153+
8s
154+
Consumed
155+
8s
156+
8s
157+
8s
158+
Produced
159+
9s
160+
Consumed
161+
9s
162+
9s
163+
9s
164+
10s
165+
Consumed
166+
10s";
167+
98168
fn main() {
169+
let messages = Rc::new(RefCell::new(Vec::<String>::new()));
99170
let mut simulation = Simulation::default();
100171
let queue = simulation.add_queue(Fifo::default());
101172
let working_on = simulation.state.insert::<Option<Product>>(None);
102173
let consumer = simulation.add_component(Consumer {
103174
incoming: queue,
104175
working_on,
176+
messages: messages.clone(),
105177
});
106178
let produced_count = simulation.state.insert(0_usize);
107179
let producer = simulation.add_component(Producer {
108180
outgoing: queue,
109181
consumer,
110182
produced_count,
183+
messages: messages.clone(),
111184
});
112185
simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
113186
// simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
114187
// The above would fail with: ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
115-
simulation.run(|sim| {
116-
println!("{:?}", sim.scheduler.time());
117-
});
188+
{
189+
let messages = messages.clone();
190+
simulation.run(move |sim| {
191+
messages
192+
.borrow_mut()
193+
.push(format!("{:?}", sim.scheduler.time()));
194+
});
195+
}
196+
assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
118197
}

src/scheduler.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,14 @@ impl Scheduler {
171171
mod test {
172172
use super::*;
173173

174+
#[test]
175+
fn test_clock_ref() {
176+
let time = Duration::from_secs(1);
177+
let clock = Clock::new(Cell::new(time));
178+
let clock_ref = ClockRef::from(clock);
179+
assert_eq!(clock_ref.time(), time);
180+
}
181+
174182
#[test]
175183
fn test_event_entry_debug() {
176184
let entry = EventEntry {

0 commit comments

Comments
 (0)