1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#![allow(unused_imports, clippy::ptr_arg, clippy::needless_lifetimes)]
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::{borrow::Cow, io::Write, string::ToString};
use wasmbus_rpc::{
deserialize, serialize, Context, Message, MessageDispatch, RpcError, RpcResult, SendOpts,
Timestamp, Transport,
};
pub const SMITHY_VERSION: &str = "1.0";
pub type OptMap = std::collections::HashMap<String, String>;
pub type PatternList = Vec<String>;
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct TestOptions {
pub patterns: PatternList,
pub options: OptMap,
}
#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct TestResult {
#[serde(default)]
pub name: String,
#[serde(default)]
pub passed: bool,
#[serde(rename = "snapData")]
#[serde(with = "serde_bytes")]
#[serde(default, skip_serializing_if = "Option::is_none")]
pub snap_data: Option<Vec<u8>>,
}
pub type TestResults = Vec<TestResult>;
#[async_trait]
pub trait Testing {
fn contract_id() -> &'static str {
"wasmcloud:testing"
}
async fn start(&self, ctx: &Context, arg: &TestOptions) -> RpcResult<TestResults>;
}
#[doc(hidden)]
#[async_trait]
pub trait TestingReceiver: MessageDispatch + Testing {
async fn dispatch(&self, ctx: &Context, message: &Message<'_>) -> RpcResult<Message<'_>> {
match message.method {
"Start" => {
let value: TestOptions = deserialize(message.arg.as_ref())
.map_err(|e| RpcError::Deser(format!("message '{}': {}", message.method, e)))?;
let resp = Testing::start(self, ctx, &value).await?;
let buf = serialize(&resp)?;
Ok(Message {
method: "Testing.Start",
arg: Cow::Owned(buf),
})
}
_ => Err(RpcError::MethodNotHandled(format!(
"Testing::{}",
message.method
))),
}
}
}
#[derive(Debug)]
pub struct TestingSender<T: Transport> {
transport: T,
}
impl<T: Transport> TestingSender<T> {
pub fn via(transport: T) -> Self {
Self { transport }
}
pub fn set_timeout(&self, interval: std::time::Duration) {
self.transport.set_timeout(interval);
}
}
#[cfg(not(target_arch = "wasm32"))]
impl<'send> TestingSender<wasmbus_rpc::provider::ProviderTransport<'send>> {
pub fn for_actor(ld: &'send wasmbus_rpc::core::LinkDefinition) -> Self {
Self {
transport: wasmbus_rpc::provider::ProviderTransport::new(ld, None),
}
}
}
#[cfg(target_arch = "wasm32")]
impl TestingSender<wasmbus_rpc::actor::prelude::WasmHost> {
pub fn to_actor(actor_id: &str) -> Self {
let transport =
wasmbus_rpc::actor::prelude::WasmHost::to_actor(actor_id.to_string()).unwrap();
Self { transport }
}
}
#[cfg(target_arch = "wasm32")]
impl TestingSender<wasmbus_rpc::actor::prelude::WasmHost> {
pub fn new() -> Self {
let transport =
wasmbus_rpc::actor::prelude::WasmHost::to_provider("wasmcloud:testing", "default")
.unwrap();
Self { transport }
}
pub fn new_with_link(link_name: &str) -> wasmbus_rpc::RpcResult<Self> {
let transport =
wasmbus_rpc::actor::prelude::WasmHost::to_provider("wasmcloud:testing", link_name)?;
Ok(Self { transport })
}
}
#[async_trait]
impl<T: Transport + std::marker::Sync + std::marker::Send> Testing for TestingSender<T> {
#[allow(unused)]
async fn start(&self, ctx: &Context, arg: &TestOptions) -> RpcResult<TestResults> {
let buf = serialize(arg)?;
let resp = self
.transport
.send(
ctx,
Message {
method: "Testing.Start",
arg: Cow::Borrowed(&buf),
},
None,
)
.await?;
let value = deserialize(&resp)
.map_err(|e| RpcError::Deser(format!("response to {}: {}", "Start", e)))?;
Ok(value)
}
}