|
1 | 1 | use crate::{
|
2 | 2 | mock::*,
|
3 | 3 | types::{PaymentDetail, PaymentState},
|
| 4 | + weights::WeightInfo, |
4 | 5 | Payment as PaymentStore, PaymentHandler, ScheduledTask, ScheduledTasks, Task,
|
5 | 6 | };
|
6 |
| -use frame_support::{assert_noop, assert_ok, storage::with_transaction}; |
| 7 | +use frame_support::{assert_noop, assert_ok, storage::with_transaction, traits::OnIdle, weights::Weight}; |
7 | 8 | use orml_traits::MultiCurrency;
|
8 | 9 | use sp_runtime::{Percent, TransactionOutcome};
|
9 | 10 |
|
@@ -1398,3 +1399,52 @@ fn test_automatic_refund_works_for_multiple_payments() {
|
1398 | 1399 | assert_eq!(Tokens::free_balance(CURRENCY_ID, &PAYMENT_RECIPENT_TWO), 0);
|
1399 | 1400 | });
|
1400 | 1401 | }
|
| 1402 | + |
| 1403 | +#[test] |
| 1404 | +fn on_idle_works() { |
| 1405 | + new_test_ext().execute_with(|| { |
| 1406 | + assert_eq!( |
| 1407 | + Payment::on_idle(System::block_number(), Weight::MAX), |
| 1408 | + <()>::remove_task() |
| 1409 | + ); |
| 1410 | + |
| 1411 | + let payment_amount = 20; |
| 1412 | + let expected_cancel_block = CANCEL_BLOCK_BUFFER + 1; |
| 1413 | + |
| 1414 | + assert_ok!(Payment::pay( |
| 1415 | + RuntimeOrigin::signed(PAYMENT_CREATOR), |
| 1416 | + PAYMENT_RECIPENT, |
| 1417 | + CURRENCY_ID, |
| 1418 | + payment_amount, |
| 1419 | + None |
| 1420 | + )); |
| 1421 | + |
| 1422 | + // creator requests a refund |
| 1423 | + assert_ok!(Payment::request_refund( |
| 1424 | + RuntimeOrigin::signed(PAYMENT_CREATOR), |
| 1425 | + PAYMENT_RECIPENT |
| 1426 | + )); |
| 1427 | + // ensure the request is added to the refund queue |
| 1428 | + let scheduled_tasks_list = ScheduledTasks::<Test>::get(); |
| 1429 | + assert_eq!(scheduled_tasks_list.len(), 1); |
| 1430 | + assert_eq!( |
| 1431 | + scheduled_tasks_list.get(&(PAYMENT_CREATOR, PAYMENT_RECIPENT)).unwrap(), |
| 1432 | + &ScheduledTask { |
| 1433 | + task: Task::Cancel, |
| 1434 | + when: expected_cancel_block |
| 1435 | + } |
| 1436 | + ); |
| 1437 | + |
| 1438 | + assert_eq!(run_n_blocks(CANCEL_BLOCK_BUFFER - 1), 600); |
| 1439 | + assert_eq!( |
| 1440 | + Payment::on_idle(System::block_number(), Weight::MAX), |
| 1441 | + <()>::remove_task() |
| 1442 | + ); |
| 1443 | + |
| 1444 | + assert_eq!(run_n_blocks(1), 601); |
| 1445 | + assert_eq!( |
| 1446 | + Payment::on_idle(System::block_number(), Weight::MAX), |
| 1447 | + <()>::remove_task() + <()>::cancel() |
| 1448 | + ); |
| 1449 | + }); |
| 1450 | +} |
0 commit comments