Skip to content

Commit be5df9e

Browse files
Fix netrunner tests (#61)
1 parent be30404 commit be5df9e

File tree

8 files changed

+36
-25
lines changed

8 files changed

+36
-25
lines changed

.github/workflows/ci.yaml

+3-4
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ jobs:
2020
- name: Build
2121
working-directory: simlib
2222
run: cargo build -v
23-
# TODO: uncomment this after fixing tests
24-
# - name: Unit tests
25-
# working-directory: simlib
26-
# run: cargo test -v
23+
- name: Unit tests
24+
working-directory: simlib
25+
run: cargo test -v

simlib/netrunner/src/network/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ mod tests {
524524

525525
impl PayloadSize for () {
526526
fn size_bytes(&self) -> u32 {
527-
todo!()
527+
0
528528
}
529529
}
530530

simlib/netrunner/src/network/regions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -156,15 +156,14 @@ pub fn create_regions<R: Rng>(
156156
mod tests {
157157
use std::collections::HashMap;
158158

159-
use consensus_engine::NodeId;
160159
use rand::rngs::mock::StepRng;
161160

162161
use crate::{
163162
network::{
164163
regions::{create_regions, Region},
165164
NetworkSettings,
166165
},
167-
node::NodeIdExt,
166+
node::{NodeId, NodeIdExt},
168167
};
169168

170169
#[test]

simlib/netrunner/src/node/dummy_streaming.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
use serde::{Deserialize, Serialize};
22
use std::time::Duration;
33

4+
use crate::warding::WardCondition;
5+
46
use super::{Node, NodeId};
57

68
#[derive(Debug, Default, Copy, Clone, Serialize, Deserialize)]
@@ -40,6 +42,16 @@ impl<S> Node for DummyStreamingNode<S> {
4042
}
4143

4244
fn step(&mut self, _: Duration) {
43-
todo!()
45+
self.state.counter += 1;
46+
}
47+
48+
fn analyze(&self, ward: &mut crate::warding::WardCondition) -> bool {
49+
match ward {
50+
WardCondition::Max(ward) => self.state.counter >= ward.max_count,
51+
WardCondition::Sum(condition) => {
52+
*condition.step_result.borrow_mut() += self.state.counter;
53+
false
54+
}
55+
}
4456
}
4557
}

simlib/netrunner/src/node/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -163,8 +163,14 @@ impl Node for usize {
163163
self.add_assign(1);
164164
}
165165

166-
fn analyze(&self, _: &mut WardCondition) -> bool {
167-
todo!()
166+
fn analyze(&self, ward: &mut WardCondition) -> bool {
167+
match ward {
168+
WardCondition::Max(ward) => *self >= ward.max_count,
169+
WardCondition::Sum(condition) => {
170+
*condition.step_result.borrow_mut() += *self;
171+
false
172+
}
173+
}
168174
}
169175
}
170176

simlib/netrunner/src/streaming/io.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,6 @@ where
117117
mod tests {
118118
use std::{collections::HashMap, time::Duration};
119119

120-
use consensus_engine::View;
121-
122120
use crate::{
123121
network::{
124122
behaviour::NetworkBehaviour,
@@ -135,20 +133,21 @@ mod tests {
135133
};
136134

137135
use super::*;
136+
138137
#[derive(Debug, Clone, Serialize)]
139-
struct IORecord {
140-
states: HashMap<NodeId, View>,
138+
struct IORecord<T> {
139+
states: HashMap<NodeId, T>,
141140
}
142141

143-
impl<S, T: Serialize> TryFrom<&SimulationState<S, T>> for IORecord {
142+
impl<S, T: Serialize + Clone> TryFrom<&SimulationState<S, T>> for IORecord<T> {
144143
type Error = anyhow::Error;
145144

146145
fn try_from(value: &SimulationState<S, T>) -> Result<Self, Self::Error> {
147146
let nodes = value.nodes.read();
148147
Ok(Self {
149148
states: nodes
150149
.iter()
151-
.map(|node| (node.id(), node.current_view()))
150+
.map(|node| (node.id(), node.state().clone()))
152151
.collect(),
153152
})
154153
}

simlib/netrunner/src/streaming/runtime_subscriber.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -103,8 +103,6 @@ where
103103
mod tests {
104104
use std::{collections::HashMap, time::Duration};
105105

106-
use consensus_engine::View;
107-
108106
use crate::{
109107
network::{
110108
behaviour::NetworkBehaviour,
@@ -122,11 +120,11 @@ mod tests {
122120

123121
use super::*;
124122
#[derive(Debug, Clone, Serialize)]
125-
struct RuntimeRecord {
126-
states: HashMap<NodeId, View>,
123+
struct RuntimeRecord<T> {
124+
states: HashMap<NodeId, T>,
127125
}
128126

129-
impl<S, T: Serialize> TryFrom<&SimulationState<S, T>> for RuntimeRecord {
127+
impl<S, T: Serialize + Clone> TryFrom<&SimulationState<S, T>> for RuntimeRecord<T> {
130128
type Error = anyhow::Error;
131129

132130
fn try_from(value: &SimulationState<S, T>) -> Result<Self, Self::Error> {
@@ -135,7 +133,7 @@ mod tests {
135133
.nodes
136134
.read()
137135
.iter()
138-
.map(|node| (node.id(), node.current_view()))
136+
.map(|node| (node.id(), node.state().clone()))
139137
.collect(),
140138
})
141139
}

simlib/netrunner/src/streaming/settings_subscriber.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -102,9 +102,7 @@ where
102102
#[cfg(test)]
103103
mod tests {
104104
use std::collections::HashSet;
105-
use std::{collections::HashMap, time::Duration};
106-
107-
type View = usize;
105+
use std::time::Duration;
108106

109107
use crate::{
110108
network::{

0 commit comments

Comments
 (0)