From be5df9ead1669c55052b1600d41c98d5d98ffc5e Mon Sep 17 00:00:00 2001 From: Youngjoon Lee <5462944+youngjoon-lee@users.noreply.github.com> Date: Fri, 20 Dec 2024 16:22:47 +0900 Subject: [PATCH] Fix netrunner tests (#61) --- .github/workflows/ci.yaml | 7 +++---- simlib/netrunner/src/network/mod.rs | 2 +- simlib/netrunner/src/network/regions.rs | 3 +-- simlib/netrunner/src/node/dummy_streaming.rs | 14 +++++++++++++- simlib/netrunner/src/node/mod.rs | 10 ++++++++-- simlib/netrunner/src/streaming/io.rs | 11 +++++------ .../netrunner/src/streaming/runtime_subscriber.rs | 10 ++++------ .../netrunner/src/streaming/settings_subscriber.rs | 4 +--- 8 files changed, 36 insertions(+), 25 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 7e98e87..1b1fbbe 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -20,7 +20,6 @@ jobs: - name: Build working-directory: simlib run: cargo build -v - # TODO: uncomment this after fixing tests - # - name: Unit tests - # working-directory: simlib - # run: cargo test -v + - name: Unit tests + working-directory: simlib + run: cargo test -v diff --git a/simlib/netrunner/src/network/mod.rs b/simlib/netrunner/src/network/mod.rs index 1fec225..e88d821 100644 --- a/simlib/netrunner/src/network/mod.rs +++ b/simlib/netrunner/src/network/mod.rs @@ -524,7 +524,7 @@ mod tests { impl PayloadSize for () { fn size_bytes(&self) -> u32 { - todo!() + 0 } } diff --git a/simlib/netrunner/src/network/regions.rs b/simlib/netrunner/src/network/regions.rs index 328deb4..185375b 100644 --- a/simlib/netrunner/src/network/regions.rs +++ b/simlib/netrunner/src/network/regions.rs @@ -156,7 +156,6 @@ pub fn create_regions( mod tests { use std::collections::HashMap; - use consensus_engine::NodeId; use rand::rngs::mock::StepRng; use crate::{ @@ -164,7 +163,7 @@ mod tests { regions::{create_regions, Region}, NetworkSettings, }, - node::NodeIdExt, + node::{NodeId, NodeIdExt}, }; #[test] diff --git a/simlib/netrunner/src/node/dummy_streaming.rs b/simlib/netrunner/src/node/dummy_streaming.rs index cdbf93b..3fce774 100644 --- a/simlib/netrunner/src/node/dummy_streaming.rs +++ b/simlib/netrunner/src/node/dummy_streaming.rs @@ -1,6 +1,8 @@ use serde::{Deserialize, Serialize}; use std::time::Duration; +use crate::warding::WardCondition; + use super::{Node, NodeId}; #[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)] @@ -40,6 +42,16 @@ impl Node for DummyStreamingNode { } fn step(&mut self, _: Duration) { - todo!() + self.state.counter += 1; + } + + fn analyze(&self, ward: &mut crate::warding::WardCondition) -> bool { + match ward { + WardCondition::Max(ward) => self.state.counter >= ward.max_count, + WardCondition::Sum(condition) => { + *condition.step_result.borrow_mut() += self.state.counter; + false + } + } } } diff --git a/simlib/netrunner/src/node/mod.rs b/simlib/netrunner/src/node/mod.rs index c5b1c0e..b107275 100644 --- a/simlib/netrunner/src/node/mod.rs +++ b/simlib/netrunner/src/node/mod.rs @@ -163,8 +163,14 @@ impl Node for usize { self.add_assign(1); } - fn analyze(&self, _: &mut WardCondition) -> bool { - todo!() + fn analyze(&self, ward: &mut WardCondition) -> bool { + match ward { + WardCondition::Max(ward) => *self >= ward.max_count, + WardCondition::Sum(condition) => { + *condition.step_result.borrow_mut() += *self; + false + } + } } } diff --git a/simlib/netrunner/src/streaming/io.rs b/simlib/netrunner/src/streaming/io.rs index 9332731..b79d2f6 100644 --- a/simlib/netrunner/src/streaming/io.rs +++ b/simlib/netrunner/src/streaming/io.rs @@ -117,8 +117,6 @@ where mod tests { use std::{collections::HashMap, time::Duration}; - use consensus_engine::View; - use crate::{ network::{ behaviour::NetworkBehaviour, @@ -135,12 +133,13 @@ mod tests { }; use super::*; + #[derive(Debug, Clone, Serialize)] - struct IORecord { - states: HashMap, + struct IORecord { + states: HashMap, } - impl TryFrom<&SimulationState> for IORecord { + impl TryFrom<&SimulationState> for IORecord { type Error = anyhow::Error; fn try_from(value: &SimulationState) -> Result { @@ -148,7 +147,7 @@ mod tests { Ok(Self { states: nodes .iter() - .map(|node| (node.id(), node.current_view())) + .map(|node| (node.id(), node.state().clone())) .collect(), }) } diff --git a/simlib/netrunner/src/streaming/runtime_subscriber.rs b/simlib/netrunner/src/streaming/runtime_subscriber.rs index 3f45c56..2b54fc2 100644 --- a/simlib/netrunner/src/streaming/runtime_subscriber.rs +++ b/simlib/netrunner/src/streaming/runtime_subscriber.rs @@ -103,8 +103,6 @@ where mod tests { use std::{collections::HashMap, time::Duration}; - use consensus_engine::View; - use crate::{ network::{ behaviour::NetworkBehaviour, @@ -122,11 +120,11 @@ mod tests { use super::*; #[derive(Debug, Clone, Serialize)] - struct RuntimeRecord { - states: HashMap, + struct RuntimeRecord { + states: HashMap, } - impl TryFrom<&SimulationState> for RuntimeRecord { + impl TryFrom<&SimulationState> for RuntimeRecord { type Error = anyhow::Error; fn try_from(value: &SimulationState) -> Result { @@ -135,7 +133,7 @@ mod tests { .nodes .read() .iter() - .map(|node| (node.id(), node.current_view())) + .map(|node| (node.id(), node.state().clone())) .collect(), }) } diff --git a/simlib/netrunner/src/streaming/settings_subscriber.rs b/simlib/netrunner/src/streaming/settings_subscriber.rs index 59105ce..1dd4ec9 100644 --- a/simlib/netrunner/src/streaming/settings_subscriber.rs +++ b/simlib/netrunner/src/streaming/settings_subscriber.rs @@ -102,9 +102,7 @@ where #[cfg(test)] mod tests { use std::collections::HashSet; - use std::{collections::HashMap, time::Duration}; - - type View = usize; + use std::time::Duration; use crate::{ network::{