4b2d78ad839524565b1e2d333a9068cf3ab339b4
[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 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29
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;
35
36     @Test
37     public void testMultiThreadAddDelete() throws InterruptedException {
38         for (int j = 0; j < RETRIES; j++) {
39             final Map<Object, Object> bt = TrieMap.create();
40
41             {
42                 final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
43                 for (int i = 0; i < N_THREADS; i++) {
44                     final int threadNo = i;
45                     es.execute(() -> {
46                         for (int k = 0; k < COUNT; k++) {
47                             if (k % N_THREADS == threadNo) {
48                                 bt.put(Integer.valueOf(k), Integer.valueOf(k));
49                             }
50                         }
51                     });
52                 }
53                 es.shutdown();
54                 es.awaitTermination(5, TimeUnit.MINUTES);
55             }
56
57             assertEquals(COUNT, bt.size());
58             assertFalse(bt.isEmpty());
59
60             {
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(() -> {
65                         for (int k = 0; k < COUNT; k++) {
66                             if (k % N_THREADS == threadNo) {
67                                 bt.remove(Integer.valueOf(k));
68                             }
69                         }
70                     });
71                 }
72                 es.shutdown();
73                 es.awaitTermination(5, TimeUnit.MINUTES);
74             }
75
76
77             assertEquals(0, bt.size());
78             assertTrue(bt.isEmpty());
79
80             {
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() {
85                         @Override
86                         public void run() {
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);
92                                     }
93                                     bt.remove(Integer.valueOf(j));
94                                     if (bt.containsKey(Integer.valueOf(j))) {
95                                         LOG.error("Key {} is still present", j);
96                                     }
97                                 }
98                             }
99                         }
100                     });
101                 }
102                 es.shutdown();
103                 es.awaitTermination(5, TimeUnit.MINUTES);
104             }
105
106             assertEquals(0, bt.size());
107             assertTrue(bt.isEmpty());
108         }
109     }
110 }