@@ -18,7 +18,6 @@ def setUp(self):
18
18
set_logging_levels ()
19
19
logging .setup (cfg .CONF , "demo" )
20
20
21
-
22
21
def test_rerunner_simple (self ):
23
22
from networking_nsxv3 .common .synchronization import JobRerunner , Runnable
24
23
@@ -43,7 +42,6 @@ def nop(item):
43
42
# no job was added twice, so no job should be re-executed
44
43
self .assertEqual (rerunner .pop (), None )
45
44
46
-
47
45
def test_rerunner_rerun (self ):
48
46
from networking_nsxv3 .common .synchronization import JobRerunner , Runnable
49
47
@@ -84,14 +82,73 @@ def nop(item):
84
82
# no job should have the same hash
85
83
self .assertEqual (len (set (alljobs )), len (alljobs ))
86
84
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