-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathintegration_test.rs
More file actions
429 lines (372 loc) · 14.5 KB
/
Copy pathintegration_test.rs
File metadata and controls
429 lines (372 loc) · 14.5 KB
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use anyhow::{Error, Result};
use integration_tests::prepare_example;
use std::{fmt::Display, sync::LazyLock};
use wasmtime::{Config, Engine, Linker, Module, Store};
const STARTING_FUEL: u64 = u64::MAX;
const THRESHOLD_PERCENTAGE: f64 = 2.0;
/// Used to detect any significant changes in the fuel consumption when making
/// changes in Shopify Function Wasm API.
///
/// A threshold is used here so that we can decide how much of a change is
/// acceptable. The threshold value needs to be sufficiently large enough to
/// account for fuel differences between different operating systems.
///
/// We check for both increases and decreases in fuel consumption:
/// - If fuel_consumed is significantly higher than target_fuel, we fail the test and ask to consider if the changes are worth the increase
/// - If fuel_consumed is significantly lower than target_fuel, we show a message to double check the changes and update the target fuel if it's a legitimate improvement
fn assert_fuel_consumed_within_threshold(target_fuel: u64, fuel_consumed: u64) {
let target_fuel = target_fuel as f64;
let fuel_consumed = fuel_consumed as f64;
let percentage_difference = ((fuel_consumed - target_fuel) / target_fuel).abs() * 100.0;
if fuel_consumed > target_fuel {
assert!(
percentage_difference <= THRESHOLD_PERCENTAGE,
"fuel_consumed ({fuel_consumed}) was not within {THRESHOLD_PERCENTAGE:.2}% of the target_fuel value ({target_fuel}). Please consider if the changes are worth the increase in fuel consumption.",
);
} else if percentage_difference > THRESHOLD_PERCENTAGE {
panic!(
"fuel_consumed ({fuel_consumed}) was significantly better than target_fuel value ({target_fuel}) by more than {THRESHOLD_PERCENTAGE:.2}%. This is a significant improvement! Please double check your changes and update the target fuel if this is a legitimate improvement.",
);
}
}
fn run_example(example: &str, input_bytes: Vec<u8>) -> Result<(Vec<u8>, String, u64)> {
let manifest_dir = env!("CARGO_MANIFEST_DIR");
let workspace_root = std::path::PathBuf::from(manifest_dir).join("..");
let engine = Engine::new(Config::new().consume_fuel(true))?;
let module_path = workspace_root.join(format!(
"target/wasm32-unknown-unknown/release/examples/{example}.merged.wasm"
));
let module = Module::from_file(&engine, workspace_root.join(module_path))?;
let provider = Module::from_file(
&engine,
workspace_root.join("target/wasm32-unknown-unknown/release/shopify_function_provider.wasm"),
)?;
let mut linker = Linker::new(&engine);
let mut store = Store::new(&engine, ());
let provider_instance = linker.instantiate(&mut store, &provider)?;
store.set_fuel(STARTING_FUEL)?;
let init_func = provider_instance.get_typed_func::<i32, i32>(&mut store, "initialize")?;
let input_buffer_offset = init_func.call(&mut store, input_bytes.len() as _)?;
provider_instance
.get_memory(&mut store, "memory")
.unwrap()
.write(&mut store, input_buffer_offset as usize, &input_bytes)?;
linker.instance(
&mut store,
shopify_function_provider::PROVIDER_MODULE_NAME,
provider_instance,
)?;
store.set_fuel(STARTING_FUEL)?;
let instance = linker.instantiate(&mut store, &module)?;
let func = instance.get_typed_func::<(), ()>(&mut store, "_start")?;
let result = func.call(&mut store, ());
let instructions = STARTING_FUEL.saturating_sub(store.get_fuel().unwrap_or_default());
let results_offset = provider_instance
.get_typed_func::<(), u32>(&mut store, "finalize")?
.call(&mut store, ())?;
let memory = provider_instance.get_memory(&mut store, "memory").unwrap();
let mut buf = [0; 24];
memory.read(&store, results_offset as usize, &mut buf)?;
let output_offset = u32::from_le_bytes(buf[0..4].try_into().unwrap()) as usize;
let output_len = u32::from_le_bytes(buf[4..8].try_into().unwrap()) as usize;
let logs_offset1 = u32::from_le_bytes(buf[8..12].try_into().unwrap()) as usize;
let logs_len1 = u32::from_le_bytes(buf[12..16].try_into().unwrap()) as usize;
let logs_offset2 = u32::from_le_bytes(buf[16..20].try_into().unwrap()) as usize;
let logs_len2 = u32::from_le_bytes(buf[20..24].try_into().unwrap()) as usize;
let mut output = vec![0; output_len];
memory.read(&store, output_offset, &mut output)?;
let mut logs1 = vec![0; logs_len1];
memory.read(&store, logs_offset1, &mut logs1)?;
let mut logs2 = vec![0; logs_len2];
memory.read(&store, logs_offset2, &mut logs2)?;
let mut logs = Vec::with_capacity(logs_len1 + logs_len2);
logs.extend(logs1);
logs.extend(logs2);
drop(store);
let logs = String::from_utf8_lossy(&logs).to_string();
if let Err(e) = result {
return Err(anyhow::anyhow!(CallFuncError {
trap_error: e,
logs,
}));
}
Ok((output, logs, instructions))
}
fn decode_msgpack_output(output: Vec<u8>) -> Result<serde_json::Value> {
Ok(rmp_serde::from_slice(&output)?)
}
fn prepare_wasm_api_input(input: serde_json::Value) -> Result<Vec<u8>> {
Ok(rmp_serde::to_vec(&input)?)
}
fn run_wasm_api_example(example: &str, input: serde_json::Value) -> Result<serde_json::Value> {
let input_bytes = prepare_wasm_api_input(input)?;
let (output, _logs, _fuel) = run_example(example, input_bytes)?;
decode_msgpack_output(output)
}
#[derive(Debug)]
struct CallFuncError {
trap_error: Error,
logs: String,
}
impl Display for CallFuncError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}\n\nLogs: {}", self.trap_error, self.logs)
}
}
static ECHO_EXAMPLE_RESULT: LazyLock<Result<()>> = LazyLock::new(|| prepare_example("echo"));
static BENCHMARK_EXAMPLE_RESULT: LazyLock<Result<()>> =
LazyLock::new(|| prepare_example("cart-checkout-validation-wasm-api"));
static LOG_EXAMPLE_RESULT: LazyLock<Result<()>> = LazyLock::new(|| prepare_example("log"));
static PANIC_EXAMPLE_RESULT: LazyLock<Result<()>> = LazyLock::new(|| prepare_example("panic"));
static LOG_LEN_EXAMPLE_RESULT: LazyLock<Result<()>> = LazyLock::new(|| prepare_example("log-len"));
static LOG_PAST_CAPACITY_EXAMPLE_RESULT: LazyLock<Result<()>> =
LazyLock::new(|| prepare_example("log-past-capacity"));
#[test]
fn test_echo_with_bool_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(true))?,
serde_json::json!(true)
);
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(false))?,
serde_json::json!(false)
);
Ok(())
}
#[test]
fn test_echo_with_null_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(null))?,
serde_json::json!(null)
);
Ok(())
}
#[test]
fn test_echo_with_int_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
[0, 1, -1, i32::MAX, i32::MIN].iter().try_for_each(|&i| {
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(i))?,
serde_json::json!(i)
);
Ok(())
})
}
#[test]
fn test_echo_with_float_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
[0.1, 1.1, -1.1, f64::MAX, f64::MIN]
.iter()
.try_for_each(|&f| {
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(f))?,
serde_json::json!(f)
);
Ok(())
})
}
#[test]
fn test_echo_with_utf8_str_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!("Hello, world!"))?,
serde_json::json!("Hello, world!")
);
Ok(())
}
#[test]
fn test_echo_with_obj_input_with_interned_strings() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!({ "foo": 1, "bar": 2 }))?,
serde_json::json!({ "foo": 1, "bar": 2 })
);
Ok(())
}
#[test]
fn test_echo_with_obj_input_with_get_obj_prop() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!({ "abc": 1, "def": 2 }))?,
serde_json::json!({ "abc": 1, "def": 2 })
);
Ok(())
}
#[test]
fn test_echo_with_obj_input_with_get_at_index() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!({ "uvw": 1, "xyz": 2 }))?,
serde_json::json!({ "uvw": 1, "xyz": 2 })
);
Ok(())
}
#[test]
fn test_echo_with_array_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
assert_eq!(
run_wasm_api_example("echo", serde_json::json!([1, 2, 3]))?,
serde_json::json!([1, 2, 3])
);
Ok(())
}
/// Generates a cart with the specified number of items for testing.
///
/// # Arguments
/// * `size` - The number of items to generate in the cart
/// * `traverse_all` - Controls whether the cart validation should process all items or can exit early
fn generate_cart_with_size(size: usize, traverse_all: bool) -> serde_json::Value {
let mut lines = Vec::with_capacity(size);
for i in 0..size {
lines.push(serde_json::json!({
"quantity": if traverse_all { 1 } else { 2 },
"merchandise": {
"id": format!("gid://shopify/ProductVariant/{}", i + 1),
"title": format!("Sample Product {}", i + 1)
}
}));
}
serde_json::json!({
"cart": {
"lines": lines
}
})
}
#[test]
fn test_echo_with_large_string_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
let large_string = "a".repeat(u16::MAX as usize);
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(large_string))?,
serde_json::json!(large_string)
);
Ok(())
}
#[test]
#[ignore = "large array test is disabled since it takes a long time to run"]
fn test_echo_with_large_array_input() -> Result<()> {
ECHO_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
let large_array: Vec<i32> = (0..=u16::MAX as usize).map(|x| x as i32).collect();
assert_eq!(
run_wasm_api_example("echo", serde_json::json!(large_array))?,
serde_json::json!(large_array)
);
Ok(())
}
#[test]
fn test_fuel_consumption_within_threshold() -> Result<()> {
BENCHMARK_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
let input = generate_cart_with_size(2, true);
let wasm_api_input = prepare_wasm_api_input(input.clone())?;
let (_, _, wasm_api_fuel) = run_example("cart-checkout-validation-wasm-api", wasm_api_input)?;
eprintln!("WASM API fuel: {}", wasm_api_fuel);
// Using a target fuel value as reference similar to the Javy example
assert_fuel_consumed_within_threshold(9637, wasm_api_fuel);
Ok(())
}
#[test]
fn test_benchmark_with_input() -> Result<()> {
BENCHMARK_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
let input = generate_cart_with_size(2, true);
let wasm_api_input = prepare_wasm_api_input(input.clone())?;
let (_, _, wasm_api_fuel) = run_example("cart-checkout-validation-wasm-api", wasm_api_input)?;
assert_fuel_consumed_within_threshold(9_637, wasm_api_fuel);
Ok(())
}
#[test]
fn test_benchmark_with_input_early_exit() -> Result<()> {
BENCHMARK_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {}", e))?;
let input = generate_cart_with_size(100, false);
let wasm_api_input = prepare_wasm_api_input(input.clone())?;
let (_, _, wasm_api_fuel) = run_example("cart-checkout-validation-wasm-api", wasm_api_input)?;
assert_fuel_consumed_within_threshold(9_017, wasm_api_fuel);
Ok(())
}
#[test]
fn test_log() -> Result<()> {
LOG_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {e}"))?;
let (_, logs, fuel) = run_example("log", vec![])?;
assert_eq!(logs, "Hi!\nHello\nHere's a third string\n✌️\n");
assert_fuel_consumed_within_threshold(466, fuel);
Ok(())
}
#[test]
fn test_log_len() -> Result<()> {
LOG_LEN_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {e}"))?;
let run = |len| -> Result<u64> {
Ok(run_example("log-len", prepare_wasm_api_input(serde_json::json!(len))?)?.2)
};
let fuel = run(1)?;
assert_fuel_consumed_within_threshold(744, fuel);
let fuel = run(500)?;
assert_fuel_consumed_within_threshold(2_750, fuel);
let fuel = run(1_000)?;
assert_fuel_consumed_within_threshold(4_375, fuel);
let fuel = run(5_000)?;
assert_fuel_consumed_within_threshold(17_411, fuel);
let fuel = run(10_000)?;
assert_fuel_consumed_within_threshold(33_706, fuel);
let fuel = run(100_000)?;
assert_fuel_consumed_within_threshold(327_055, fuel);
Ok(())
}
#[test]
fn test_log_past_capacity() -> Result<()> {
LOG_PAST_CAPACITY_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {e}"))?;
let (_, logs, fuel) = run_example("log-past-capacity", vec![])?;
assert_eq!(logs, format!("{}{}", "a".repeat(991), "b".repeat(10)));
assert_fuel_consumed_within_threshold(965, fuel);
Ok(())
}
#[test]
fn test_panic() -> Result<()> {
PANIC_EXAMPLE_RESULT
.as_ref()
.map_err(|e| anyhow::anyhow!("Failed to prepare example: {e}"))?;
let error = run_example("panic", vec![])
.unwrap_err()
.downcast::<CallFuncError>()?;
assert_eq!(
error.logs,
"panicked at api/examples/panic.rs:6:5:
at the disco
"
);
Ok(())
}