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;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.Test;
24 public class TestMultiThreadAddDelete {
25 private static final int RETRIES = 1;
26 private static final int N_THREADS = 7;
27 private static final int COUNT = 50 * 1000;
30 public void testMultiThreadAddDelete () {
31 for (int j = 0; j < RETRIES; j++) {
32 final Map<Object, Object> bt = new TrieMap <> ();
35 final ExecutorService es = Executors.newFixedThreadPool (N_THREADS);
36 for (int i = 0; i < N_THREADS; i++) {
37 final int threadNo = i;
38 es.execute (new Runnable () {
41 for (int j = 0; j < COUNT; j++) {
42 if (j % N_THREADS == threadNo) {
43 bt.put (Integer.valueOf (j), Integer.valueOf (j));
51 es.awaitTermination (3600L, TimeUnit.SECONDS);
52 } catch (final InterruptedException e) {
57 TestHelper.assertEquals (COUNT, bt.size ());
58 TestHelper.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;
64 es.execute (new Runnable () {
67 for (int j = 0; j < COUNT; j++) {
68 if (j % N_THREADS == threadNo) {
69 bt.remove (Integer.valueOf (j));
77 es.awaitTermination (3600L, TimeUnit.SECONDS);
78 } catch (final InterruptedException e) {
84 TestHelper.assertEquals (0, bt.size ());
85 TestHelper.assertTrue (bt.isEmpty ());
88 final ExecutorService es = Executors.newFixedThreadPool (N_THREADS);
89 for (int i = 0; i < N_THREADS; i++) {
90 final int threadNo = i;
91 es.execute (new Runnable () {
94 for (int j = 0; j < COUNT; j++) {
95 if (j % N_THREADS == threadNo) {
97 bt.put (Integer.valueOf (j), Integer.valueOf (j));
98 if (!bt.containsKey (Integer.valueOf (j))) {
99 System.out.println (j);
101 bt.remove (Integer.valueOf (j));
102 if (bt.containsKey (Integer.valueOf (j))) {
103 System.out.println (-j);
105 } catch (Throwable t) {
106 t.printStackTrace ();
115 es.awaitTermination (3600L, TimeUnit.SECONDS);
116 } catch (final InterruptedException e) {
117 e.printStackTrace ();
121 TestHelper.assertEquals (0, bt.size ());
122 if (!bt.isEmpty ()) {
123 System.out.println ();
125 TestHelper.assertTrue (bt.isEmpty ());