1919
2020import java .util .HashMap ;
2121import java .util .HashSet ;
22+ import java .util .Iterator ;
2223import java .util .Map ;
2324import java .util .Set ;
25+ import java .util .TreeMap ;
26+ import java .util .concurrent .ThreadLocalRandom ;
2427import org .apache .ignite .internal .processors .cache .distributed .dht .topology .GridDhtPartitionState ;
2528import org .apache .ignite .internal .util .GridPartitionStateMap ;
2629import org .apache .ignite .testframework .junits .common .GridCommonAbstractTest ;
@@ -144,6 +147,96 @@ public void testCopyNoActive() {
144147 assertEquals (1 , cp2 .size ());
145148 }
146149
150+ /**
151+ * Tests that entries from {@link Iterator#next()} remain unaltered.
152+ */
153+ public void testIteratorNext () {
154+ GridPartitionStateMap map = new GridPartitionStateMap ();
155+
156+ initMap (map );
157+
158+ Iterator <Map .Entry <Integer , GridDhtPartitionState >> iter = map .entrySet ().iterator ();
159+
160+ for (int i = 0 ; i < map .size () + 1 ; i ++)
161+ assertTrue (iter .hasNext ());
162+
163+ Map .Entry <Integer , GridDhtPartitionState > entry1 = iter .next ();
164+
165+ for (int i = 0 ; i < map .size () + 1 ; i ++)
166+ assertTrue (iter .hasNext ());
167+
168+ Map .Entry <Integer , GridDhtPartitionState > entry2 = iter .next ();
169+
170+ iter .remove ();
171+
172+ assertNotNull (entry1 .getValue ());
173+ assertNotNull (entry2 .getValue ());
174+
175+ assertEquals (Integer .valueOf (0 ), entry1 .getKey ());
176+ assertEquals (Integer .valueOf (1 ), entry2 .getKey ());
177+
178+ assertEquals (GridDhtPartitionState .MOVING , entry1 .getValue ());
179+ assertEquals (GridDhtPartitionState .RENTING , entry2 .getValue ());
180+ }
181+
182+ /**
183+ * Tests {@link GridDhtPartitionState} compatibility with {@link TreeMap} on random operations.
184+ */
185+ public void testOnRandomOperations () {
186+ ThreadLocalRandom rnd = ThreadLocalRandom .current ();
187+
188+ Map <Integer , GridDhtPartitionState > treeMap = new TreeMap <>();
189+ Map <Integer , GridDhtPartitionState > gridMap = new GridPartitionStateMap ();
190+
191+ int statesNum = GridDhtPartitionState .values ().length ;
192+
193+ for (int i = 0 ; i < 10000 ; i ++) {
194+ Integer part = rnd .nextInt (65536 );
195+
196+ GridDhtPartitionState state = GridDhtPartitionState .fromOrdinal (rnd .nextInt (statesNum ));
197+
198+ int rndOperation = rnd .nextInt (9 );
199+
200+ if (rndOperation <= 5 ) {
201+ treeMap .put (part , state );
202+ gridMap .put (part , state );
203+ }
204+ else if (rndOperation == 6 ) {
205+ treeMap .remove (part );
206+ gridMap .remove (part );
207+ }
208+ else if (!treeMap .isEmpty ()) {
209+ int n = rnd .nextInt (0 , treeMap .size ());
210+
211+ Iterator <Map .Entry <Integer , GridDhtPartitionState >> iter1 = treeMap .entrySet ().iterator ();
212+ Iterator <Map .Entry <Integer , GridDhtPartitionState >> iter2 = gridMap .entrySet ().iterator ();
213+
214+ Map .Entry <Integer , GridDhtPartitionState > entry1 = iter1 .next ();
215+ Map .Entry <Integer , GridDhtPartitionState > entry2 = iter2 .next ();
216+
217+ for (int j = 1 ; j <= n ; j ++) {
218+ entry1 = iter1 .next ();
219+ entry2 = iter2 .next ();
220+
221+ assertEquals (entry1 .getValue (), entry2 .getValue ());
222+ }
223+
224+ if (rndOperation == 7 ) {
225+ entry1 .setValue (state );
226+ entry2 .setValue (state );
227+ }
228+ else {
229+ iter1 .remove ();
230+ iter2 .remove ();
231+ }
232+ }
233+
234+ assertEquals (treeMap .size (), gridMap .size ());
235+ }
236+
237+ assertEquals (treeMap , gridMap );
238+ }
239+
147240 /** */
148241 private GridPartitionStateMap initMap (GridPartitionStateMap map ) {
149242 map .put (0 , GridDhtPartitionState .MOVING );
@@ -159,4 +252,4 @@ private GridPartitionStateMap initMap(GridPartitionStateMap map) {
159252
160253 return map ;
161254 }
162- }
255+ }
0 commit comments