Skip to content

Commit a0c426d

Browse files
committed
[tests] adding tests for UniqPriorityQueue and UniqFifoQueue
1 parent c2a198d commit a0c426d

File tree

1 file changed

+70
-13
lines changed

1 file changed

+70
-13
lines changed

networking_nsxv3/tests/unit/realization/test_coordination.py

Lines changed: 70 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ def setUp(self):
1818
set_logging_levels()
1919
logging.setup(cfg.CONF, "demo")
2020

21-
2221
def test_rerunner_simple(self):
2322
from networking_nsxv3.common.synchronization import JobRerunner, Runnable
2423

@@ -43,7 +42,6 @@ def nop(item):
4342
# no job was added twice, so no job should be re-executed
4443
self.assertEqual(rerunner.pop(), None)
4544

46-
4745
def test_rerunner_rerun(self):
4846
from networking_nsxv3.common.synchronization import JobRerunner, Runnable
4947

@@ -84,14 +82,73 @@ def nop(item):
8482
# no job should have the same hash
8583
self.assertEqual(len(set(alljobs)), len(alljobs))
8684

87-
88-
89-
90-
91-
92-
93-
94-
95-
96-
97-
85+
def test_fifoqueue(self):
86+
from networking_nsxv3.common.synchronization import UniqFiFoQueue
87+
from eventlet.queue import Empty
88+
queue = UniqFiFoQueue()
89+
90+
for i in range(10):
91+
queue.put(i)
92+
queue.put(i)
93+
for i in range(20, 10, -1):
94+
queue.put(i)
95+
96+
try:
97+
ret = []
98+
for i in range(200):
99+
item = queue.get(block=False)
100+
ret.append(item)
101+
except Empty:
102+
pass
103+
104+
expected = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11]
105+
self.assertEqual(expected, ret)
106+
107+
def test_prioqueue(self):
108+
from networking_nsxv3.common.synchronization import UniqPriorityQueue
109+
from eventlet.queue import Empty
110+
queue = UniqPriorityQueue()
111+
112+
class PrioItem:
113+
114+
def __init__(self, uid, prio):
115+
self.priority = prio
116+
self.uid = uid
117+
118+
def __lt__(self, other):
119+
if self.priority < other.priority:
120+
return -1
121+
elif self.priority > other.priority:
122+
return 1
123+
return 0
124+
125+
def __eq__(self, other):
126+
return self.uid == other.uid
127+
128+
def __repr__(self):
129+
return f"PrioItem({self.uid},{self.priority})"
130+
131+
for i in range(10):
132+
queue.put(PrioItem(i, 5))
133+
queue.put(PrioItem(i, 1))
134+
for i in range(15, 5, -1):
135+
queue.put(PrioItem(i, 4))
136+
137+
try:
138+
ret = []
139+
for i in range(200):
140+
item = queue.get(block=False)
141+
ret.append((item.uid, item.priority))
142+
except Empty:
143+
pass
144+
145+
# the first 6 items should have the lowest prio,
146+
# then 10 with the highest, prio 5 should never be seen
147+
self.assertEqual([4] * 6, [prio for uid, prio in ret[:6]])
148+
self.assertEqual([1] * 10, [prio for uid, prio in ret[6:]])
149+
150+
# each item should be present only once.
151+
# because sorting is not stable, we need to sort the uids
152+
# to compare
153+
ret = sorted([uid for uid, prio in ret])
154+
self.assertEqual(list(range(16)), ret)

0 commit comments

Comments
 (0)