Skip to content

Commit 16d8cba

Browse files
authored
test(hidpp): wait for the message listener before asserting on it (#2503)
test(hidpp): wait for the message listener before asserting on it raw_listener_gets_every_report_intact_including_foreign_vendor_pdus waited only for the raw listener count, then asserted that exactly one message had reached the message listener. read_loop hands each report to the raw listeners before it parses it as HID++ and dispatches it to the message listeners, so the raw count can reach 2 while the short message is still on its way to the message path. That window failed the test once on tests (macos, x86_64). Wait for the message listener too, and record what it receives: the assertion now compares the delivered messages with [short]. A call count alone would let a vendor PDU that wrongly reached the message path satisfy the wait in place of the short message. The timeout panic in wait_for_atomic_count now also reports the count it saw.
1 parent 5a4c174 commit 16d8cba

1 file changed

Lines changed: 14 additions & 4 deletions

File tree

‎crates/openlogi-hidpp/src/channel/tests/raw_and_listeners.rs‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ async fn wait_for_atomic_count(count: &AtomicUsize, expected: usize) {
128128
futures_timer::Delay::new(Duration::from_millis(10)).await;
129129
}
130130

131-
panic!("timed out waiting for atomic count {expected}");
131+
panic!(
132+
"timed out waiting for atomic count {expected}, saw {}",
133+
count.load(Ordering::SeqCst)
134+
);
132135
}
133136

134137
/// A grid-key vendor PDU of the MX Creative Keypad: report ID `0x13`, the
@@ -161,10 +164,13 @@ fn raw_listener_gets_every_report_intact_including_foreign_vendor_pdus() {
161164
calls.fetch_add(1, Ordering::SeqCst);
162165
}
163166
});
167+
let delivered = Arc::new(Mutex::new(Vec::<HidppMessage>::new()));
164168
let msg_calls = Arc::new(AtomicUsize::new(0));
165169
channel.add_msg_listener({
170+
let delivered = Arc::clone(&delivered);
166171
let msg_calls = Arc::clone(&msg_calls);
167-
move |_, _| {
172+
move |msg, _| {
173+
delivered.lock().unwrap().push(msg);
168174
msg_calls.fetch_add(1, Ordering::SeqCst);
169175
}
170176
});
@@ -173,14 +179,18 @@ fn raw_listener_gets_every_report_intact_including_foreign_vendor_pdus() {
173179
wait_for_atomic_count(&calls, 1).await;
174180
handle.send_incoming(short).await;
175181
wait_for_atomic_count(&calls, 2).await;
182+
// The reader hands each report to the raw listeners before it parses
183+
// it for the message listeners, so the raw count can lead.
184+
wait_for_atomic_count(&msg_calls, 1).await;
176185

177186
assert_eq!(
178187
seen.lock().unwrap().as_slice(),
179188
&[grid_key_pdu(0x09, 0x02), raw_report_bytes(short)]
180189
);
181190
// Only the message parses as HID++: a vendor PDU is never delivered
182-
// to the message path.
183-
assert_eq!(msg_calls.load(Ordering::SeqCst), 1);
191+
// to the message path. Reads are FIFO on the one read thread, so a PDU
192+
// that leaked onto the message path would be first in `delivered`.
193+
assert_eq!(delivered.lock().unwrap().as_slice(), &[short]);
184194
});
185195
}
186196

0 commit comments

Comments
 (0)