-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgtest.rs
524 lines (410 loc) · 14.3 KB
/
gtest.rs
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
use demo_client::{
counter::events::CounterEvents, demo_factory, dog::events::DogEvents, ping_pong, traits::*,
};
use fixture::{Fixture, ADMIN_ID, DEMO_WASM_PATH};
use futures::stream::StreamExt;
use gstd::errors::{ErrorReplyReason, SimpleExecutionError};
use sails_rs::{
calls::*,
errors::RtlError,
events::*,
gtest::{
calls::{BlockRunMode, GTestRemoting},
Program, System,
},
};
mod fixture;
#[tokio::test]
async fn counter_add_works() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send_recv` method
let demo_program_id = demo_factory
.new(Some(42), None)
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut counter_client = fixture.counter_client();
// Listen to Counter events
let mut counter_listener = fixture.counter_listener();
let mut counter_events = counter_listener.listen().await.unwrap();
// Act
// Use generated client code for calling Counter service
// using the `send_recv` method
let result = counter_client
.add(10)
.send_recv(demo_program_id)
.await
.unwrap();
// Asert
let event = counter_events.next().await.unwrap();
assert_eq!(result, 52);
assert_eq!((demo_program_id, CounterEvents::Added(10)), event);
}
#[tokio::test]
async fn counter_sub_works() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send`/`recv` pair
// of methods
let activation = demo_factory
.new(Some(42), None)
.send(fixture.demo_code_id(), "123")
.await
.unwrap();
let demo_program_id = activation.recv().await.unwrap();
let mut counter_client = fixture.counter_client();
// Listen to Counter events
let mut counter_listener = fixture.counter_listener();
let mut counter_events = counter_listener.listen().await.unwrap();
// Act
// Use generated client code for calling Counter service
// using the `send`/`recv` pair of methods
let response = counter_client.sub(10).send(demo_program_id).await.unwrap();
let result = response.recv().await.unwrap();
// Assert
let event = counter_events.next().await.unwrap();
assert_eq!(result, 32);
assert_eq!((demo_program_id, CounterEvents::Subtracted(10)), event);
}
#[tokio::test]
async fn counter_query_works() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send_recv` method
let demo_program_id = demo_factory
.new(Some(42), None)
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let counter_client = fixture.counter_client();
// Act
// Use generated client code for query Counter service using the `recv` method
let result = counter_client.value().recv(demo_program_id).await.unwrap();
// Asert
assert_eq!(result, 42);
}
#[tokio::test]
async fn counter_query_not_enough_gas() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send_recv` method
let demo_program_id = demo_factory
.new(Some(42), None)
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let counter_client = fixture.counter_client();
// Act
// Use generated client code for query Counter service using the `recv` method
let result = counter_client
.value()
.with_gas_limit(0) // Set gas_limit to 0
.recv(demo_program_id)
.await;
// Asert
assert!(matches!(
result,
Err(sails_rs::errors::Error::Rtl(RtlError::ReplyHasError(
ErrorReplyReason::Execution(SimpleExecutionError::RanOutOfGas),
message
))) if message == "Not enough gas to handle program data"
));
}
/// Low level program test using `gtest::System` and call encoding/decoding with `io` module
#[tokio::test]
async fn ping_pong_low_level_works() {
let system = System::new();
system.init_logger_with_default_filter("gwasm=debug,gtest=info,sails_rs=debug");
system.mint_to(ADMIN_ID, 100_000_000_000_000);
let demo_program = Program::from_file(&system, DEMO_WASM_PATH);
// Use generated `io` module to create a program
demo_program.send_bytes(ADMIN_ID, demo_factory::io::Default::encode_call());
// Use generated `io` module for encoding/decoding calls and replies
// and send/receive bytes using `gtest` native means
let ping_call_payload = ping_pong::io::Ping::encode_call("ping".into());
let message_id = demo_program.send_bytes(ADMIN_ID, ping_call_payload);
let run_result = system.run_next_block();
let reply_log_record = run_result
.log()
.iter()
.find(|entry| entry.reply_to() == Some(message_id))
.unwrap();
let ping_reply_payload = reply_log_record.payload();
let ping_reply = ping_pong::io::Ping::decode_reply(ping_reply_payload).unwrap();
assert_eq!(ping_reply, Ok("pong".to_string()));
}
#[tokio::test]
async fn dog_barks() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut dog_client = fixture.dog_client();
let mut dog_listener = fixture.dog_listener();
let mut dog_events = dog_listener.listen().await.unwrap();
// Act
let result = dog_client
.make_sound()
.send_recv(demo_program_id)
.await
.unwrap();
// Assert
let event = dog_events.next().await.unwrap();
assert_eq!(result, "Woof! Woof!");
assert_eq!((demo_program_id, DogEvents::Barked), event);
}
#[tokio::test]
async fn dog_walks() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut dog_client = fixture.dog_client();
let mut dog_listener = fixture.dog_listener();
let mut dog_events = dog_listener.listen().await.unwrap();
// Act
dog_client
.walk(10, 20)
.send_recv(demo_program_id)
.await
.unwrap();
// Assert
let position = dog_client.position().recv(demo_program_id).await.unwrap();
let event = dog_events.next().await.unwrap();
assert_eq!(position, (11, 19));
assert_eq!(
(
demo_program_id,
DogEvents::Walked {
from: (1, -1),
to: (11, 19)
}
),
event
);
}
#[tokio::test]
async fn dog_weights() {
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let dog_client = fixture.dog_client();
let avg_weight = dog_client.avg_weight().recv(demo_program_id).await.unwrap();
assert_eq!(avg_weight, 42);
}
#[tokio::test]
async fn references_add() {
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut client = fixture.references_client();
let value = client.add(42).send_recv(demo_program_id).await.unwrap();
assert_eq!(42, value);
}
#[tokio::test]
async fn references_bytes() {
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut client = fixture.references_client();
_ = client
.add_byte(42)
.send_recv(demo_program_id)
.await
.unwrap();
_ = client
.add_byte(89)
.send_recv(demo_program_id)
.await
.unwrap();
_ = client
.add_byte(14)
.send_recv(demo_program_id)
.await
.unwrap();
let last = client.last_byte().recv(demo_program_id).await.unwrap();
assert_eq!(Some(14), last);
}
#[tokio::test]
async fn references_guess_num() {
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
let demo_program_id = demo_factory
.new(None, Some((1, -1)))
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let mut client = fixture.references_client();
let res1 = client
.guess_num(42)
.send_recv(demo_program_id)
.await
.unwrap();
let res2 = client
.guess_num(89)
.send_recv(demo_program_id)
.await
.unwrap();
let res3 = client.message().recv(demo_program_id).await.unwrap();
let res4 = client.set_num(14).send_recv(demo_program_id).await.unwrap();
let res5 = client
.guess_num(14)
.send_recv(demo_program_id)
.await
.unwrap();
assert_eq!(Ok("demo".to_owned()), res1);
assert_eq!(Err("Number is too large".to_owned()), res2);
assert_eq!(Some("demo".to_owned()), res3);
assert_eq!(Ok(()), res4);
assert_eq!(Ok("demo".to_owned()), res5);
}
#[tokio::test]
async fn counter_add_works_via_next_mode() {
// Arrange
const DEMO_WASM_PATH: &str = "../../../target/wasm32-unknown-unknown/debug/demo.opt.wasm";
let system = System::new();
system.init_logger_with_default_filter("gwasm=debug,gtest=info,sails_rs=debug");
system.mint_to(fixture::ADMIN_ID, 100_000_000_000_000);
let demo_code_id = system.submit_code_file(DEMO_WASM_PATH);
let remoting = GTestRemoting::new(system, fixture::ADMIN_ID.into())
.with_block_run_mode(BlockRunMode::Next);
let demo_factory = demo_client::DemoFactory::new(remoting.clone());
let demo_program_id = demo_factory
.new(Some(42), None)
.send_recv(demo_code_id, "123")
.await
.unwrap();
let mut counter_client = demo_client::Counter::new(remoting.clone());
// Listen to Counter events
let mut counter_listener = demo_client::counter::events::listener(remoting.clone());
let mut counter_events = counter_listener.listen().await.unwrap();
// Act
let result = counter_client
.add(10)
.send_recv(demo_program_id)
.await
.unwrap();
// Asert
let event = counter_events.next().await.unwrap();
assert_eq!(result, 52);
assert_eq!((demo_program_id, CounterEvents::Added(10)), event);
}
#[tokio::test]
async fn counter_add_works_via_manual_mode() {
// Arrange
const DEMO_WASM_PATH: &str = "../../../target/wasm32-unknown-unknown/debug/demo.opt.wasm";
let system = System::new();
system.init_logger_with_default_filter("gwasm=debug,gtest=info,sails_rs=debug");
system.mint_to(fixture::ADMIN_ID, 100_000_000_000_000);
let demo_code_id = system.submit_code_file(DEMO_WASM_PATH);
let remoting = GTestRemoting::new(system, fixture::ADMIN_ID.into())
.with_block_run_mode(BlockRunMode::Manual);
let demo_factory = demo_client::DemoFactory::new(remoting.clone());
// Use generated client code for activating Demo program
let activation = demo_factory
.new(Some(42), None)
.send(demo_code_id, "123")
.await
.unwrap();
// Run next Block
remoting.run_next_block();
let demo_program_id = activation.recv().await.unwrap();
let mut counter_client_add = demo_client::Counter::new(remoting.clone());
let mut counter_client_sub = demo_client::Counter::new(remoting.clone());
// Listen to Counter events
let mut counter_listener = demo_client::counter::events::listener(remoting.clone());
let mut counter_events = counter_listener.listen().await.unwrap();
// Use generated client code for calling Counter service
let call_add = counter_client_add
.add(10)
.send(demo_program_id)
.await
.unwrap();
let call_sub = counter_client_sub
.sub(20)
.send(demo_program_id)
.await
.unwrap();
// Run next Block
remoting.run_next_block();
// Got replies
let result_add = call_add.recv().await.unwrap();
assert_eq!(result_add, 52);
let result_sub = call_sub.recv().await.unwrap();
assert_eq!(result_sub, 32);
// Got events
assert_eq!(
(demo_program_id, CounterEvents::Added(10)),
counter_events.next().await.unwrap()
);
assert_eq!(
(demo_program_id, CounterEvents::Subtracted(20)),
counter_events.next().await.unwrap()
);
}
#[tokio::test]
async fn value_fee_works() {
// Arrange
let fixture = Fixture::new();
let demo_factory = fixture.demo_factory();
// Use generated client code for activating Demo program
// using the `new` constructor and the `send_recv` method
let program_id = demo_factory
.new(Some(42), None)
.send_recv(fixture.demo_code_id(), "123")
.await
.unwrap();
let initial_balance = fixture.balance_of(fixture::ADMIN_ID.into());
let mut client = fixture.value_fee_client();
// Act
// Use generated client code to call `do_something_and_take_fee` method with zero value
let result = client
.do_something_and_take_fee()
.send_recv(program_id)
.await
.unwrap();
assert!(!result);
// Use generated client code to call `do_something_and_take_fee` method with value
let result = client
.do_something_and_take_fee()
.with_value(15_000_000_000_000)
.send_recv(program_id)
.await
.unwrap();
assert!(result);
let balance = fixture.balance_of(fixture::ADMIN_ID.into());
// fee is 10_000_000_000_000 + spent gas
assert!(
initial_balance - balance > 10_000_000_000_000
&& initial_balance - balance < 10_100_000_000_000
);
}