Skip to content

Commit

Permalink
Run example as test (#16)
Browse files Browse the repository at this point in the history
Run example as test
  • Loading branch information
elshize authored Mar 28, 2021
1 parent 16ad72f commit a813544
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ jobs:
with:
rust-version: ${{ matrix.rust }}
- run: cargo test --verbose --workspace
- run: cargo run --example simulation
cargo-check:
name: Check for warnings
runs-on: ubuntu-latest
Expand Down Expand Up @@ -76,6 +77,7 @@ jobs:
- run: echo "PATH=/home/runner/.cargo/bin:$PATH" >> $GITHUB_ENV
- run: curl -L https://github.com/mozilla/grcov/releases/latest/download/grcov-linux-x86_64.tar.bz2 | tar jxf -
- run: cargo test --verbose --workspace
- run: cargo run --example simulation
- run: mkdir ./coverage
- run: ./grcov . --binary-path ./target/debug/ -s . -t lcov --branch --ignore-not-existing --ignore "*cargo*" --ignore "*example*" -o ./coverage/lcov.info
- name: Coveralls
Expand Down
1 change: 1 addition & 0 deletions codecov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ format="$1"

cargo clean
cargo test
cargo run --example simulation
rm -rf "$output"
grcov . --binary-path ./target/debug/ -s . -t "$format" --branch --ignore-not-existing \
--ignore "*cargo*" \
Expand Down
89 changes: 84 additions & 5 deletions examples/simulation.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use simrs::{Component, ComponentId, Fifo, Key, QueueId, Scheduler, Simulation, State};

use std::cell::RefCell;
use std::rc::Rc;
use std::time::Duration;

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

struct Consumer {
incoming: QueueId<Fifo<Product>>,
working_on: Key<Option<Product>>,
messages: Rc<RefCell<Vec<String>>>,
}

#[derive(Debug)]
Expand All @@ -31,14 +36,17 @@ impl Producer {
fn interval(&self) -> Duration {
Duration::from_secs(1)
}
fn log(&self) {
self.messages.borrow_mut().push(String::from("Produced"));
}
}

impl Consumer {
fn interval(&self) -> Duration {
Duration::from_secs(1)
}
fn log(&self, product: Product) {
println!("{:?}", product)
fn log(&self, _: Product) {
self.messages.borrow_mut().push(String::from("Consumed"));
}
}

Expand All @@ -55,6 +63,7 @@ impl Component for Producer {
let count = *state.get(self.produced_count).unwrap();
if count < 10 {
let _ = state.send(self.outgoing, self.produce());
self.log();
scheduler.schedule(self.interval(), self_id, ProducerEvent);
scheduler.schedule(Duration::default(), self.consumer, ConsumerEvent::Received);
*state.get_mut(self.produced_count).unwrap() = count + 1;
Expand Down Expand Up @@ -95,24 +104,94 @@ impl Component for Consumer {
}
}

const EXPECTED: &str = "Produced
0ns
0ns
Produced
1s
Consumed
1s
1s
1s
Produced
2s
Consumed
2s
2s
2s
Produced
3s
Consumed
3s
3s
3s
Produced
4s
Consumed
4s
4s
4s
Produced
5s
Consumed
5s
5s
5s
Produced
6s
Consumed
6s
6s
6s
Produced
7s
Consumed
7s
7s
7s
Produced
8s
Consumed
8s
8s
8s
Produced
9s
Consumed
9s
9s
9s
10s
Consumed
10s";

fn main() {
let messages = Rc::new(RefCell::new(Vec::<String>::new()));
let mut simulation = Simulation::default();
let queue = simulation.add_queue(Fifo::default());
let working_on = simulation.state.insert::<Option<Product>>(None);
let consumer = simulation.add_component(Consumer {
incoming: queue,
working_on,
messages: messages.clone(),
});
let produced_count = simulation.state.insert(0_usize);
let producer = simulation.add_component(Producer {
outgoing: queue,
consumer,
produced_count,
messages: messages.clone(),
});
simulation.schedule(Duration::new(0, 0), producer, ProducerEvent);
// simulation.schedule(Duration::new(0, 0), consumer, ProducerEvent);
// The above would fail with: ^^^^^^^^^^^^^ expected enum `ConsumerEvent`, found struct `ProducerEvent`
simulation.run(|sim| {
println!("{:?}", sim.scheduler.time());
});
{
let messages = messages.clone();
simulation.run(move |sim| {
messages
.borrow_mut()
.push(format!("{:?}", sim.scheduler.time()));
});
}
assert_eq!(*messages.borrow(), EXPECTED.split('\n').collect::<Vec<_>>());
}
8 changes: 8 additions & 0 deletions src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,14 @@ impl Scheduler {
mod test {
use super::*;

#[test]
fn test_clock_ref() {
let time = Duration::from_secs(1);
let clock = Clock::new(Cell::new(time));
let clock_ref = ClockRef::from(clock);
assert_eq!(clock_ref.time(), time);
}

#[test]
fn test_event_entry_debug() {
let entry = EventEntry {
Expand Down

0 comments on commit a813544

Please sign in to comment.