Migrate to tech.pantheon.TrieMap
[yangtools.git] / third-party / triemap / src / test / java / org / opendaylight / yangtools / triemap / TestMultiThreadAddDelete.java
index efcb846a9b63b4323af6cfcf222fa722407fd7e7..bbfe70ed3def695638dccb8ca8039a9cbd8cff96 100644 (file)
+/*
+ * (C) Copyright 2016 Pantheon Technologies, s.r.o. and others.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.opendaylight.yangtools.triemap;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
 import java.util.Map;
 import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
-
 import org.junit.Test;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
+@Deprecated
 public class TestMultiThreadAddDelete {
+    private static final Logger LOG = LoggerFactory.getLogger(TestMultiThreadAddDelete.class);
     private static final int RETRIES = 1;
     private static final int N_THREADS = 7;
-    private static final int COUNT =  50 * 1000;
+    private static final int COUNT = 50 * 1000;
 
     @Test
-    public void testMultiThreadAddDelete () {
+    public void testMultiThreadAddDelete() throws InterruptedException {
         for (int j = 0; j < RETRIES; j++) {
-            final Map<Object, Object> bt = new TrieMap <Object, Object> ();
-            
+            final Map<Object, Object> bt = TrieMap.create();
+
             {
-                final ExecutorService es = Executors.newFixedThreadPool (N_THREADS);
+                final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
                 for (int i = 0; i < N_THREADS; i++) {
                     final int threadNo = i;
-                    es.execute (new Runnable () {
-                        @Override
-                        public void run () {
-                            for (int j = 0; j < COUNT; j++) {
-                                if (j % N_THREADS == threadNo) {
-                                    bt.put (Integer.valueOf (j), Integer.valueOf (j));
-                                }
+                    es.execute(() -> {
+                        for (int k = 0; k < COUNT; k++) {
+                            if (k % N_THREADS == threadNo) {
+                                bt.put(Integer.valueOf(k), Integer.valueOf(k));
                             }
                         }
                     });
                 }
-                es.shutdown ();
-                try {
-                    es.awaitTermination (3600L, TimeUnit.SECONDS);
-                } catch (final InterruptedException e) {
-                    e.printStackTrace ();
-                }
+                es.shutdown();
+                es.awaitTermination(5, TimeUnit.MINUTES);
             }
-            
-            TestHelper.assertEquals (COUNT, bt.size ());
-            TestHelper.assertFalse (bt.isEmpty ());
-            
+
+            assertEquals(COUNT, bt.size());
+            assertFalse(bt.isEmpty());
+
             {
-                final ExecutorService es = Executors.newFixedThreadPool (N_THREADS);
+                final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
                 for (int i = 0; i < N_THREADS; i++) {
                     final int threadNo = i;
-                    es.execute (new Runnable () {
-                        @Override
-                        public void run () {
-                            for (int j = 0; j < COUNT; j++) {
-                                if (j % N_THREADS == threadNo) {
-                                    bt.remove (Integer.valueOf (j));
-                                }
+                    es.execute(() -> {
+                        for (int k = 0; k < COUNT; k++) {
+                            if (k % N_THREADS == threadNo) {
+                                bt.remove(Integer.valueOf(k));
                             }
                         }
                     });
                 }
-                es.shutdown ();
-                try {
-                    es.awaitTermination (3600L, TimeUnit.SECONDS);
-                } catch (final InterruptedException e) {
-                    e.printStackTrace ();
-                }
+                es.shutdown();
+                es.awaitTermination(5, TimeUnit.MINUTES);
             }
-            
-            
-            TestHelper.assertEquals (0, bt.size ());
-            TestHelper.assertTrue (bt.isEmpty ());
-            
+
+
+            assertEquals(0, bt.size());
+            assertTrue(bt.isEmpty());
+
             {
-                final ExecutorService es = Executors.newFixedThreadPool (N_THREADS);
+                final ExecutorService es = Executors.newFixedThreadPool(N_THREADS);
                 for (int i = 0; i < N_THREADS; i++) {
                     final int threadNo = i;
-                    es.execute (new Runnable () {
-                        @Override
-                        public void run () {
-                            for (int j = 0; j < COUNT; j++) {
-                                if (j % N_THREADS == threadNo) {
-                                    try {
-                                        bt.put (Integer.valueOf (j), Integer.valueOf (j));
-                                        if (!bt.containsKey (Integer.valueOf (j))) {
-                                            System.out.println (j);
-                                        }
-                                        bt.remove (Integer.valueOf (j));
-                                        if (bt.containsKey (Integer.valueOf (j))) {
-                                            System.out.println (-j);
-                                        }
-                                    } catch (Throwable t) {
-                                        t.printStackTrace ();
-                                    }
+                    es.execute(() -> {
+                        for (int j1 = 0; j1 < COUNT; j1++) {
+                            if (j1 % N_THREADS == threadNo) {
+                                bt.put(Integer.valueOf(j1), Integer.valueOf(j1));
+                                if (!bt.containsKey(Integer.valueOf(j1))) {
+                                    LOG.error("Key {} not present", j1);
+                                }
+                                bt.remove(Integer.valueOf(j1));
+                                if (bt.containsKey(Integer.valueOf(j1))) {
+                                    LOG.error("Key {} is still present", j1);
                                 }
                             }
                         }
                     });
                 }
-                es.shutdown ();
-                try {
-                    es.awaitTermination (3600L, TimeUnit.SECONDS);
-                } catch (final InterruptedException e) {
-                    e.printStackTrace ();
-                }
+                es.shutdown();
+                es.awaitTermination(5, TimeUnit.MINUTES);
             }
-            
-            TestHelper.assertEquals (0, bt.size ());
-            if (!bt.isEmpty ()) {
-                System.out.println ();
-            }
-            TestHelper.assertTrue (bt.isEmpty ());
+
+            assertEquals(0, bt.size());
+            assertTrue(bt.isEmpty());
         }
     }
 }