Ray Milkey | 8526700 | 2016-11-16 11:06:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present Open Networking Laboratory |
| 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 | |
Yuta HIGUCHI | e8f48d8 | 2016-08-24 20:27:48 -0700 | [diff] [blame] | 17 | package org.onlab.util; |
| 18 | |
| 19 | import java.util.concurrent.Callable; |
| 20 | import java.util.concurrent.CountDownLatch; |
| 21 | import java.util.concurrent.ExecutorService; |
| 22 | import java.util.concurrent.TimeUnit; |
| 23 | import java.util.concurrent.atomic.AtomicReference; |
| 24 | |
| 25 | import org.junit.After; |
| 26 | import org.junit.Before; |
| 27 | import org.junit.Test; |
| 28 | import org.onlab.util.PredictableExecutor.PickyRunnable; |
| 29 | import com.google.common.testing.EqualsTester; |
| 30 | |
| 31 | public class PredictableExecutorTest { |
| 32 | |
| 33 | private PredictableExecutor pexecutor; |
| 34 | private ExecutorService executor; |
| 35 | |
| 36 | @Before |
| 37 | public void setUp() { |
| 38 | pexecutor = new PredictableExecutor(3, Tools.namedThreads("Thread-%d")); |
| 39 | executor = pexecutor; |
| 40 | } |
| 41 | |
| 42 | @After |
| 43 | public void tearDown() { |
| 44 | pexecutor.shutdownNow(); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void test() throws InterruptedException { |
| 49 | CountDownLatch latch = new CountDownLatch(7); |
| 50 | AtomicReference<String> hintValue0 = new AtomicReference<>(""); |
| 51 | AtomicReference<String> hintValue1 = new AtomicReference<>(""); |
| 52 | AtomicReference<String> hintFunction0 = new AtomicReference<>(""); |
| 53 | AtomicReference<String> pickyRunnable0 = new AtomicReference<>(""); |
| 54 | AtomicReference<String> pickyRunnable1 = new AtomicReference<>(""); |
| 55 | AtomicReference<String> pickyCallable0 = new AtomicReference<>(""); |
| 56 | AtomicReference<String> hashCode0 = new AtomicReference<>(""); |
| 57 | |
| 58 | pexecutor.execute(() -> { |
| 59 | hintValue0.set(Thread.currentThread().getName()); |
| 60 | latch.countDown(); |
| 61 | }, 0); |
| 62 | |
| 63 | pexecutor.execute(() -> { |
| 64 | hintValue1.set(Thread.currentThread().getName()); |
| 65 | latch.countDown(); |
| 66 | }, 1); |
| 67 | |
| 68 | pexecutor.execute(() -> { |
| 69 | hintFunction0.set(Thread.currentThread().getName()); |
| 70 | latch.countDown(); |
| 71 | }, (runnable) -> 0); |
| 72 | |
| 73 | pexecutor.execute(new PickyRunnable() { |
| 74 | |
| 75 | @Override |
| 76 | public void run() { |
| 77 | pickyRunnable0.set(Thread.currentThread().getName()); |
| 78 | latch.countDown(); |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | public int hint() { |
| 83 | return 0; |
| 84 | } |
| 85 | }); |
| 86 | |
| 87 | executor.execute(new PickyRunnable() { |
| 88 | |
| 89 | @Override |
| 90 | public void run() { |
| 91 | pickyRunnable1.set(Thread.currentThread().getName()); |
| 92 | latch.countDown(); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int hint() { |
| 97 | return 1; |
| 98 | } |
| 99 | }); |
| 100 | |
| 101 | Callable<Void> callable = new Callable<Void>() { |
| 102 | @Override |
| 103 | public Void call() { |
| 104 | pickyCallable0.set(Thread.currentThread().getName()); |
| 105 | latch.countDown(); |
| 106 | return null; |
| 107 | } |
| 108 | }; |
| 109 | |
| 110 | executor.submit(PredictableExecutor.picky(callable, 0)); |
| 111 | |
| 112 | |
| 113 | executor.execute(new Runnable() { |
| 114 | |
| 115 | @Override |
| 116 | public void run() { |
| 117 | hashCode0.set(Thread.currentThread().getName()); |
| 118 | latch.countDown(); |
| 119 | |
| 120 | } |
| 121 | |
| 122 | @Override |
| 123 | public int hashCode() { |
| 124 | return 0; |
| 125 | } |
| 126 | }); |
| 127 | |
| 128 | latch.await(1, TimeUnit.SECONDS); |
| 129 | |
| 130 | new EqualsTester() |
| 131 | .addEqualityGroup(hintValue0.get(), |
| 132 | hintFunction0.get(), |
| 133 | pickyRunnable0.get(), |
| 134 | pickyCallable0.get(), |
| 135 | hashCode0.get()) |
| 136 | .addEqualityGroup(hintValue1.get(), |
| 137 | pickyRunnable1.get()) |
| 138 | .testEquals(); |
| 139 | } |
| 140 | } |