Skip to content

Commit

Permalink
fix: tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikolas Haimerl committed Feb 6, 2024
1 parent a6b5739 commit 9fd1308
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 15 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

11 changes: 8 additions & 3 deletions ic-agent/src/agent/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ use crate::{
identity::Identity,
to_request_id, RequestId,
};
use backoff::exponential::ExponentialBackoff;
use backoff::{backoff::Backoff, ExponentialBackoffBuilder};
use backoff::{exponential::ExponentialBackoff, SystemClock};
use ic_certification::{Certificate, Delegation, Label};
use ic_transport_types::{
signed::{SignedQuery, SignedRequestStatus, SignedUpdate},
Expand Down Expand Up @@ -657,7 +657,7 @@ impl Agent {
}
}

fn get_retry_policy() -> ExponentialBackoff {
fn get_retry_policy() -> ExponentialBackoff<SystemClock> {
ExponentialBackoffBuilder::new()
.with_initial_interval(Duration::from_millis(500))
.with_max_interval(Duration::from_secs(1))
Expand All @@ -666,6 +666,7 @@ impl Agent {
.build()
}

/// Wait for request_status to return a Replied response and return the arg.
pub async fn wait_signed(
&self,
request_id: &RequestId,
Expand All @@ -677,7 +678,11 @@ impl Agent {
let mut request_accepted = false;
loop {
match self
.request_status_signed(request_id, effective_canister_id, signed_request_status)
.request_status_signed(
request_id,
effective_canister_id,
signed_request_status.clone(),
)
.await?
{
RequestStatusResponse::Unknown => {}
Expand Down
1 change: 1 addition & 0 deletions ref-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ tokio = { workspace = true, features = ["full"] }

[dev-dependencies]
serde_cbor = { workspace = true }
ic-certification = { workspace = true }
37 changes: 25 additions & 12 deletions ref-tests/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
//! integration tests with a running IC-Ref.
use candid::CandidType;
use ic_agent::{
agent::{agent_error::HttpErrorPayload, RejectCode, RejectResponse},
agent::{agent_error::HttpErrorPayload, Envelope, EnvelopeContent, RejectCode, RejectResponse},
export::Principal,
AgentError, Identity,
};
use ic_certification::Label;
use ic_utils::{
call::{AsyncCall, SyncCall},
interfaces::{
Expand All @@ -21,7 +22,12 @@ use ref_tests::{
get_wallet_wasm_from_env, universal_canister::payload, with_universal_canister,
with_wallet_canister,
};
use std::{alloc::System, sync::Arc};
use serde::Serialize;
use std::{
borrow::Cow,
sync::Arc,
time::{Duration, SystemTime, UNIX_EPOCH},
};

#[ignore]
#[test]
Expand Down Expand Up @@ -68,31 +74,37 @@ fn basic_expiry() {
#[ignore]
#[test]
fn wait_signed() {
with_universal_canister(|agent, canister_id| async move {
with_universal_canister(|mut agent, canister_id| async move {
fn serialized_bytes(envelope: Envelope) -> Vec<u8> {
let mut serialized_bytes = Vec::new();
let mut serializer = serde_cbor::Serializer::new(&mut serialized_bytes);
serializer.self_describe().unwrap();
envelope.serialize(&mut serializer).unwrap()
envelope.serialize(&mut serializer).unwrap();
serialized_bytes
}

let arg = payload().reply_data(b"hello").build();
let ingress_expiry = (SystemTime::now() + Duration::from_secs(120)).as_nanos() as u64;
let ingress_expiry = (SystemTime::now().duration_since(UNIX_EPOCH).unwrap()
+ Duration::from_secs(120))
.as_nanos() as u64;

let agent_identity = create_basic_identity().unwrap().into();
agent.set_arc_identity(agent_identity);
let agent_identity = Arc::new(create_basic_identity().unwrap());
agent.set_arc_identity(agent_identity.clone());

let call_envelope_content = EnvelopeContent::Call {
sender: agent.get_principal().unwrap(),
arg: arg.clone(),
ingress_expiry,
nonce: None,
canister_id,
method_name: "update".to_string(),
};

let call_request_id = call_envelope_content.to_request_id();
let call_signature = agent_identity.sign(call_envelope_content).unwrap();
let call_signature = agent_identity.sign(&call_envelope_content).unwrap();

let call_envelope = Envelope {
content: Cow::Borrowed(call_envelope_content),
content: Cow::Borrowed(&call_envelope_content),
sender_pubkey: call_signature.public_key,
sender_sig: call_signature.signature,
sender_delegation: call_signature.delegations,
Expand All @@ -102,6 +114,7 @@ fn wait_signed() {

agent
.update_signed(canister_id, call_envelope_serialized)
.await
.unwrap();

let paths: Vec<Vec<Label>> = vec![vec![
Expand All @@ -114,10 +127,10 @@ fn wait_signed() {
ingress_expiry,
};

let read_signature = agent_identity.sign(read_state_envelope_content).unwrap();
let read_signature = agent_identity.sign(&read_state_envelope_content).unwrap();

let read_state_envelope = Envelope {
content: Cow::Borrowed(call_envelope_serialized),
content: Cow::Borrowed(&read_state_envelope_content),
sender_pubkey: read_signature.public_key,
sender_sig: read_signature.signature,
sender_delegation: read_signature.delegations,
Expand All @@ -126,7 +139,7 @@ fn wait_signed() {
let read_envelope_serialized = serialized_bytes(read_state_envelope);

let result = agent
.wait_signed(call_request_id, canister_id, read_envelope_serialized)
.wait_signed(&call_request_id, canister_id, read_envelope_serialized)
.await
.unwrap();

Expand Down

0 comments on commit 9fd1308

Please sign in to comment.