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;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
30 public class TestMultiThreadAddDelete {
31 private static final Logger LOG = LoggerFactory.getLogger(TestMultiThreadAddDelete.class);
32 private static final int RETRIES = 1;
33 private static final int N_THREADS = 7;
34 private static final int COUNT = 50 * 1000;
37 public void testMultiThreadAddDelete() throws InterruptedException {
38 for (int j = 0; j < RETRIES; j++) {
39 final Map<Object, Object> bt = TrieMap.create();
42 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
43 for (int i = 0; i < N_THREADS; i++) {
44 final int threadNo = i;
46 for (int k = 0; k < COUNT; k++) {
47 if (k % N_THREADS == threadNo) {
48 bt.put(Integer.valueOf(k), Integer.valueOf(k));
54 es.awaitTermination(5, TimeUnit.MINUTES);
57 assertEquals(COUNT, bt.size());
58 assertFalse(bt.isEmpty());
61 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
62 for (int i = 0; i < N_THREADS; i++) {
63 final int threadNo = i;
65 for (int k = 0; k < COUNT; k++) {
66 if (k % N_THREADS == threadNo) {
67 bt.remove(Integer.valueOf(k));
73 es.awaitTermination(5, TimeUnit.MINUTES);
77 assertEquals(0, bt.size());
78 assertTrue(bt.isEmpty());
81 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
82 for (int i = 0; i < N_THREADS; i++) {
83 final int threadNo = i;
84 es.execute(new Runnable() {
87 for (int j = 0; j < COUNT; j++) {
88 if (j % N_THREADS == threadNo) {
89 bt.put(Integer.valueOf(j), Integer.valueOf(j));
90 if (!bt.containsKey(Integer.valueOf(j))) {
91 LOG.error("Key {} not present", j);
93 bt.remove(Integer.valueOf(j));
94 if (bt.containsKey(Integer.valueOf(j))) {
95 LOG.error("Key {} is still present", j);
103 es.awaitTermination(5, TimeUnit.MINUTES);
106 assertEquals(0, bt.size());
107 assertTrue(bt.isEmpty());