Brian O'Connor | c6713a8 | 2015-02-24 11:55:48 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2015 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 | package org.onlab.util; |
| 17 | |
| 18 | import org.apache.commons.lang.mutable.MutableBoolean; |
| 19 | import org.junit.Ignore; |
| 20 | import org.junit.Test; |
| 21 | |
| 22 | import java.util.concurrent.CountDownLatch; |
| 23 | import java.util.concurrent.ExecutorService; |
| 24 | import java.util.concurrent.Executors; |
| 25 | import java.util.concurrent.TimeUnit; |
| 26 | |
| 27 | import static org.junit.Assert.*; |
| 28 | |
| 29 | /** |
| 30 | * Tests of the BlockingBoolean utility. |
| 31 | */ |
| 32 | public class BlockingBooleanTest { |
| 33 | |
| 34 | @Test |
| 35 | public void basics() { |
| 36 | BlockingBoolean b = new BlockingBoolean(false); |
| 37 | assertEquals(false, b.get()); |
| 38 | b.set(true); |
| 39 | assertEquals(true, b.get()); |
| 40 | b.set(true); |
| 41 | assertEquals(true, b.get()); |
| 42 | b.set(false); |
| 43 | assertEquals(false, b.get()); |
| 44 | } |
| 45 | |
| 46 | private void waitChange(boolean value, int numThreads) { |
| 47 | BlockingBoolean b = new BlockingBoolean(!value); |
| 48 | |
| 49 | CountDownLatch latch = new CountDownLatch(numThreads); |
| 50 | ExecutorService exec = Executors.newFixedThreadPool(numThreads); |
| 51 | for (int i = 0; i < numThreads; i++) { |
| 52 | exec.submit(() -> { |
| 53 | try { |
| 54 | b.await(value); |
| 55 | latch.countDown(); |
| 56 | } catch (InterruptedException e) { |
| 57 | fail(); |
| 58 | } |
| 59 | }); |
| 60 | } |
| 61 | b.set(value); |
| 62 | try { |
| 63 | assertTrue(latch.await(10, TimeUnit.MILLISECONDS)); |
| 64 | } catch (InterruptedException e) { |
| 65 | fail(); |
| 66 | } |
| 67 | exec.shutdown(); |
| 68 | } |
| 69 | |
| 70 | @Test |
| 71 | public void waitTrueChange() { |
| 72 | waitChange(true, 4); |
| 73 | } |
| 74 | |
| 75 | @Test |
| 76 | public void waitFalseChange() { |
| 77 | waitChange(false, 4); |
| 78 | } |
| 79 | |
| 80 | @Test |
| 81 | public void waitSame() { |
| 82 | BlockingBoolean b = new BlockingBoolean(true); |
| 83 | |
| 84 | CountDownLatch latch = new CountDownLatch(1); |
| 85 | ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 86 | exec.submit(() -> { |
| 87 | try { |
| 88 | b.await(true); |
| 89 | latch.countDown(); |
| 90 | } catch (InterruptedException e) { |
| 91 | fail(); |
| 92 | } |
| 93 | }); |
| 94 | try { |
| 95 | assertTrue(latch.await(10, TimeUnit.MILLISECONDS)); |
| 96 | } catch (InterruptedException e) { |
| 97 | fail(); |
| 98 | } |
| 99 | exec.shutdown(); |
| 100 | } |
| 101 | |
| 102 | @Test |
| 103 | public void someWait() { |
| 104 | BlockingBoolean b = new BlockingBoolean(false); |
| 105 | |
| 106 | int numThreads = 4; |
| 107 | CountDownLatch sameLatch = new CountDownLatch(numThreads / 2); |
| 108 | CountDownLatch waitLatch = new CountDownLatch(numThreads / 2); |
| 109 | |
| 110 | ExecutorService exec = Executors.newFixedThreadPool(numThreads); |
| 111 | for (int i = 0; i < numThreads; i++) { |
| 112 | final boolean value = (i % 2 == 1); |
| 113 | exec.submit(() -> { |
| 114 | try { |
| 115 | b.await(value); |
| 116 | if (value) { |
| 117 | waitLatch.countDown(); |
| 118 | } else { |
| 119 | sameLatch.countDown(); |
| 120 | } |
| 121 | } catch (InterruptedException e) { |
| 122 | fail(); |
| 123 | } |
| 124 | }); |
| 125 | } |
| 126 | try { |
| 127 | assertTrue(sameLatch.await(10, TimeUnit.MILLISECONDS)); |
| 128 | assertEquals(waitLatch.getCount(), numThreads / 2); |
| 129 | } catch (InterruptedException e) { |
| 130 | fail(); |
| 131 | } |
| 132 | b.set(true); |
| 133 | try { |
| 134 | assertTrue(waitLatch.await(10, TimeUnit.MILLISECONDS)); |
| 135 | } catch (InterruptedException e) { |
| 136 | fail(); |
| 137 | } |
| 138 | exec.shutdown(); |
| 139 | } |
| 140 | |
| 141 | @Test |
| 142 | public void waitTimeout() { |
| 143 | BlockingBoolean b = new BlockingBoolean(true); |
| 144 | |
| 145 | CountDownLatch latch = new CountDownLatch(1); |
| 146 | ExecutorService exec = Executors.newSingleThreadExecutor(); |
| 147 | exec.submit(() -> { |
| 148 | try { |
| 149 | if (!b.await(false, 1, TimeUnit.NANOSECONDS)) { |
| 150 | latch.countDown(); |
| 151 | } else { |
| 152 | fail(); |
| 153 | } |
| 154 | } catch (InterruptedException e) { |
| 155 | fail(); |
| 156 | } |
| 157 | }); |
| 158 | try { |
| 159 | assertTrue(latch.await(10, TimeUnit.MILLISECONDS)); |
| 160 | } catch (InterruptedException e) { |
| 161 | fail(); |
| 162 | } |
| 163 | exec.shutdown(); |
| 164 | |
| 165 | } |
| 166 | |
| 167 | @Test |
| 168 | @Ignore |
| 169 | public void samePerf() { |
| 170 | int iters = 10_000; |
| 171 | |
| 172 | BlockingBoolean b1 = new BlockingBoolean(false); |
| 173 | long t1 = System.nanoTime(); |
| 174 | for (int i = 0; i < iters; i++) { |
| 175 | b1.set(false); |
| 176 | } |
| 177 | long t2 = System.nanoTime(); |
| 178 | MutableBoolean b2 = new MutableBoolean(false); |
| 179 | for (int i = 0; i < iters; i++) { |
| 180 | b2.setValue(false); |
| 181 | } |
| 182 | long t3 = System.nanoTime(); |
| 183 | System.out.println((t2 - t1) + " " + (t3 - t2) + " " + ((t2 - t1) <= (t3 - t2))); |
| 184 | } |
| 185 | |
| 186 | @Test |
| 187 | @Ignore |
| 188 | public void changePerf() { |
| 189 | int iters = 10_000; |
| 190 | |
| 191 | BlockingBoolean b1 = new BlockingBoolean(false); |
| 192 | boolean v = true; |
| 193 | long t1 = System.nanoTime(); |
| 194 | for (int i = 0; i < iters; i++) { |
| 195 | b1.set(v); |
| 196 | v = !v; |
| 197 | } |
| 198 | long t2 = System.nanoTime(); |
| 199 | MutableBoolean b2 = new MutableBoolean(false); |
| 200 | for (int i = 0; i < iters; i++) { |
| 201 | b2.setValue(v); |
| 202 | v = !v; |
| 203 | } |
| 204 | long t3 = System.nanoTime(); |
| 205 | System.out.println((t2 - t1) + " " + (t3 - t2) + " " + ((t2 - t1) <= (t3 - t2))); |
| 206 | } |
| 207 | |
| 208 | } |