b575e915dcb9b1281988e423829dfa39bdb81743
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestMultiThreadAddDelete.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.assertFalse;
20 import static org.junit.Assert.assertTrue;
21
22 import java.util.Map;
23 import java.util.concurrent.ExecutorService;
24 import java.util.concurrent.Executors;
25 import java.util.concurrent.TimeUnit;
26 import org.junit.Test;
27
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;
32
33     @Test
34     public void testMultiThreadAddDelete() throws InterruptedException {
35         for (int j = 0; j < RETRIES; j++) {
36             final Map<Object, Object> bt = TrieMap.create();
37
38             {
39                 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
40                 for (int i = 0; i < N_THREADS; i++) {
41                     final int threadNo = i;
42                     es.execute(() -> {
43                         for (int k = 0; k < COUNT; k++) {
44                             if (k % N_THREADS == threadNo) {
45                                 bt.put(Integer.valueOf(k), Integer.valueOf(k));
46                             }
47                         }
48                     });
49                 }
50                 es.shutdown();
51                 es.awaitTermination(5, TimeUnit.MINUTES);
52             }
53
54             assertEquals(COUNT, bt.size());
55             assertFalse(bt.isEmpty());
56
57             {
58                 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
59                 for (int i = 0; i < N_THREADS; i++) {
60                     final int threadNo = i;
61                     es.execute(() -> {
62                         for (int k = 0; k < COUNT; k++) {
63                             if (k % N_THREADS == threadNo) {
64                                 bt.remove(Integer.valueOf(k));
65                             }
66                         }
67                     });
68                 }
69                 es.shutdown();
70                 es.awaitTermination(5, TimeUnit.MINUTES);
71             }
72
73
74             assertEquals(0, bt.size());
75             assertTrue(bt.isEmpty());
76
77             {
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() {
82                         @Override
83                         public void run() {
84                             for (int j = 0; j < COUNT; j++) {
85                                 if (j % N_THREADS == threadNo) {
86                                     bt.put(Integer.valueOf(j), Integer.valueOf(j));
87                                     if (!bt.containsKey(Integer.valueOf(j))) {
88                                         System.out.println(j);
89                                     }
90                                     bt.remove(Integer.valueOf(j));
91                                     if (bt.containsKey(Integer.valueOf(j))) {
92                                         System.out.println(-j);
93                                     }
94                                 }
95                             }
96                         }
97                     });
98                 }
99                 es.shutdown();
100                 es.awaitTermination(5, TimeUnit.MINUTES);
101             }
102
103             assertEquals(0, bt.size());
104             assertTrue(bt.isEmpty());
105         }
106     }
107 }