BUG-7464: remove TestHelper
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestMultiThreadMapIterator.java
1 /*
2  * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.opendaylight.yangtools.triemap;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.concurrent.ExecutorService;
28 import java.util.concurrent.Executors;
29 import java.util.concurrent.TimeUnit;
30 import org.junit.Test;
31
32 public class TestMultiThreadMapIterator {
33     private static final int NTHREADS = 7;
34
35     @Test
36     public void testMultiThreadMapIterator () throws InterruptedException {
37         final Map<Object, Object> bt = new TrieMap<>();
38         for (int j = 0; j < 50 * 1000; j++) {
39             for (final Object o : getObjects(j)) {
40                 bt.put (o, o);
41             }
42         }
43
44         // System.out.println ("Size of initialized map is " + bt.size ());
45         int count = 0;
46         {
47             final ExecutorService es = Executors.newFixedThreadPool(NTHREADS);
48             for (int i = 0; i < NTHREADS; i++) {
49                 final int threadNo = i;
50                 es.execute(() -> {
51                     for (Entry<Object, Object> e : bt.entrySet()) {
52                         if (accepts(threadNo, NTHREADS, e.getKey())) {
53                             String newValue = "TEST:" + threadNo;
54                             e.setValue(newValue);
55                         }
56                     }
57                 });
58             }
59
60             es.shutdown();
61             es.awaitTermination(5, TimeUnit.MINUTES);
62         }
63
64         count = 0;
65         for (final Map.Entry<Object, Object> kv : bt.entrySet()) {
66             assertTrue(kv.getValue() instanceof String);
67             count++;
68         }
69         assertEquals(50000 + 2000 + 1000 + 100, count);
70
71         final ConcurrentHashMap<Object, Object> removed = new ConcurrentHashMap<>();
72         {
73             final ExecutorService es = Executors.newFixedThreadPool(NTHREADS);
74             for (int i = 0; i < NTHREADS; i++) {
75                 final int threadNo = i;
76                 es.execute (() -> {
77                     for (final Iterator<Map.Entry<Object, Object>> it = bt.entrySet ().iterator(); it.hasNext();) {
78                         final Entry<Object, Object> e = it.next();
79                         Object key = e.getKey ();
80                         if (accepts (threadNo, NTHREADS, key)) {
81                             if (null == bt.get (key)) {
82                                 // System.out.println (key);
83                             }
84                             it.remove();
85                             if (null != bt.get (key)) {
86                                 // System.out.println (key);
87                             }
88                             removed.put (key, key);
89                         }
90                     }
91                 });
92             }
93
94             es.shutdown();
95             es.awaitTermination(5, TimeUnit.MINUTES);
96         }
97
98       count = 0;
99       for (final Object value : bt.keySet ()) {
100           value.toString ();
101           count++;
102       }
103       for (final Object o : bt.keySet ()) {
104           if (!removed.contains (bt.get (o))) {
105               System.out.println ("Not removed: " + o);
106           }
107       }
108       assertEquals(0, count);
109       assertEquals(0, bt.size ());
110       assertTrue(bt.isEmpty ());
111     }
112
113     protected static boolean accepts (final int threadNo, final int nThreads, final Object key) {
114         final int val = getKeyValue(key);
115         return val >= 0 ? val % nThreads == threadNo : false;
116     }
117
118     private static int getKeyValue (final Object key) {
119         if (key instanceof Integer) {
120             return ((Integer) key).intValue();
121         } else if (key instanceof Character) {
122             return Math.abs(Character.getNumericValue((Character) key) + 1);
123         } else if (key instanceof Short) {
124             return ((Short) key).intValue() + 2;
125         } else if (key instanceof Byte) {
126             return ((Byte) key).intValue() + 3;
127         } else {
128             return -1;
129         }
130     }
131
132     static Collection<Object> getObjects(final int j) {
133         final Collection<Object> results = new ArrayList<>(4);
134         results.add(Integer.valueOf(j));
135         if (j < 2000) {
136             results.add(Character.valueOf((char) j));
137         }
138         if (j < 1000) {
139             results.add(Short.valueOf((short) j));
140         }
141         if (j < 100) {
142             results.add(Byte.valueOf((byte) j));
143         }
144
145         return results;
146     }
147 }