Newer
Older
assert!(QueueChanges::take().is_empty());
})
}
/// The footprint of an invalid queue is the default footprint.
#[test]
fn footprint_invalid_works() {
test_closure(|| {
let origin = MessageOrigin::Here;
assert_eq!(MessageQueue::footprint(origin), Default::default());
})
}
/// The footprint of a swept queue is still correct.
#[test]
fn footprint_on_swept_works() {
use MessageOrigin::*;
test_closure(|| {
let mut book = empty_book::<Test>();
book.message_count = 3;
book.size = 10;
BookStateFor::<Test>::insert(Here, &book);
knit(&Here);
MessageQueue::sweep_queue(Here);
let fp = MessageQueue::footprint(Here);
assert_eq!(fp.count, 3);
assert_eq!(fp.size, 10);
})
}
#[test]
fn execute_overweight_works() {
test_closure(|| {
set_weight("bump_service_head", 1.into_weight());
set_weight("service_queue_base", 1.into_weight());
set_weight("service_page_base_completion", 1.into_weight());
// Enqueue a message
let origin = MessageOrigin::Here;
MessageQueue::enqueue_message(msg("weight=6"), origin);
// Load the current book
let book = BookStateFor::<Test>::get(origin);
assert_eq!(book.message_count, 1);
assert!(Pages::<Test>::contains_key(origin, 0));
// Mark the message as permanently overweight.
assert_eq!(MessageQueue::service_queues(4.into_weight()), 4.into_weight());
assert_eq!(QueueChanges::take(), vec![(origin, 1, 8)]);
assert_last_event::<Test>(
Event::OverweightEnqueued {
id: blake2_256(b"weight=6"),
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
origin: MessageOrigin::Here,
message_index: 0,
page_index: 0,
}
.into(),
);
// Now try to execute it with too few weight.
let consumed =
<MessageQueue as ServiceQueues>::execute_overweight(5.into_weight(), (origin, 0, 0));
assert_eq!(consumed, Err(ExecuteOverweightError::InsufficientWeight));
// Execute it with enough weight.
assert_eq!(Pages::<Test>::iter().count(), 1);
assert!(QueueChanges::take().is_empty());
let consumed =
<MessageQueue as ServiceQueues>::execute_overweight(7.into_weight(), (origin, 0, 0))
.unwrap();
assert_eq!(consumed, 6.into_weight());
assert_eq!(QueueChanges::take(), vec![(origin, 0, 0)]);
// There is no message left in the book.
let book = BookStateFor::<Test>::get(origin);
assert_eq!(book.message_count, 0);
// And no more pages.
assert_eq!(Pages::<Test>::iter().count(), 0);
// Doing it again with enough weight will error.
let consumed =
<MessageQueue as ServiceQueues>::execute_overweight(70.into_weight(), (origin, 0, 0));
assert_eq!(consumed, Err(ExecuteOverweightError::NotFound));
assert!(QueueChanges::take().is_empty());
assert!(!Pages::<Test>::contains_key(origin, 0), "Page is gone");
// The book should have been unknit from the ready ring.
assert!(!ServiceHead::<Test>::exists(), "No ready book");
});
}
#[test]
fn permanently_overweight_book_unknits() {
use MessageOrigin::*;
test_closure(|| {
set_weight("bump_service_head", 1.into_weight());
set_weight("service_queue_base", 1.into_weight());
set_weight("service_page_base_completion", 1.into_weight());
MessageQueue::enqueue_messages([msg("weight=9")].into_iter(), Here);
// It is the only ready book.
assert_ring(&[Here]);
// Mark the message as overweight.
assert_eq!(MessageQueue::service_queues(8.into_weight()), 4.into_weight());
assert_last_event::<Test>(
Event::OverweightEnqueued {
id: blake2_256(b"weight=9"),
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
origin: Here,
message_index: 0,
page_index: 0,
}
.into(),
);
// The book is not ready anymore.
assert_ring(&[]);
assert_eq!(MessagesProcessed::take().len(), 0);
assert_eq!(BookStateFor::<Test>::get(Here).message_count, 1);
// Now if we enqueue another message, it will become ready again.
MessageQueue::enqueue_messages([msg("weight=1")].into_iter(), Here);
assert_ring(&[Here]);
assert_eq!(MessageQueue::service_queues(8.into_weight()), 5.into_weight());
assert_eq!(MessagesProcessed::take().len(), 1);
assert_ring(&[]);
});
}
#[test]
fn permanently_overweight_book_unknits_multiple() {
use MessageOrigin::*;
test_closure(|| {
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
set_weight("bump_service_head", 1.into_weight());
set_weight("service_queue_base", 1.into_weight());
set_weight("service_page_base_completion", 1.into_weight());
MessageQueue::enqueue_messages(
[msg("weight=1"), msg("weight=9"), msg("weight=9")].into_iter(),
Here,
);
assert_ring(&[Here]);
// Process the first message.
assert_eq!(MessageQueue::service_queues(4.into_weight()), 4.into_weight());
assert_eq!(num_overweight_enqueued_events(), 0);
assert_eq!(MessagesProcessed::take().len(), 1);
// Book is still ready since it was not marked as overweight yet.
assert_ring(&[Here]);
assert_eq!(MessageQueue::service_queues(8.into_weight()), 5.into_weight());
assert_eq!(num_overweight_enqueued_events(), 2);
assert_eq!(MessagesProcessed::take().len(), 0);
// Now it is overweight.
assert_ring(&[]);
// Enqueue another message.
MessageQueue::enqueue_messages([msg("weight=1")].into_iter(), Here);
assert_ring(&[Here]);
assert_eq!(MessageQueue::service_queues(4.into_weight()), 4.into_weight());
assert_eq!(MessagesProcessed::take().len(), 1);
assert_ring(&[]);
});
}
/// We don't want empty books in the ready ring, but if they somehow make their way in there, it
/// should not panic.
#[test]
#[cfg(not(debug_assertions))] // Would trigger a defensive failure otherwise.
fn ready_but_empty_does_not_panic() {
use MessageOrigin::*;
test_closure(|| {
BookStateFor::<Test>::insert(Here, empty_book::<Test>());
BookStateFor::<Test>::insert(There, empty_book::<Test>());
knit(&Here);
knit(&There);
assert_ring(&[Here, There]);
assert_eq!(MessageQueue::service_queues(Weight::MAX), 0.into_weight());
assert_ring(&[]);
});
}
/// We don't want permanently books in the ready ring, but if they somehow make their way in there,
/// it should not panic.
#[test]
#[cfg(not(debug_assertions))] // Would trigger a defensive failure otherwise.
fn ready_but_perm_overweight_does_not_panic() {
use MessageOrigin::*;
test_closure(|| {
MessageQueue::enqueue_message(msg("weight=9"), Here);
assert_eq!(MessageQueue::service_queues(8.into_weight()), 0.into_weight());
assert_ring(&[]);
// Force it back into the ready ring.
knit(&Here);
assert_ring(&[Here]);
assert_eq!(MessageQueue::service_queues(Weight::MAX), 0.into_weight());
// Unready again.
assert_ring(&[]);
});
}
/// Checks that (un)knitting the ready ring works with just one queue.
///
/// This case is interesting since it wraps and a lot of `mutate` now operate on the same object.
#[test]
fn ready_ring_knit_basic_works() {
use MessageOrigin::*;
test_closure(|| {
BookStateFor::<Test>::insert(Here, empty_book::<Test>());
for i in 0..10 {
if i % 2 == 0 {
knit(&Here);
assert_ring(&[Here]);
} else {
unknit(&Here);
assert_ring(&[]);
}
}
assert_ring(&[]);
});
}
#[test]
fn ready_ring_knit_and_unknit_works() {
use MessageOrigin::*;
test_closure(|| {
// Place three queues into the storage.
BookStateFor::<Test>::insert(Here, empty_book::<Test>());
BookStateFor::<Test>::insert(There, empty_book::<Test>());
BookStateFor::<Test>::insert(Everywhere(0), empty_book::<Test>());
// Pausing should make no difference:
PausedQueues::set(vec![Here, There, Everywhere(0)]);
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
// Knit them into the ready ring.
assert_ring(&[]);
knit(&Here);
assert_ring(&[Here]);
knit(&There);
assert_ring(&[Here, There]);
knit(&Everywhere(0));
assert_ring(&[Here, There, Everywhere(0)]);
// Now unknit…
unknit(&Here);
assert_ring(&[There, Everywhere(0)]);
unknit(&There);
assert_ring(&[Everywhere(0)]);
unknit(&Everywhere(0));
assert_ring(&[]);
});
}
#[test]
fn enqueue_message_works() {
use MessageOrigin::*;
let max_msg_per_page = <Test as Config>::HeapSize::get() as u64 /
(ItemHeader::<<Test as Config>::Size>::max_encoded_len() as u64 + 1);
test_closure(|| {
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
// Enqueue messages which should fill three pages.
let n = max_msg_per_page * 3;
for i in 1..=n {
MessageQueue::enqueue_message(msg("a"), Here);
assert_eq!(QueueChanges::take(), vec![(Here, i, i)], "OnQueueChanged not called");
}
assert_eq!(Pages::<Test>::iter().count(), 3);
// Enqueue one more onto page 4.
MessageQueue::enqueue_message(msg("abc"), Here);
assert_eq!(QueueChanges::take(), vec![(Here, n + 1, n + 3)]);
assert_eq!(Pages::<Test>::iter().count(), 4);
// Check the state.
assert_eq!(BookStateFor::<Test>::iter().count(), 1);
let book = BookStateFor::<Test>::get(Here);
assert_eq!(book.message_count, n + 1);
assert_eq!(book.size, n + 3);
assert_eq!((book.begin, book.end), (0, 4));
assert_eq!(book.count as usize, Pages::<Test>::iter().count());
});
}
#[test]
fn enqueue_messages_works() {
use MessageOrigin::*;
let max_msg_per_page = <Test as Config>::HeapSize::get() as u64 /
(ItemHeader::<<Test as Config>::Size>::max_encoded_len() as u64 + 1);
test_closure(|| {
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
// Enqueue messages which should fill three pages.
let n = max_msg_per_page * 3;
let msgs = vec![msg("a"); n as usize];
// Now queue all messages at once.
MessageQueue::enqueue_messages(msgs.into_iter(), Here);
// The changed handler should only be called once.
assert_eq!(QueueChanges::take(), vec![(Here, n, n)], "OnQueueChanged not called");
assert_eq!(Pages::<Test>::iter().count(), 3);
// Enqueue one more onto page 4.
MessageQueue::enqueue_message(msg("abc"), Here);
assert_eq!(QueueChanges::take(), vec![(Here, n + 1, n + 3)]);
assert_eq!(Pages::<Test>::iter().count(), 4);
// Check the state.
assert_eq!(BookStateFor::<Test>::iter().count(), 1);
let book = BookStateFor::<Test>::get(Here);
assert_eq!(book.message_count, n + 1);
assert_eq!(book.size, n + 3);
assert_eq!((book.begin, book.end), (0, 4));
assert_eq!(book.count as usize, Pages::<Test>::iter().count());
});
}
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
#[test]
fn service_queues_suspend_works() {
use MessageOrigin::*;
test_closure(|| {
MessageQueue::enqueue_messages(vec![msg("a"), msg("ab"), msg("abc")].into_iter(), Here);
MessageQueue::enqueue_messages(vec![msg("x"), msg("xy"), msg("xyz")].into_iter(), There);
assert_eq!(QueueChanges::take(), vec![(Here, 3, 6), (There, 3, 6)]);
// Pause `Here` - execution starts `There`.
PausedQueues::set(vec![Here]);
assert_eq!(
(true, false),
(
<Test as Config>::QueuePausedQuery::is_paused(&Here),
<Test as Config>::QueuePausedQuery::is_paused(&There)
)
);
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("x"), There)]);
assert_eq!(QueueChanges::take(), vec![(There, 2, 5)]);
// Unpause `Here` - execution continues `There`.
PausedQueues::take();
assert_eq!(
(false, false),
(
<Test as Config>::QueuePausedQuery::is_paused(&Here),
<Test as Config>::QueuePausedQuery::is_paused(&There)
)
);
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("xy"), There)]);
assert_eq!(QueueChanges::take(), vec![(There, 1, 3)]);
// Now it swaps to `Here`.
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("a"), Here)]);
assert_eq!(QueueChanges::take(), vec![(Here, 2, 5)]);
// Pause `There` - execution continues `Here`.
PausedQueues::set(vec![There]);
assert_eq!(
(false, true),
(
<Test as Config>::QueuePausedQuery::is_paused(&Here),
<Test as Config>::QueuePausedQuery::is_paused(&There)
)
);
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("ab"), Here)]);
assert_eq!(QueueChanges::take(), vec![(Here, 1, 3)]);
// Unpause `There` and service all remaining messages.
PausedQueues::take();
assert_eq!(
(false, false),
(
<Test as Config>::QueuePausedQuery::is_paused(&Here),
<Test as Config>::QueuePausedQuery::is_paused(&There)
)
);
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("abc"), Here), (vmsg("xyz"), There)]);
assert_eq!(QueueChanges::take(), vec![(Here, 0, 0), (There, 0, 0)]);
});
}
/// Tests that manual overweight execution on a suspended queue errors with `QueueSuspended`.
#[test]
fn execute_overweight_respects_suspension() {
test_closure(|| {
let origin = MessageOrigin::Here;
MessageQueue::enqueue_message(msg("weight=5"), origin);
// Mark the message as permanently overweight.
MessageQueue::service_queues(4.into_weight());
assert_last_event::<Test>(
Event::OverweightEnqueued {
id: blake2_256(b"weight=5"),
origin,
message_index: 0,
page_index: 0,
}
.into(),
);
PausedQueues::set(vec![origin]);
assert!(<Test as Config>::QueuePausedQuery::is_paused(&origin));
// Execution should fail.
assert_eq!(
<MessageQueue as ServiceQueues>::execute_overweight(Weight::MAX, (origin, 0, 0)),
Err(ExecuteOverweightError::QueuePaused)
);
PausedQueues::take();
assert!(!<Test as Config>::QueuePausedQuery::is_paused(&origin));
// Execution should work again with same args.
assert_ok!(<MessageQueue as ServiceQueues>::execute_overweight(
Weight::MAX,
(origin, 0, 0)
));
assert_last_event::<Test>(
Event::Processed {
id: blake2_256(b"weight=5"),
origin,
weight_used: 5.into_weight(),
success: true,
}
.into(),
);
});
}
#[test]
fn service_queue_suspension_ready_ring_works() {
test_closure(|| {
let origin = MessageOrigin::Here;
PausedQueues::set(vec![origin]);
MessageQueue::enqueue_message(msg("weight=5"), origin);
MessageQueue::service_queues(Weight::MAX);
// It did not execute but is in the ready ring.
assert!(System::events().is_empty(), "Paused");
assert_ring(&[origin]);
// Now when we un-pause, it will execute.
PausedQueues::take();
MessageQueue::service_queues(Weight::MAX);
assert_last_event::<Test>(
Event::Processed {
id: blake2_256(b"weight=5"),
origin,
weight_used: 5.into_weight(),
success: true,
}
.into(),
);
});
}