Newer
Older
// Copyright (C) Parity Technologies (UK) Ltd.
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
// SPDX-License-Identifier: Apache-2.0
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//! Tests for Message Queue Pallet.
#![cfg(test)]
use crate::{mock::*, *};
use frame_support::{assert_noop, assert_ok, assert_storage_noop, StorageNoopGuard};
use rand::{rngs::StdRng, Rng, SeedableRng};
#[test]
fn mocked_weight_works() {
new_test_ext::<Test>().execute_with(|| {
assert!(<Test as Config>::WeightInfo::service_queue_base().is_zero());
});
new_test_ext::<Test>().execute_with(|| {
set_weight("service_queue_base", Weight::MAX);
assert_eq!(<Test as Config>::WeightInfo::service_queue_base(), Weight::MAX);
});
// The externalities reset it.
new_test_ext::<Test>().execute_with(|| {
assert!(<Test as Config>::WeightInfo::service_queue_base().is_zero());
});
}
#[test]
fn enqueue_within_one_page_works() {
new_test_ext::<Test>().execute_with(|| {
use MessageOrigin::*;
MessageQueue::enqueue_message(msg("a"), Here);
MessageQueue::enqueue_message(msg("b"), Here);
MessageQueue::enqueue_message(msg("c"), Here);
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(b"a".to_vec(), Here), (b"b".to_vec(), Here)]);
assert_eq!(MessageQueue::service_queues(2.into_weight()), 1.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(b"c".to_vec(), Here)]);
assert_eq!(MessageQueue::service_queues(2.into_weight()), 0.into_weight());
assert!(MessagesProcessed::get().is_empty());
MessageQueue::enqueue_messages([msg("a"), msg("b"), msg("c")].into_iter(), There);
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(
MessagesProcessed::take(),
vec![(b"a".to_vec(), There), (b"b".to_vec(), There),]
);
MessageQueue::enqueue_message(msg("d"), Everywhere(1));
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(MessageQueue::service_queues(2.into_weight()), 0.into_weight());
assert_eq!(
MessagesProcessed::take(),
vec![(b"c".to_vec(), There), (b"d".to_vec(), Everywhere(1))]
);
});
}
#[test]
fn queue_priority_retains() {
new_test_ext::<Test>().execute_with(|| {
use MessageOrigin::*;
assert_ring(&[]);
MessageQueue::enqueue_message(msg("a"), Everywhere(1));
assert_ring(&[Everywhere(1)]);
MessageQueue::enqueue_message(msg("b"), Everywhere(2));
assert_ring(&[Everywhere(1), Everywhere(2)]);
MessageQueue::enqueue_message(msg("c"), Everywhere(3));
assert_ring(&[Everywhere(1), Everywhere(2), Everywhere(3)]);
MessageQueue::enqueue_message(msg("d"), Everywhere(2));
assert_ring(&[Everywhere(1), Everywhere(2), Everywhere(3)]);
// service head is 1, it will process a, leaving service head at 2. it also processes b but
// doees not empty queue 2, so service head will end at 2.
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(
MessagesProcessed::take(),
vec![(vmsg("a"), Everywhere(1)), (vmsg("b"), Everywhere(2)),]
);
assert_ring(&[Everywhere(2), Everywhere(3)]);
// service head is 2, so will process d first, then c.
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(
MessagesProcessed::get(),
vec![(vmsg("d"), Everywhere(2)), (vmsg("c"), Everywhere(3)),]
);
assert_ring(&[]);
});
}
#[test]
fn queue_priority_reset_once_serviced() {
new_test_ext::<Test>().execute_with(|| {
use MessageOrigin::*;
MessageQueue::enqueue_message(msg("a"), Everywhere(1));
MessageQueue::enqueue_message(msg("b"), Everywhere(2));
MessageQueue::enqueue_message(msg("c"), Everywhere(3));
// service head is 1, it will process a, leaving service head at 2. it also processes b and
// empties queue 2, so service head will end at 3.
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
MessageQueue::enqueue_message(msg("d"), Everywhere(2));
// service head is 3, so will process c first, then d.
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(
MessagesProcessed::get(),
vec![
(vmsg("a"), Everywhere(1)),
(vmsg("b"), Everywhere(2)),
(vmsg("c"), Everywhere(3)),
(vmsg("d"), Everywhere(2)),
]
);
});
}
#[test]
fn service_queues_basic_works() {
use MessageOrigin::*;
new_test_ext::<Test>().execute_with(|| {
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)]);
// Service one message from `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)]);
// Service one message from `There`.
ServiceHead::<Test>::set(There.into());
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)]);
// Service the remaining from `Here`.
ServiceHead::<Test>::set(Here.into());
assert_eq!(MessageQueue::service_queues(2.into_weight()), 2.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("ab"), Here), (vmsg("abc"), Here)]);
assert_eq!(QueueChanges::take(), vec![(Here, 0, 0)]);
// Service all remaining messages.
assert_eq!(MessageQueue::service_queues(Weight::MAX), 2.into_weight());
assert_eq!(MessagesProcessed::take(), vec![(vmsg("xy"), There), (vmsg("xyz"), There)]);
assert_eq!(QueueChanges::take(), vec![(There, 0, 0)]);
});
}
#[test]
fn service_queues_failing_messages_works() {
use MessageOrigin::*;
new_test_ext::<Test>().execute_with(|| {
set_weight("service_page_item", 1.into_weight());
MessageQueue::enqueue_message(msg("badformat"), Here);
MessageQueue::enqueue_message(msg("corrupt"), Here);
MessageQueue::enqueue_message(msg("unsupported"), Here);
MessageQueue::enqueue_message(msg("yield"), Here);
// Starts with four pages.
assert_pages(&[0, 1, 2, 3]);
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_last_event::<Test>(
Event::ProcessingFailed {
hash: <Test as frame_system::Config>::Hashing::hash(b"badformat"),
origin: MessageOrigin::Here,
error: ProcessMessageError::BadFormat,
}
.into(),
);
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_last_event::<Test>(
Event::ProcessingFailed {
hash: <Test as frame_system::Config>::Hashing::hash(b"corrupt"),
origin: MessageOrigin::Here,
error: ProcessMessageError::Corrupt,
}
.into(),
);
assert_eq!(MessageQueue::service_queues(1.into_weight()), 1.into_weight());
assert_last_event::<Test>(
Event::ProcessingFailed {
hash: <Test as frame_system::Config>::Hashing::hash(b"unsupported"),
origin: MessageOrigin::Here,
Loading full blame...