Skip to content

Commit

Permalink
fix(spells): update trigger config by alias [NET-418] (#1521)
Browse files Browse the repository at this point in the history
  • Loading branch information
justprosh committed Mar 27, 2023
1 parent 016cb5f commit 0531848
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 4 deletions.
87 changes: 87 additions & 0 deletions crates/particle-node-tests/tests/spells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1634,3 +1634,90 @@ async fn create_remove_worker() {
panic!("expected one string result")
}
}

#[tokio::test]
async fn spell_update_trigger_by_alias() {
let swarms = make_swarms(1).await;
let mut client = ConnectedClient::connect_with_keypair(
swarms[0].multiaddr.clone(),
Some(swarms[0].management_keypair.clone()),
)
.await
.wrap_err("connect client")
.unwrap();

let script = format!(
r#"
(seq
(seq
(call %init_peer_id% ("getDataSrv" "spell_id") [] spell_id)
(call %init_peer_id% (spell_id "get_u32") ["counter"] counter)
)
(call "{}" ("return" "") [spell_id])
)
"#,
client.peer_id
);

let mut config = TriggerConfig::default();
config.connections.connect = true;
let (spell_id, worker) = create_spell(&mut client, &script, config, json!({})).await;

let mut new_config = TriggerConfig::default();
new_config.connections.connect = true;
new_config.connections.disconnect = true;

let id = client.send_particle(
r#"(seq
(seq
(call relay ("op" "noop") [])
(call worker ("srv" "add_alias") ["alias" spell_id])
)
(seq
(call worker ("spell" "update_trigger_config") ["alias" config])
(call %init_peer_id% ("return" "") ["ok"])
)
)"#,
hashmap! {
"relay" => json!(client.node.to_string()),
"worker" => json!(worker),
"spell_id" => json!(spell_id.clone()),
"config" => json!(new_config)
},
);

client.wait_particle_args(id).await.unwrap();

// This connect should trigger the spell
let connect_num = 3;
for _ in 0..connect_num {
ConnectedClient::connect_to(swarms[0].multiaddr.clone())
.await
.unwrap();
}

let mut trigger_counter = 0;

// we must receive `connect_num` messages on connect and `connect_num` messages
// on disconnect, so 2 * `connect_num` messages in total
for _ in 0..2 * connect_num {
if let [spell_reply] = client
.receive_args()
.await
.wrap_err("receive")
.unwrap()
.as_slice()
{
let given_spell_id = spell_reply.as_str().unwrap();
assert_eq!(spell_id, given_spell_id);

trigger_counter += 1;
}
}

assert_eq!(
trigger_counter,
connect_num * 2,
"spell must be triggered {connect_num} * 2 times"
);
}
12 changes: 8 additions & 4 deletions sorcerer/src/spells.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ pub(crate) async fn spell_update_config(
key_manager: KeyManager,
) -> Result<(), JError> {
let mut args = args.function_args.into_iter();
let spell_id: String = Args::next("spell_id", &mut args)?;
let spell_id_or_alias: String = Args::next("spell_id", &mut args)?;

let worker_id = params.host_id;
let init_peer_id = params.init_peer_id;
Expand All @@ -220,10 +220,12 @@ pub(crate) async fn spell_update_config(

if !is_worker_creator && !is_worker && !is_management {
return Err(JError::new(format!(
"Failed to update spell config {spell_id}, spell config can be updated by worker creator {worker_creator}, worker itself {worker_id} or peer manager; init_peer_id={init_peer_id}"
"Failed to update spell config {spell_id_or_alias}, spell config can be updated by worker creator {worker_creator}, worker itself {worker_id} or peer manager; init_peer_id={init_peer_id}"
)));
}

let spell_id = services.to_service_id(worker_id, spell_id_or_alias.clone())?;

let user_config: TriggerConfig = Args::next("config", &mut args)?;
let config = api::from_user_config(user_config.clone())?;

Expand Down Expand Up @@ -253,9 +255,11 @@ pub(crate) async fn spell_update_config(
};

if let Err(err) = result {
log::warn!("can't update a spell {spell_id} config via spell-event-bus-api: {err}");
log::warn!(
"can't update a spell {spell_id_or_alias} config via spell-event-bus-api: {err}"
);
return Err(JError::new(format!(
"can't update a spell {spell_id} config due to an internal error while updating the triggers: {err}"
"can't update a spell {spell_id_or_alias} config due to an internal error while updating the triggers: {err}"
)));
}

Expand Down

0 comments on commit 0531848

Please sign in to comment.