Skip to content

Commit 3593b81

Browse files
authored
Merge pull request #2 from katanemo/further-abi-0.2.0-work
Add Stats testing functionality
2 parents a13897a + 57e21ba commit 3593b81

File tree

4 files changed

+325
-53
lines changed

4 files changed

+325
-53
lines changed

src/expectations.rs

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,10 @@ pub struct Expect {
8686
Option<Duration>,
8787
Option<u32>,
8888
)>,
89+
metrics_create: Vec<(i32, String)>,
90+
metrics_increment: Vec<(i32, i64)>,
91+
metrics_record: Vec<(i32, u64)>,
92+
metrics_get: Vec<(i32, u64)>,
8993
}
9094

9195
impl Expect {
@@ -106,6 +110,10 @@ impl Expect {
106110
add_header_map_value: vec![],
107111
send_local_response: vec![],
108112
http_call: vec![],
113+
metrics_create: vec![],
114+
metrics_increment: vec![],
115+
metrics_record: vec![],
116+
metrics_get: vec![],
109117
}
110118
}
111119

@@ -571,4 +579,92 @@ impl Expect {
571579
}
572580
}
573581
}
582+
583+
pub fn set_expect_metric_create(&mut self, metric_type: i32, name: &str) {
584+
self.expect_count += 1;
585+
self.metrics_create.push((metric_type, name.to_string()));
586+
}
587+
588+
pub fn get_expect_metric_create(&mut self, metric_type: i32, name: &str) {
589+
match self.metrics_create.len() {
590+
0 => {
591+
if !self.allow_unexpected {
592+
self.expect_count -= 1;
593+
}
594+
set_status(ExpectStatus::Unexpected);
595+
}
596+
_ => {
597+
self.expect_count -= 1;
598+
let expected_metric_type = self.metrics_create.remove(0);
599+
let expect_status = expected_metric_type == (metric_type, name.to_string());
600+
set_expect_status(expect_status);
601+
}
602+
}
603+
}
604+
605+
pub fn set_expect_metric_increment(&mut self, metric_id: i32, offset: i64) {
606+
self.expect_count += 1;
607+
self.metrics_increment.push((metric_id, offset));
608+
}
609+
610+
pub fn get_expect_metric_increment(&mut self, metric_id: i32, offset: i64) {
611+
match self.metrics_increment.len() {
612+
0 => {
613+
if !self.allow_unexpected {
614+
self.expect_count -= 1;
615+
}
616+
set_status(ExpectStatus::Unexpected);
617+
}
618+
_ => {
619+
self.expect_count -= 1;
620+
let expected_metric_increment_tuple = self.metrics_increment.remove(0);
621+
let expect_status = expected_metric_increment_tuple == (metric_id, offset);
622+
set_expect_status(expect_status);
623+
}
624+
}
625+
}
626+
627+
pub fn set_expect_metric_record(&mut self, metric_id: i32, value: u64) {
628+
self.expect_count += 1;
629+
self.metrics_record.push((metric_id, value));
630+
}
631+
632+
pub fn get_expect_metric_record(&mut self, metric_id: i32, value: u64) {
633+
match self.metrics_record.len() {
634+
0 => {
635+
if !self.allow_unexpected {
636+
self.expect_count -= 1;
637+
}
638+
set_status(ExpectStatus::Unexpected);
639+
}
640+
_ => {
641+
self.expect_count -= 1;
642+
let expected_metric_record_tuple = self.metrics_record.remove(0);
643+
let expect_status = expected_metric_record_tuple == (metric_id, value);
644+
set_expect_status(expect_status);
645+
}
646+
}
647+
}
648+
649+
pub fn set_expect_metric_get(&mut self, metric_id: i32, value: u64) {
650+
self.expect_count += 1;
651+
self.metrics_get.push((metric_id, value));
652+
}
653+
654+
pub fn get_expect_metric_get(&mut self, metric_id: i32, value: u64) {
655+
match self.metrics_get.len() {
656+
0 => {
657+
if !self.allow_unexpected {
658+
self.expect_count -= 1;
659+
}
660+
set_status(ExpectStatus::Unexpected);
661+
}
662+
_ => {
663+
self.expect_count -= 1;
664+
let expected_get_metric_tuple = self.metrics_get.remove(0);
665+
let expect_status = expected_get_metric_tuple == (metric_id, value);
666+
set_expect_status(expect_status);
667+
}
668+
}
669+
}
574670
}

src/host_settings.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ use crate::hostcalls::serial_utils::serialize_map;
1616
use crate::types::*;
1717

1818
use std::collections::HashMap;
19+
use std::convert::TryFrom;
20+
use std::convert::TryInto;
1921
use std::time::Duration;
2022

2123
// Global structure for handling default host behaviour (and high-level expectation setting)
@@ -48,6 +50,8 @@ pub struct HostSettings {
4850
tick_period_millis: Duration,
4951
header_map_pairs: HashMap<i32, Vec<(String, String)>>,
5052
buffer_bytes: HashMap<i32, Bytes>,
53+
metrics_value: HashMap<i32, i64>,
54+
metrics_ids: HashMap<String, i32>,
5155
}
5256

5357
impl HostSettings {
@@ -59,6 +63,8 @@ impl HostSettings {
5963
tick_period_millis: Duration::new(0, 0),
6064
header_map_pairs: default_header_map_pairs(),
6165
buffer_bytes: default_buffer_bytes(),
66+
metrics_value: HashMap::new(),
67+
metrics_ids: HashMap::new(),
6268
}
6369
}
6470

@@ -194,6 +200,32 @@ impl HostSettings {
194200
}
195201
self.header_map_pairs.insert(map_type, new_header_map);
196202
}
203+
204+
pub fn create_metric(&mut self, name: &str) -> i32 {
205+
let metric_id: i32 = self.metrics_value.len().try_into().unwrap();
206+
self.metrics_value.insert(metric_id, 0);
207+
self.metrics_ids.insert(name.to_string(), metric_id);
208+
metric_id
209+
}
210+
211+
pub fn increment_metric(&mut self, metric_id: i32, offset: i64) {
212+
let value = self.metrics_value.get_mut(&metric_id).unwrap();
213+
*value += offset;
214+
}
215+
216+
pub fn record_metric(&mut self, metric_id: i32, new_value: i64) {
217+
let value = self.metrics_value.get_mut(&metric_id).unwrap();
218+
*value = new_value;
219+
}
220+
221+
pub fn get_metric(&self, metric_id: i32) -> u64 {
222+
let value = *self.metrics_value.get(&metric_id).unwrap();
223+
u64::try_from(value).unwrap()
224+
}
225+
226+
pub fn get_metric_id(&self, name: &str) -> i32 {
227+
*self.metrics_ids.get(name).unwrap()
228+
}
197229
}
198230

199231
// functions to retrieve default values

0 commit comments

Comments
 (0)