Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: modify test_simple_signer to fix intermittent failure #4306

Merged
merged 1 commit into from
Jan 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 46 additions & 20 deletions libsigner/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,10 +96,11 @@ fn test_simple_signer() {
let ev = SignerEventReceiver::new(vec![contract_id.clone()], false);
let (_cmd_send, cmd_recv) = channel();
let (res_send, _res_recv) = channel();
let mut signer = Signer::new(SimpleRunLoop::new(5), ev, cmd_recv, res_send);
let max_events = 5;
let mut signer = Signer::new(SimpleRunLoop::new(max_events), ev, cmd_recv, res_send);
let endpoint: SocketAddr = "127.0.0.1:30000".parse().unwrap();
let mut chunks = vec![];
for i in 0..5 {
for i in 0..max_events {
let privk = Secp256k1PrivateKey::new();
let msg = wsts::net::Message::DkgBegin(DkgBegin { dkg_id: 0 });
let message = SignerMessage::Packet(Packet { msg, sig: vec![] });
Expand Down Expand Up @@ -138,24 +139,6 @@ fn test_simple_signer() {

num_sent += 1;
}
// Test the /status endpoint
{
let mut sock = match TcpStream::connect(endpoint) {
Ok(sock) => sock,
Err(..) => {
sleep_ms(100);
return;
}
};
let req = "GET /status HTTP/1.0\r\nConnection: close\r\n\r\n";
sock.write_all(req.as_bytes()).unwrap();
let mut buf = [0; 128];
sock.read(&mut buf).unwrap();
let res_str = std::str::from_utf8(&buf).unwrap();
let expected_status_res = "HTTP/1.0 200 OK\r\n";
assert_eq!(expected_status_res, &res_str[..expected_status_res.len()]);
sock.flush().unwrap();
}
});

let running_signer = signer.spawn(endpoint).unwrap();
Expand All @@ -181,3 +164,46 @@ fn test_simple_signer() {
assert_eq!(sent_events, accepted_events);
mock_stacks_node.join().unwrap();
}

#[test]
fn test_status_endpoint() {
let contract_id =
QualifiedContractIdentifier::parse("ST2DS4MSWSGJ3W9FBC6BVT0Y92S345HY8N3T6AV7R.signers")
.unwrap(); // TODO: change to boot_code_id(SIGNERS_NAME, false) when .signers is deployed
let ev = SignerEventReceiver::new(vec![contract_id.clone()], false);
let (_cmd_send, cmd_recv) = channel();
let (res_send, _res_recv) = channel();
let max_events = 1;
let mut signer = Signer::new(SimpleRunLoop::new(max_events), ev, cmd_recv, res_send);
let endpoint: SocketAddr = "127.0.0.1:31000".parse().unwrap();

// simulate a node that's trying to push data
let mock_stacks_node = thread::spawn(move || {
let mut sock = match TcpStream::connect(endpoint) {
Ok(sock) => sock,
Err(e) => {
eprint!("Error connecting to {}: {}", endpoint, e);
sleep_ms(100);
return;
}
};
let req = "GET /status HTTP/1.0\r\nConnection: close\r\n\r\n";

sock.write_all(req.as_bytes()).unwrap();
let mut buf = [0; 128];
sock.read(&mut buf).unwrap();
let res_str = std::str::from_utf8(&buf).unwrap();
let expected_status_res = "HTTP/1.0 200 OK\r\n";
assert_eq!(expected_status_res, &res_str[..expected_status_res.len()]);
sock.flush().unwrap();
});

let running_signer = signer.spawn(endpoint).unwrap();
sleep_ms(3000);
let accepted_events = running_signer.stop().unwrap();

let sent_events: Vec<SignerEvent> = vec![SignerEvent::StatusCheck];

assert_eq!(sent_events, accepted_events);
mock_stacks_node.join().unwrap();
}