Skip to content

Commit

Permalink
Add protocol test runner
Browse files Browse the repository at this point in the history
  • Loading branch information
DmitryAstafyev committed Dec 5, 2024
1 parent 55a2495 commit f8aa677
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 15 deletions.
2 changes: 1 addition & 1 deletion application/apps/indexer/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 21 additions & 8 deletions application/apps/indexer/stypes/src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
use std::{env::temp_dir, path::PathBuf};

use proptest::prelude::*;

pub const TESTS_USECASE_COUNT: usize = 100;
pub const OUTPUT_PATH_DEFAULT: &str = "stypes_test";
pub const OUTPUT_PATH_ENVVAR: &str = "CHIPMUNK_PROTOCOL_TEST_OUTPUT";

pub fn rnd_usize() -> BoxedStrategy<usize> {
any::<u32>().prop_map(|n| n as usize).boxed()
}
pub fn rnd_u64() -> BoxedStrategy<u64> {
any::<u32>().prop_map(|n| n as u64).boxed()
}
pub fn get_output_path() -> PathBuf {
std::env::var(OUTPUT_PATH_ENVVAR)
.map_err(|err| err.to_string())
.and_then(|s| {
if s.is_empty() {
Err(String::from("Default output folder will be used"))
} else {
Ok(s)
}
})
.map(PathBuf::from)
.unwrap_or_else(|_| temp_dir().join(OUTPUT_PATH_DEFAULT))
}

#[macro_export]
macro_rules! test_msg {
Expand All @@ -23,12 +40,11 @@ macro_rules! test_msg {
#[allow(non_snake_case)]
#[test]
fn [< write_test_data_for_ $type >](cases in proptest::collection::vec($type::arbitrary(), $exp_count)) {
use std::{env::temp_dir};
use std::fs::{File, create_dir_all};
use std::io::{Write};
use remove_dir_all::remove_dir_all;

let dest = temp_dir().join("stypes_test").join(stringify!($type));
let dest = get_output_path().join(stringify!($type));
if dest.exists() {
remove_dir_all(&dest).expect("Folder for tests has been cleaned");
}
Expand Down Expand Up @@ -68,12 +84,11 @@ macro_rules! test_msg {
#[allow(non_snake_case)]
#[test]
fn [< write_test_data_for_ $type Void >](cases in proptest::collection::vec($type::<()>::arbitrary(), $exp_count)) {
use std::{env::temp_dir};
use std::fs::{File, create_dir_all};
use std::io::{Write};
use remove_dir_all::remove_dir_all;

let dest = temp_dir().join("stypes_test").join(format!("{}_Void",stringify!($type)));
let dest = get_output_path().join(format!("{}_Void",stringify!($type)));
if dest.exists() {
remove_dir_all(&dest).expect("Folder for tests has been cleaned");
}
Expand Down Expand Up @@ -113,12 +128,11 @@ macro_rules! test_msg {
#[allow(non_snake_case)]
#[test]
fn [< write_test_data_for_ $type $generic >](cases in proptest::collection::vec($type::<$generic>::arbitrary(), $exp_count)) {
use std::{env::temp_dir};
use std::fs::{File, create_dir_all};
use std::io::{Write};
use remove_dir_all::remove_dir_all;

let dest = temp_dir().join("stypes_test").join(format!("{}_{}",stringify!($type), stringify!($generic)));
let dest = get_output_path().join(format!("{}_{}",stringify!($type), stringify!($generic)));
if dest.exists() {
remove_dir_all(&dest).expect("Folder for tests has been cleaned");
}
Expand Down Expand Up @@ -155,12 +169,11 @@ macro_rules! test_msg {
#[allow(non_snake_case)]
#[test]
fn [< write_test_data_for_ $type $generic $nested>](cases in proptest::collection::vec($type::<$generic<$nested>>::arbitrary(), $exp_count)) {
use std::{env::temp_dir};
use std::fs::{File, create_dir_all};
use std::io::{Write};
use remove_dir_all::remove_dir_all;

let dest = temp_dir().join("stypes_test").join(format!("{}_{}_{}",stringify!($type), stringify!($generic), stringify!($nested)));
let dest = get_output_path().join(format!("{}_{}_{}",stringify!($type), stringify!($generic), stringify!($nested)));
if dest.exists() {
remove_dir_all(&dest).expect("Folder for tests has been cleaned");
}
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ wasm-bindgen = "0.2"
serde = { version = "1.0", features = ["derive"] }
thiserror = "2.0"
wasm-bindgen-test = "0.3"
stypes = { path = "../../indexer/stypes"}
stypes = { path = "../indexer/stypes"}
paste = "1.0"

File renamed without changes.
File renamed without changes.
File renamed without changes.
36 changes: 36 additions & 0 deletions application/apps/protocol/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#!/bin/bash

reset
echo "
====== WARNING ===============================================================
This script performs an operation to DELETE ALL the contents of
the folder specified in the environment variable CHIPMUNK_PROTOCOL_TEST_OUTPUT.
Before proceeding, make sure that the CHIPMUNK_PROTOCOL_TEST_OUTPUT variable
contains the correct path. If the CHIPMUNK_PROTOCOL_TEST_OUTPUT variable is
not defined, test data will be written to /\$TMP/stypes_tests.
====== WARNING ===============================================================
"
read -p "Do you want to continue? (y/N): " response

response=${response,,}
if [[ "$response" != "y" ]]; then
echo "Operation aborted."
exit 1
fi

echo "Build wasm module"
cargo clean
wasm-pack build --target nodejs

echo "Create test use-cases"
cd ../indexer/stypes
export CHIPMUNK_PROTOCOL_TEST_OUTPUT="/tmp/stypes_test/"
cargo test --release -- --nocapture

echo "Run tests"
export JASMIN_TEST_BLOCKS_LOGS=on
cd ../../rustcore/ts-bindings
rm -rf ./node_modules
rm -rf ./spec/build
rake bindings:test:protocol

2 changes: 1 addition & 1 deletion application/apps/rustcore/ts-bindings/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
},
"dependencies": {
"platform": "link:../../../platform",
"protocol": "link:../../protocol/wasm/pkg",
"protocol": "link:../../protocol/pkg",
"tslib": "^2.6.2",
"uuid": "^9.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ const MAP: { [key: string]: (buf: Uint8Array) => any } = {
UDPTransportConfig: protocol.decodeUDPTransportConfig,
};

const OUTPUT_PATH_ENVVAR = 'CHIPMUNK_PROTOCOL_TEST_OUTPUT';

describe('Protocol', function () {
it(config.regular.list[1], function () {
return runners.noSession(config.regular, 1, async (logger, done) => {
Expand Down Expand Up @@ -230,7 +232,7 @@ describe('Protocol', function () {
const decoded = protocol.decodeObserveOptions(bytes);
expect(deepEqualObj(decoded, origin)).toBe(true);
}
const casesPath = process.env['PROTOCOL_TEST_CASES_PATH'];
const casesPath = process.env[OUTPUT_PATH_ENVVAR];
if (typeof casesPath !== 'string' || casesPath.trim() === '') {
logger.info('Testing of all use-cases is skipped');
return finish(undefined, done);
Expand Down
6 changes: 3 additions & 3 deletions application/apps/rustcore/ts-bindings/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3385,9 +3385,9 @@ __metadata:
languageName: node
linkType: hard

"protocol@link:../../protocol/wasm/pkg::locator=rustcore%40workspace%3A.":
"protocol@link:../../protocol/pkg::locator=rustcore%40workspace%3A.":
version: 0.0.0-use.local
resolution: "protocol@link:../../protocol/wasm/pkg::locator=rustcore%40workspace%3A."
resolution: "protocol@link:../../protocol/pkg::locator=rustcore%40workspace%3A."
languageName: node
linkType: soft

Expand Down Expand Up @@ -3587,7 +3587,7 @@ __metadata:
jasmine: "npm:^5.1.0"
loglevel: "npm:^1.6.6"
platform: "link:../../../platform"
protocol: "link:../../protocol/wasm/pkg"
protocol: "link:../../protocol/pkg"
tmp: "npm:^0.2.3"
ts-node: "npm:^10.4.0"
tslib: "npm:^2.6.2"
Expand Down

0 comments on commit f8aa677

Please sign in to comment.