BUG-7464: cleanup and disable instantiation speed test
[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 java.util.Map;
19 import java.util.concurrent.ExecutorService;
20 import java.util.concurrent.Executors;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.Test;
23
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;
28
29     @Test
30     public void testMultiThreadAddDelete () {
31         for (int j = 0; j < RETRIES; j++) {
32             final Map<Object, Object> bt = new TrieMap <> ();
33
34             {
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 () {
39                         @Override
40                         public void run () {
41                             for (int j = 0; j < COUNT; j++) {
42                                 if (j % N_THREADS == threadNo) {
43                                     bt.put (Integer.valueOf (j), Integer.valueOf (j));
44                                 }
45                             }
46                         }
47                     });
48                 }
49                 es.shutdown ();
50                 try {
51                     es.awaitTermination (3600L, TimeUnit.SECONDS);
52                 } catch (final InterruptedException e) {
53                     e.printStackTrace ();
54                 }
55             }
56
57             TestHelper.assertEquals (COUNT, bt.size ());
58             TestHelper.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 (new Runnable () {
65                         @Override
66                         public void run () {
67                             for (int j = 0; j < COUNT; j++) {
68                                 if (j % N_THREADS == threadNo) {
69                                     bt.remove (Integer.valueOf (j));
70                                 }
71                             }
72                         }
73                     });
74                 }
75                 es.shutdown ();
76                 try {
77                     es.awaitTermination (3600L, TimeUnit.SECONDS);
78                 } catch (final InterruptedException e) {
79                     e.printStackTrace ();
80                 }
81             }
82
83
84             TestHelper.assertEquals (0, bt.size ());
85             TestHelper.assertTrue (bt.isEmpty ());
86
87             {
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 () {
92                         @Override
93                         public void run () {
94                             for (int j = 0; j < COUNT; j++) {
95                                 if (j % N_THREADS == threadNo) {
96                                     try {
97                                         bt.put (Integer.valueOf (j), Integer.valueOf (j));
98                                         if (!bt.containsKey (Integer.valueOf (j))) {
99                                             System.out.println (j);
100                                         }
101                                         bt.remove (Integer.valueOf (j));
102                                         if (bt.containsKey (Integer.valueOf (j))) {
103                                             System.out.println (-j);
104                                         }
105                                     } catch (Throwable t) {
106                                         t.printStackTrace ();
107                                     }
108                                 }
109                             }
110                         }
111                     });
112                 }
113                 es.shutdown ();
114                 try {
115                     es.awaitTermination (3600L, TimeUnit.SECONDS);
116                 } catch (final InterruptedException e) {
117                     e.printStackTrace ();
118                 }
119             }
120
121             TestHelper.assertEquals (0, bt.size ());
122             if (!bt.isEmpty ()) {
123                 System.out.println ();
124             }
125             TestHelper.assertTrue (bt.isEmpty ());
126         }
127     }
128 }