Skip to content

Commit 639e289

Browse files
committed
tests: switch to QueueT
Make all accesses to the underlying QueueState go through the with() function. The function which will take care of locking when testing QueueSync. Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 670de1b commit 639e289

File tree

1 file changed

+93
-83
lines changed

1 file changed

+93
-83
lines changed

crates/virtio-queue/src/lib.rs

Lines changed: 93 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1031,6 +1031,7 @@ impl<M: GuestAddressSpace> Queue<M> {
10311031
#[cfg(test)]
10321032
mod tests {
10331033
use super::*;
1034+
use generic::QueueT;
10341035
use memoffset::offset_of;
10351036
use mock::{DescriptorTable, MockSplitQueue};
10361037

@@ -1193,72 +1194,74 @@ mod tests {
11931194
assert!(q.is_valid());
11941195

11951196
// shouldn't be valid when not marked as ready
1196-
q.state.ready = false;
1197+
q.with(|mut qstate| qstate.ready = false);
11971198
assert!(!q.is_valid());
1198-
q.state.ready = true;
1199+
q.with(|mut qstate| qstate.ready = true);
1200+
1201+
let max_size = q.with(|qstate| qstate.max_size);
11991202

12001203
// or when size > max_size
1201-
q.state.size = q.state.max_size << 1;
1204+
q.with(|mut qstate| qstate.size = max_size << 1);
12021205
assert!(!q.is_valid());
1203-
q.state.size = q.state.max_size;
1206+
q.with(|mut qstate| qstate.size = max_size);
12041207

12051208
// or when size is 0
1206-
q.state.size = 0;
1209+
q.with(|mut qstate| qstate.size = 0);
12071210
assert!(!q.is_valid());
1208-
q.state.size = q.state.max_size;
1211+
q.with(|mut qstate| qstate.size = max_size);
12091212

12101213
// or when size is not a power of 2
1211-
q.state.size = 11;
1214+
q.with(|mut qstate| qstate.size = 11);
12121215
assert!(!q.is_valid());
1213-
q.state.size = q.state.max_size;
1216+
q.with(|mut qstate| qstate.size = max_size);
12141217

12151218
// or if the various addresses are off
12161219

1217-
q.state.desc_table = GuestAddress(0xffff_ffff);
1220+
q.with(|mut qstate| qstate.desc_table = GuestAddress(0xffff_ffff));
12181221
assert!(!q.is_valid());
1219-
q.state.desc_table = GuestAddress(0x1001);
1222+
q.with(|mut qstate| qstate.desc_table = GuestAddress(0x1001));
12201223
assert!(!q.is_valid());
1221-
q.state.desc_table = vq.desc_table_addr();
1224+
q.with(|mut qstate| qstate.desc_table = vq.desc_table_addr());
12221225

1223-
q.state.avail_ring = GuestAddress(0xffff_ffff);
1226+
q.with(|mut qstate| qstate.avail_ring = GuestAddress(0xffff_ffff));
12241227
assert!(!q.is_valid());
1225-
q.state.avail_ring = GuestAddress(0x1001);
1228+
q.with(|mut qstate| qstate.avail_ring = GuestAddress(0x1001));
12261229
assert!(!q.is_valid());
1227-
q.state.avail_ring = vq.avail_addr();
1230+
q.with(|mut qstate| qstate.avail_ring = vq.avail_addr());
12281231

1229-
q.state.used_ring = GuestAddress(0xffff_ffff);
1232+
q.with(|mut qstate| qstate.used_ring = GuestAddress(0xffff_ffff));
12301233
assert!(!q.is_valid());
1231-
q.state.used_ring = GuestAddress(0x1001);
1234+
q.with(|mut qstate| qstate.used_ring = GuestAddress(0x1001));
12321235
assert!(!q.is_valid());
1233-
q.state.used_ring = vq.used_addr();
1236+
q.with(|mut qstate| qstate.used_ring = vq.used_addr());
12341237

1235-
{
1238+
q.with(|mut qstate| {
12361239
// an invalid queue should return an iterator with no next
1237-
q.state.ready = false;
1238-
let mut i = q.iter().unwrap();
1240+
qstate.ready = false;
1241+
let mut i = qstate.iter().unwrap();
12391242
assert!(i.next().is_none());
1240-
}
1243+
});
12411244

1242-
q.state.ready = true;
1245+
q.with(|mut qstate| qstate.ready = true);
12431246

12441247
// now let's create two simple descriptor chains
12451248
// the chains are (0, 1) and (2, 3, 4)
1246-
{
1247-
for j in 0..5u16 {
1248-
let flags = match j {
1249-
1 | 4 => 0,
1250-
_ => VIRTQ_DESC_F_NEXT,
1251-
};
1252-
1253-
let desc = Descriptor::new((0x1000 * (j + 1)) as u64, 0x1000, flags, j + 1);
1254-
vq.desc_table().store(j, desc);
1255-
}
1249+
for j in 0..5u16 {
1250+
let flags = match j {
1251+
1 | 4 => 0,
1252+
_ => VIRTQ_DESC_F_NEXT,
1253+
};
12561254

1257-
vq.avail().ring().ref_at(0).store(0);
1258-
vq.avail().ring().ref_at(1).store(2);
1259-
vq.avail().idx().store(2);
1255+
let desc = Descriptor::new((0x1000 * (j + 1)) as u64, 0x1000, flags, j + 1);
1256+
vq.desc_table().store(j, desc);
1257+
}
1258+
1259+
vq.avail().ring().ref_at(0).store(0);
1260+
vq.avail().ring().ref_at(1).store(2);
1261+
vq.avail().idx().store(2);
12601262

1261-
let mut i = q.iter().unwrap();
1263+
q.with(|mut qstate| {
1264+
let mut i = qstate.iter().unwrap();
12621265

12631266
{
12641267
let mut c = i.next().unwrap();
@@ -1285,13 +1288,13 @@ mod tests {
12851288
{
12861289
assert!(i.next().is_none());
12871290
i.go_to_previous_position();
1288-
let mut c = q.iter().unwrap().next().unwrap();
1291+
let mut c = qstate.iter().unwrap().next().unwrap();
12891292
c.next().unwrap();
12901293
c.next().unwrap();
12911294
c.next().unwrap();
12921295
assert!(c.next().is_none());
12931296
}
1294-
}
1297+
})
12951298
}
12961299

12971300
#[test]
@@ -1321,39 +1324,41 @@ mod tests {
13211324
vq.avail().ring().ref_at(2).store(5);
13221325
vq.avail().idx().store(3);
13231326

1324-
let mut i = q.iter().unwrap();
1327+
q.with(|mut qstate| {
1328+
let mut i = qstate.iter().unwrap();
13251329

1326-
{
1327-
let c = i.next().unwrap();
1328-
assert_eq!(c.head_index(), 0);
1329-
1330-
let mut iter = c;
1331-
assert!(iter.next().is_some());
1332-
assert!(iter.next().is_some());
1333-
assert!(iter.next().is_none());
1334-
assert!(iter.next().is_none());
1335-
}
1330+
{
1331+
let c = i.next().unwrap();
1332+
assert_eq!(c.head_index(), 0);
13361333

1337-
{
1338-
let c = i.next().unwrap();
1339-
assert_eq!(c.head_index(), 2);
1340-
1341-
let mut iter = c.writable();
1342-
assert!(iter.next().is_some());
1343-
assert!(iter.next().is_some());
1344-
assert!(iter.next().is_none());
1345-
assert!(iter.next().is_none());
1346-
}
1334+
let mut iter = c;
1335+
assert!(iter.next().is_some());
1336+
assert!(iter.next().is_some());
1337+
assert!(iter.next().is_none());
1338+
assert!(iter.next().is_none());
1339+
}
13471340

1348-
{
1349-
let c = i.next().unwrap();
1350-
assert_eq!(c.head_index(), 5);
1341+
{
1342+
let c = i.next().unwrap();
1343+
assert_eq!(c.head_index(), 2);
13511344

1352-
let mut iter = c.readable();
1353-
assert!(iter.next().is_some());
1354-
assert!(iter.next().is_none());
1355-
assert!(iter.next().is_none());
1356-
}
1345+
let mut iter = c.writable();
1346+
assert!(iter.next().is_some());
1347+
assert!(iter.next().is_some());
1348+
assert!(iter.next().is_none());
1349+
assert!(iter.next().is_none());
1350+
}
1351+
1352+
{
1353+
let c = i.next().unwrap();
1354+
assert_eq!(c.head_index(), 5);
1355+
1356+
let mut iter = c.readable();
1357+
assert!(iter.next().is_some());
1358+
assert!(iter.next().is_none());
1359+
assert!(iter.next().is_none());
1360+
}
1361+
})
13571362
}
13581363

13591364
#[test]
@@ -1371,7 +1376,7 @@ mod tests {
13711376

13721377
// should be ok
13731378
q.add_used(1, 0x1000).unwrap();
1374-
assert_eq!(q.state.next_used, Wrapping(1));
1379+
q.with(|qstate| assert_eq!(qstate.next_used, Wrapping(1)));
13751380
assert_eq!(vq.used().idx().load(), 1);
13761381

13771382
let x = vq.used().ring().ref_at(0).load();
@@ -1399,11 +1404,13 @@ mod tests {
13991404
let vq = MockSplitQueue::new(m, 16);
14001405

14011406
let mut q: Queue<_> = vq.as_queue(m);
1402-
q.state.size = 8;
1403-
q.state.ready = true;
1404-
q.state.reset();
1405-
assert_eq!(q.state.size, 16);
1406-
assert_eq!(q.state.ready, false);
1407+
q.with(|mut qstate| {
1408+
qstate.size = 8;
1409+
qstate.ready = true;
1410+
qstate.reset();
1411+
assert_eq!(qstate.size, 16);
1412+
assert_eq!(qstate.ready, false);
1413+
})
14071414
}
14081415

14091416
#[test]
@@ -1417,21 +1424,24 @@ mod tests {
14171424

14181425
// It should always return true when EVENT_IDX isn't enabled.
14191426
for i in 0..qsize {
1420-
q.state.next_used = Wrapping(i);
1427+
q.with(|mut qstate| qstate.next_used = Wrapping(i));
14211428
assert_eq!(q.needs_notification().unwrap(), true);
14221429
}
14231430

14241431
m.write_obj::<u16>(4, avail_addr.unchecked_add(4 + qsize as u64 * 2))
14251432
.unwrap();
1426-
q.state.set_event_idx(true);
1433+
q.with(|mut qstate| qstate.set_event_idx(true));
14271434

14281435
// Incrementing up to this value causes an `u16` to wrap back to 0.
14291436
let wrap = u32::from(u16::MAX) + 1;
14301437

14311438
for i in 0..wrap + 12 {
1432-
q.state.next_used = Wrapping(i as u16);
1433-
// Let's test wrapping around the maximum index value as well.
1434-
let expected = i == 5 || i == (5 + wrap) || q.state.signalled_used.is_none();
1439+
let expected = q.with(|mut qstate| {
1440+
qstate.next_used = Wrapping(i as u16);
1441+
// Let's test wrapping around the maximum index value as well.
1442+
i == 5 || i == (5 + wrap) || qstate.signalled_used.is_none()
1443+
});
1444+
14351445
assert_eq!(q.needs_notification().unwrap(), expected);
14361446
}
14371447

@@ -1445,9 +1455,9 @@ mod tests {
14451455
.unwrap();
14461456

14471457
assert_eq!(q.needs_notification().unwrap(), false);
1448-
q.state.next_used = Wrapping(15);
1458+
q.with(|mut qstate| qstate.next_used = Wrapping(15));
14491459
assert_eq!(q.needs_notification().unwrap(), false);
1450-
q.state.next_used = Wrapping(0);
1460+
q.with(|mut qstate| qstate.next_used = Wrapping(0));
14511461
assert_eq!(q.needs_notification().unwrap(), true);
14521462
assert_eq!(q.needs_notification().unwrap(), false);
14531463
}
@@ -1460,7 +1470,7 @@ mod tests {
14601470
let mut q: Queue<_> = vq.as_queue(m);
14611471
let used_addr = vq.used_addr();
14621472

1463-
assert_eq!(q.state.event_idx_enabled, false);
1473+
q.with(|qstate| assert_eq!(qstate.event_idx_enabled, false));
14641474

14651475
q.enable_notification().unwrap();
14661476
let v = m.read_obj::<u16>(used_addr).unwrap();
@@ -1479,13 +1489,13 @@ mod tests {
14791489
m.write_obj::<u16>(2, avail_addr.unchecked_add(2)).unwrap();
14801490

14811491
assert_eq!(q.enable_notification().unwrap(), true);
1482-
q.state.next_avail = Wrapping(2);
1492+
q.with(|mut qstate| qstate.next_avail = Wrapping(2));
14831493
assert_eq!(q.enable_notification().unwrap(), false);
14841494

14851495
m.write_obj::<u16>(8, avail_addr.unchecked_add(2)).unwrap();
14861496

14871497
assert_eq!(q.enable_notification().unwrap(), true);
1488-
q.state.next_avail = Wrapping(8);
1498+
q.with(|mut qstate| qstate.next_avail = Wrapping(8));
14891499
assert_eq!(q.enable_notification().unwrap(), false);
14901500
}
14911501
}

0 commit comments

Comments
 (0)