2 * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.md.sal.trace.closetracker.impl.tests;
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.fail;
13 import java.util.List;
15 import java.util.function.Predicate;
16 import org.junit.Test;
17 import org.opendaylight.controller.md.sal.trace.closetracker.impl.AbstractCloseTracked;
18 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTrackedRegistry;
20 public class CloseTrackedRegistryTest {
22 private static class SomethingClosable extends AbstractCloseTracked<SomethingClosable> implements AutoCloseable {
23 SomethingClosable(CloseTrackedRegistry<SomethingClosable> transactionChainRegistry) {
24 super(transactionChainRegistry);
29 removeFromTrackedRegistry();
34 public void testDuplicateAllocationContexts() {
35 final CloseTrackedRegistry<SomethingClosable> registry =
36 new CloseTrackedRegistry<>(this, "testDuplicateAllocationContexts", true);
38 for (int i = 0; i < 100; i++) {
39 SomethingClosable isClosedManyTimes = new SomethingClosable(registry);
40 isClosedManyTimes.close();
41 someOtherMethodWhichDoesNotClose(registry);
43 @SuppressWarnings({ "resource", "unused" })
44 SomethingClosable forgotToCloseOnce = new SomethingClosable(registry);
46 Map<List<StackTraceElement>, Long> uniqueNonClosed = registry.getAllUnique();
47 assertThat(uniqueNonClosed.values()).containsExactly(100L, 1L);
48 uniqueNonClosed.forEach((stackTraceElements, occurrences) -> {
49 if (occurrences == 100) {
50 assertThatIterableContains(stackTraceElements,
51 element -> element.getMethodName().equals("someOtherMethodWhichDoesNotClose"));
52 } else { // occurrences == 1
53 assertThatIterableContains(stackTraceElements,
54 element -> element.getMethodName().equals("testDuplicateAllocationContexts"));
57 // one of the two has a StackTraceElement containing
60 // Something like this really should be in Google Truth...
61 private <T> void assertThatIterableContains(Iterable<T> iterable, Predicate<T> predicate) {
62 for (T element : iterable) {
63 if (predicate.test(element)) {
67 fail("Iterable did not contain any element matching predicate");
70 @SuppressWarnings({ "resource", "unused" })
71 private void someOtherMethodWhichDoesNotClose(CloseTrackedRegistry<SomethingClosable> registry) {
72 new SomethingClosable(registry);
76 @SuppressWarnings({ "unused", "resource" })
77 public void testDebugContextDisabled() {
78 final CloseTrackedRegistry<SomethingClosable> debugContextDisabledRegistry =
79 new CloseTrackedRegistry<>(this, "testDebugContextDisabled", false);
81 SomethingClosable forgotToCloseOnce = new SomethingClosable(debugContextDisabledRegistry);
83 assertThat(debugContextDisabledRegistry.getAllUnique()).hasSize(1);
84 assertThat(debugContextDisabledRegistry.getAllUnique().values().iterator().next()).isEqualTo(1);
85 assertThat(debugContextDisabledRegistry.getAllUnique().keySet().iterator().next()).isEmpty();