Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [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 | |
| 17 | package org.onosproject.store.primitives.resources.impl; |
| 18 | |
| 19 | import static org.junit.Assert.assertArrayEquals; |
| 20 | import static org.junit.Assert.assertEquals; |
| 21 | import static org.junit.Assert.assertFalse; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 22 | import static org.junit.Assert.assertNull; |
| 23 | import static org.junit.Assert.assertTrue; |
| 24 | import static org.junit.Assert.fail; |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 25 | import io.atomix.AtomixClient; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 26 | import io.atomix.resource.ResourceType; |
| 27 | |
| 28 | import java.util.Map; |
| 29 | import java.util.UUID; |
| 30 | import java.util.concurrent.ArrayBlockingQueue; |
| 31 | import java.util.concurrent.BlockingQueue; |
| 32 | |
| 33 | import org.junit.AfterClass; |
| 34 | import org.junit.BeforeClass; |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 35 | import org.junit.Test; |
| 36 | import org.onosproject.store.service.DocumentPath; |
| 37 | import org.onosproject.store.service.DocumentTreeEvent; |
| 38 | import org.onosproject.store.service.DocumentTreeListener; |
| 39 | import org.onosproject.store.service.IllegalDocumentModificationException; |
| 40 | import org.onosproject.store.service.NoSuchDocumentPathException; |
| 41 | import org.onosproject.store.service.Versioned; |
| 42 | |
| 43 | import com.google.common.base.Throwables; |
| 44 | |
| 45 | /** |
| 46 | * Unit tests for {@link AtomixDocumentTree}. |
| 47 | */ |
| 48 | public class AtomixDocumentTreeTest extends AtomixTestBase { |
| 49 | @BeforeClass |
| 50 | public static void preTestSetup() throws Throwable { |
| 51 | createCopycatServers(3); |
| 52 | } |
| 53 | |
| 54 | @AfterClass |
| 55 | public static void postTestCleanup() throws Exception { |
| 56 | clearTests(); |
| 57 | } |
| 58 | @Override |
| 59 | protected ResourceType resourceType() { |
| 60 | return new ResourceType(AtomixDocumentTree.class); |
| 61 | } |
| 62 | /** |
| 63 | * Tests queries (get and getChildren). |
| 64 | */ |
| 65 | @Test |
| 66 | public void testQueries() throws Throwable { |
| 67 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 68 | AtomixDocumentTree.class).join(); |
| 69 | Versioned<byte[]> root = tree.get(DocumentPath.from("root")).join(); |
| 70 | assertEquals(1, root.version()); |
| 71 | assertNull(root.value()); |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Tests create. |
| 76 | */ |
| 77 | @Test |
| 78 | public void testCreate() throws Throwable { |
| 79 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 80 | AtomixDocumentTree.class).join(); |
| 81 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 82 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 83 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 84 | Versioned<byte[]> a = tree.get(DocumentPath.from("root.a")).join(); |
| 85 | assertArrayEquals("a".getBytes(), a.value()); |
| 86 | |
| 87 | Versioned<byte[]> ab = tree.get(DocumentPath.from("root.a.b")).join(); |
| 88 | assertArrayEquals("ab".getBytes(), ab.value()); |
| 89 | |
| 90 | Versioned<byte[]> ac = tree.get(DocumentPath.from("root.a.c")).join(); |
| 91 | assertArrayEquals("ac".getBytes(), ac.value()); |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 92 | |
| 93 | tree.create(DocumentPath.from("root.x"), null).join(); |
| 94 | Versioned<byte[]> x = tree.get(DocumentPath.from("root.x")).join(); |
| 95 | assertNull(x.value()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | /** |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 99 | * Tests recursive create. |
| 100 | */ |
| 101 | @Test |
| 102 | public void testRecursiveCreate() throws Throwable { |
| 103 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 104 | AtomixDocumentTree.class).join(); |
| 105 | tree.createRecursive(DocumentPath.from("root.a.b.c"), "abc".getBytes()).join(); |
| 106 | Versioned<byte[]> a = tree.get(DocumentPath.from("root.a")).join(); |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 107 | assertArrayEquals(null, a.value()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 108 | |
| 109 | Versioned<byte[]> ab = tree.get(DocumentPath.from("root.a.b")).join(); |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 110 | assertArrayEquals(null, ab.value()); |
Madan Jampani | 8698328 | 2016-09-15 14:55:48 -0700 | [diff] [blame] | 111 | |
| 112 | Versioned<byte[]> abc = tree.get(DocumentPath.from("root.a.b.c")).join(); |
| 113 | assertArrayEquals("abc".getBytes(), abc.value()); |
| 114 | } |
| 115 | |
| 116 | /** |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 117 | * Tests set. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testSet() throws Throwable { |
| 121 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 122 | AtomixDocumentTree.class).join(); |
| 123 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 124 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 125 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 126 | |
| 127 | tree.set(DocumentPath.from("root.a.d"), "ad".getBytes()).join(); |
| 128 | Versioned<byte[]> ad = tree.get(DocumentPath.from("root.a.d")).join(); |
| 129 | assertArrayEquals("ad".getBytes(), ad.value()); |
| 130 | |
| 131 | tree.set(DocumentPath.from("root.a"), "newA".getBytes()).join(); |
| 132 | Versioned<byte[]> newA = tree.get(DocumentPath.from("root.a")).join(); |
| 133 | assertArrayEquals("newA".getBytes(), newA.value()); |
| 134 | |
| 135 | tree.set(DocumentPath.from("root.a.b"), "newAB".getBytes()).join(); |
| 136 | Versioned<byte[]> newAB = tree.get(DocumentPath.from("root.a.b")).join(); |
| 137 | assertArrayEquals("newAB".getBytes(), newAB.value()); |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 138 | |
| 139 | tree.set(DocumentPath.from("root.x"), null).join(); |
| 140 | Versioned<byte[]> x = tree.get(DocumentPath.from("root.x")).join(); |
| 141 | assertNull(x.value()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Tests replace if version matches. |
| 146 | */ |
| 147 | @Test |
| 148 | public void testReplaceVersion() throws Throwable { |
| 149 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 150 | AtomixDocumentTree.class).join(); |
| 151 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 152 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 153 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 154 | |
| 155 | Versioned<byte[]> ab = tree.get(DocumentPath.from("root.a.b")).join(); |
| 156 | assertTrue(tree.replace(DocumentPath.from("root.a.b"), "newAB".getBytes(), ab.version()).join()); |
| 157 | Versioned<byte[]> newAB = tree.get(DocumentPath.from("root.a.b")).join(); |
| 158 | assertArrayEquals("newAB".getBytes(), newAB.value()); |
| 159 | |
| 160 | assertFalse(tree.replace(DocumentPath.from("root.a.b"), "newestAB".getBytes(), ab.version()).join()); |
| 161 | assertArrayEquals("newAB".getBytes(), tree.get(DocumentPath.from("root.a.b")).join().value()); |
| 162 | |
| 163 | assertFalse(tree.replace(DocumentPath.from("root.a.d"), "foo".getBytes(), 1).join()); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Tests replace if value matches. |
| 168 | */ |
| 169 | @Test |
| 170 | public void testReplaceValue() throws Throwable { |
| 171 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 172 | AtomixDocumentTree.class).join(); |
| 173 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 174 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 175 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 176 | |
| 177 | Versioned<byte[]> ab = tree.get(DocumentPath.from("root.a.b")).join(); |
| 178 | assertTrue(tree.replace(DocumentPath.from("root.a.b"), "newAB".getBytes(), ab.value()).join()); |
| 179 | Versioned<byte[]> newAB = tree.get(DocumentPath.from("root.a.b")).join(); |
| 180 | assertArrayEquals("newAB".getBytes(), newAB.value()); |
| 181 | |
| 182 | assertFalse(tree.replace(DocumentPath.from("root.a.b"), "newestAB".getBytes(), ab.value()).join()); |
| 183 | assertArrayEquals("newAB".getBytes(), tree.get(DocumentPath.from("root.a.b")).join().value()); |
| 184 | |
| 185 | assertFalse(tree.replace(DocumentPath.from("root.a.d"), "bar".getBytes(), "foo".getBytes()).join()); |
| 186 | } |
| 187 | |
| 188 | /** |
| 189 | * Tests remove. |
| 190 | */ |
| 191 | @Test |
| 192 | public void testRemove() throws Throwable { |
| 193 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 194 | AtomixDocumentTree.class).join(); |
| 195 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 196 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 197 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 198 | |
| 199 | Versioned<byte[]> ab = tree.removeNode(DocumentPath.from("root.a.b")).join(); |
| 200 | assertArrayEquals("ab".getBytes(), ab.value()); |
| 201 | assertNull(tree.get(DocumentPath.from("root.a.b")).join()); |
| 202 | |
| 203 | Versioned<byte[]> ac = tree.removeNode(DocumentPath.from("root.a.c")).join(); |
| 204 | assertArrayEquals("ac".getBytes(), ac.value()); |
| 205 | assertNull(tree.get(DocumentPath.from("root.a.c")).join()); |
| 206 | |
| 207 | Versioned<byte[]> a = tree.removeNode(DocumentPath.from("root.a")).join(); |
| 208 | assertArrayEquals("a".getBytes(), a.value()); |
| 209 | assertNull(tree.get(DocumentPath.from("root.a")).join()); |
Madan Jampani | 4c8e3fe | 2016-09-16 16:20:28 -0700 | [diff] [blame] | 210 | |
| 211 | tree.create(DocumentPath.from("root.x"), null).join(); |
| 212 | Versioned<byte[]> x = tree.removeNode(DocumentPath.from("root.x")).join(); |
| 213 | assertNull(x.value()); |
| 214 | assertNull(tree.get(DocumentPath.from("root.a.x")).join()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Tests invalid removes. |
| 219 | */ |
| 220 | @Test |
| 221 | public void testRemoveFailures() throws Throwable { |
| 222 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 223 | AtomixDocumentTree.class).join(); |
| 224 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 225 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 226 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 227 | |
| 228 | try { |
| 229 | tree.removeNode(DocumentPath.from("root")).join(); |
| 230 | fail(); |
| 231 | } catch (Exception e) { |
| 232 | assertTrue(Throwables.getRootCause(e) instanceof IllegalDocumentModificationException); |
| 233 | } |
| 234 | |
| 235 | try { |
| 236 | tree.removeNode(DocumentPath.from("root.a")).join(); |
| 237 | fail(); |
| 238 | } catch (Exception e) { |
| 239 | assertTrue(Throwables.getRootCause(e) instanceof IllegalDocumentModificationException); |
| 240 | } |
| 241 | |
| 242 | try { |
| 243 | tree.removeNode(DocumentPath.from("root.d")).join(); |
| 244 | fail(); |
| 245 | } catch (Exception e) { |
| 246 | assertTrue(Throwables.getRootCause(e) instanceof NoSuchDocumentPathException); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * Tests invalid create. |
| 252 | */ |
| 253 | @Test |
| 254 | public void testCreateFailures() throws Throwable { |
| 255 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 256 | AtomixDocumentTree.class).join(); |
| 257 | try { |
| 258 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 259 | fail(); |
| 260 | } catch (Exception e) { |
| 261 | assertTrue(Throwables.getRootCause(e) instanceof IllegalDocumentModificationException); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Tests invalid set. |
| 267 | */ |
| 268 | @Test |
| 269 | public void testSetFailures() throws Throwable { |
| 270 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 271 | AtomixDocumentTree.class).join(); |
| 272 | try { |
| 273 | tree.set(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 274 | fail(); |
| 275 | } catch (Exception e) { |
| 276 | assertTrue(Throwables.getRootCause(e) instanceof IllegalDocumentModificationException); |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | /** |
| 281 | * Tests getChildren. |
| 282 | */ |
| 283 | @Test |
| 284 | public void testGetChildren() throws Throwable { |
| 285 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 286 | AtomixDocumentTree.class).join(); |
| 287 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 288 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 289 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 290 | |
| 291 | Map<String, Versioned<byte[]>> rootChildren = tree.getChildren(DocumentPath.from("root")).join(); |
| 292 | assertEquals(1, rootChildren.size()); |
| 293 | Versioned<byte[]> a = rootChildren.get("a"); |
| 294 | assertArrayEquals("a".getBytes(), a.value()); |
| 295 | |
| 296 | Map<String, Versioned<byte[]>> children = tree.getChildren(DocumentPath.from("root.a")).join(); |
| 297 | assertEquals(2, children.size()); |
| 298 | Versioned<byte[]> ab = children.get("b"); |
| 299 | assertArrayEquals("ab".getBytes(), ab.value()); |
| 300 | Versioned<byte[]> ac = children.get("c"); |
| 301 | assertArrayEquals("ac".getBytes(), ac.value()); |
| 302 | |
| 303 | assertEquals(0, tree.getChildren(DocumentPath.from("root.a.b")).join().size()); |
| 304 | assertEquals(0, tree.getChildren(DocumentPath.from("root.a.c")).join().size()); |
| 305 | } |
| 306 | |
| 307 | /** |
| 308 | * Tests destroy. |
| 309 | */ |
| 310 | @Test |
| 311 | public void testClear() { |
| 312 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 313 | AtomixDocumentTree.class).join(); |
| 314 | tree.create(DocumentPath.from("root.a"), "a".getBytes()).join(); |
| 315 | tree.create(DocumentPath.from("root.a.b"), "ab".getBytes()).join(); |
| 316 | tree.create(DocumentPath.from("root.a.c"), "ac".getBytes()).join(); |
| 317 | |
| 318 | tree.destroy().join(); |
| 319 | assertEquals(0, tree.getChildren(DocumentPath.from("root")).join().size()); |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * Tests listeners. |
| 324 | */ |
| 325 | @Test |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 326 | public void testNotifications() throws Exception { |
| 327 | AtomixDocumentTree tree = createAtomixClient().getResource(UUID.randomUUID().toString(), |
| 328 | AtomixDocumentTree.class).join(); |
| 329 | TestEventListener listener = new TestEventListener(); |
| 330 | |
| 331 | // add listener; create a node in the tree and verify an CREATED event is received. |
| 332 | tree.addListener(listener).thenCompose(v -> tree.set(DocumentPath.from("root.a"), "a".getBytes())).join(); |
| 333 | DocumentTreeEvent<byte[]> event = listener.event(); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 334 | assertEquals(DocumentTreeEvent.Type.CREATED, event.type()); |
Madan Jampani | cdbf677 | 2016-09-15 15:41:34 -0700 | [diff] [blame] | 335 | assertFalse(event.oldValue().isPresent()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 336 | assertArrayEquals("a".getBytes(), event.newValue().get().value()); |
Madan Jampani | cdbf677 | 2016-09-15 15:41:34 -0700 | [diff] [blame] | 337 | // update a node in the tree and verify an UPDATED event is received. |
| 338 | tree.set(DocumentPath.from("root.a"), "newA".getBytes()).join(); |
| 339 | event = listener.event(); |
| 340 | assertEquals(DocumentTreeEvent.Type.UPDATED, event.type()); |
| 341 | assertArrayEquals("newA".getBytes(), event.newValue().get().value()); |
| 342 | assertArrayEquals("a".getBytes(), event.oldValue().get().value()); |
| 343 | // remove a node in the tree and verify an REMOVED event is received. |
| 344 | tree.removeNode(DocumentPath.from("root.a")).join(); |
| 345 | event = listener.event(); |
| 346 | assertEquals(DocumentTreeEvent.Type.DELETED, event.type()); |
| 347 | assertFalse(event.newValue().isPresent()); |
| 348 | assertArrayEquals("newA".getBytes(), event.oldValue().get().value()); |
| 349 | // recursively create a node and verify CREATED events for all intermediate nodes. |
| 350 | tree.createRecursive(DocumentPath.from("root.x.y"), "xy".getBytes()).join(); |
| 351 | event = listener.event(); |
| 352 | assertEquals(DocumentTreeEvent.Type.CREATED, event.type()); |
| 353 | assertEquals(DocumentPath.from("root.x"), event.path()); |
| 354 | event = listener.event(); |
| 355 | assertEquals(DocumentTreeEvent.Type.CREATED, event.type()); |
| 356 | assertEquals(DocumentPath.from("root.x.y"), event.path()); |
| 357 | assertArrayEquals("xy".getBytes(), event.newValue().get().value()); |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 360 | @Test |
| 361 | public void testFilteredNotifications() throws Throwable { |
| 362 | AtomixClient client1 = createAtomixClient(); |
| 363 | AtomixClient client2 = createAtomixClient(); |
| 364 | |
| 365 | String treeName = UUID.randomUUID().toString(); |
| 366 | AtomixDocumentTree tree1 = client1.getResource(treeName, AtomixDocumentTree.class).join(); |
| 367 | AtomixDocumentTree tree2 = client2.getResource(treeName, AtomixDocumentTree.class).join(); |
| 368 | |
| 369 | TestEventListener listener1a = new TestEventListener(3); |
| 370 | TestEventListener listener1ab = new TestEventListener(2); |
| 371 | TestEventListener listener2abc = new TestEventListener(1); |
| 372 | |
| 373 | tree1.addListener(DocumentPath.from("root.a"), listener1a).join(); |
| 374 | tree1.addListener(DocumentPath.from("root.a.b"), listener1ab).join(); |
| 375 | tree2.addListener(DocumentPath.from("root.a.b.c"), listener2abc).join(); |
| 376 | |
| 377 | tree1.createRecursive(DocumentPath.from("root.a.b.c"), "abc".getBytes()).join(); |
| 378 | DocumentTreeEvent<byte[]> event = listener1a.event(); |
| 379 | assertEquals(DocumentPath.from("root.a"), event.path()); |
| 380 | event = listener1a.event(); |
| 381 | assertEquals(DocumentPath.from("root.a.b"), event.path()); |
| 382 | event = listener1a.event(); |
| 383 | assertEquals(DocumentPath.from("root.a.b.c"), event.path()); |
| 384 | event = listener1ab.event(); |
| 385 | assertEquals(DocumentPath.from("root.a.b"), event.path()); |
| 386 | event = listener1ab.event(); |
| 387 | assertEquals(DocumentPath.from("root.a.b.c"), event.path()); |
| 388 | event = listener2abc.event(); |
| 389 | assertEquals(DocumentPath.from("root.a.b.c"), event.path()); |
| 390 | } |
| 391 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 392 | private static class TestEventListener implements DocumentTreeListener<byte[]> { |
| 393 | |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 394 | private final BlockingQueue<DocumentTreeEvent<byte[]>> queue; |
| 395 | |
| 396 | public TestEventListener() { |
| 397 | this(1); |
| 398 | } |
| 399 | |
| 400 | public TestEventListener(int maxEvents) { |
| 401 | queue = new ArrayBlockingQueue<>(maxEvents); |
| 402 | } |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 403 | |
| 404 | @Override |
| 405 | public void event(DocumentTreeEvent<byte[]> event) { |
Madan Jampani | 9809422 | 2016-09-15 21:12:46 -0700 | [diff] [blame] | 406 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 407 | try { |
| 408 | queue.put(event); |
| 409 | } catch (InterruptedException e) { |
| 410 | Throwables.propagate(e); |
| 411 | } |
| 412 | } |
| 413 | |
Madan Jampani | 79924fa | 2016-09-13 13:57:03 -0700 | [diff] [blame] | 414 | public DocumentTreeEvent<byte[]> event() throws InterruptedException { |
| 415 | return queue.take(); |
| 416 | } |
| 417 | } |
| 418 | } |