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.assertFalse;
20 import static org.junit.Assert.assertTrue;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.TimeUnit;
26 import org.junit.Test;
28 public class TestMultiThreadAddDelete {
29 private static final int RETRIES = 1;
30 private static final int N_THREADS = 7;
31 private static final int COUNT = 50 * 1000;
34 public void testMultiThreadAddDelete () throws InterruptedException {
35 for (int j = 0; j < RETRIES; j++) {
36 final Map<Object, Object> bt = new TrieMap<>();
39 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
40 for (int i = 0; i < N_THREADS; i++) {
41 final int threadNo = i;
43 for (int k = 0; k < COUNT; k++) {
44 if (k % N_THREADS == threadNo) {
45 bt.put(Integer.valueOf(k), Integer.valueOf(k));
51 es.awaitTermination(5, TimeUnit.MINUTES);
54 assertEquals(COUNT, bt.size());
55 assertFalse(bt.isEmpty());
58 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
59 for (int i = 0; i < N_THREADS; i++) {
60 final int threadNo = i;
62 for (int k = 0; k < COUNT; k++) {
63 if (k % N_THREADS == threadNo) {
64 bt.remove (Integer.valueOf (k));
70 es.awaitTermination(5, TimeUnit.MINUTES);
74 assertEquals(0, bt.size ());
75 assertTrue(bt.isEmpty());
78 final ExecutorService es = Executors.newFixedThreadPool (N_THREADS);
79 for (int i = 0; i < N_THREADS; i++) {
80 final int threadNo = i;
81 es.execute (new Runnable () {
84 for (int j = 0; j < COUNT; j++) {
85 if (j % N_THREADS == threadNo) {
87 bt.put (Integer.valueOf (j), Integer.valueOf (j));
88 if (!bt.containsKey (Integer.valueOf (j))) {
89 System.out.println (j);
91 bt.remove (Integer.valueOf (j));
92 if (bt.containsKey (Integer.valueOf (j))) {
93 System.out.println (-j);
95 } catch (Throwable t) {
104 es.awaitTermination(5, TimeUnit.MINUTES);
107 assertEquals(0, bt.size());
108 assertTrue(bt.isEmpty());