2 * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.opendaylight.yangtools.triemap;
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertTrue;
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Iterator;
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;
32 public class TestMultiThreadMapIterator {
33 private static final int NTHREADS = 7;
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)) {
44 // System.out.println ("Size of initialized map is " + bt.size ());
47 final ExecutorService es = Executors.newFixedThreadPool(NTHREADS);
48 for (int i = 0; i < NTHREADS; i++) {
49 final int threadNo = i;
51 for (Entry<Object, Object> e : bt.entrySet()) {
52 if (accepts(threadNo, NTHREADS, e.getKey())) {
53 String newValue = "TEST:" + threadNo;
61 es.awaitTermination(5, TimeUnit.MINUTES);
65 for (final Map.Entry<Object, Object> kv : bt.entrySet()) {
66 assertTrue(kv.getValue() instanceof String);
69 assertEquals(50000 + 2000 + 1000 + 100, count);
71 final ConcurrentHashMap<Object, Object> removed = new ConcurrentHashMap<>();
73 final ExecutorService es = Executors.newFixedThreadPool(NTHREADS);
74 for (int i = 0; i < NTHREADS; i++) {
75 final int threadNo = i;
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);
85 if (null != bt.get (key)) {
86 // System.out.println (key);
88 removed.put (key, key);
95 es.awaitTermination(5, TimeUnit.MINUTES);
99 for (final Object value : bt.keySet ()) {
103 for (final Object o : bt.keySet ()) {
104 if (!removed.contains (bt.get (o))) {
105 System.out.println ("Not removed: " + o);
108 assertEquals(0, count);
109 assertEquals(0, bt.size ());
110 assertTrue(bt.isEmpty ());
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;
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;
132 static Collection<Object> getObjects(final int j) {
133 final Collection<Object> results = new ArrayList<>(4);
134 results.add(Integer.valueOf(j));
136 results.add(Character.valueOf((char) j));
139 results.add(Short.valueOf((short) j));
142 results.add(Byte.valueOf((byte) j));