Bug 9060: mdsal-trace tooling with getAllUnique() to find Tx leaks
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / test / java / org / opendaylight / controller / md / sal / trace / closetracker / impl / tests / CloseTrackedRegistryTest.java
1 /*
2  * Copyright (c) 2017 Red Hat, Inc. and others. All rights reserved.
3  *
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
7  */
8 package org.opendaylight.controller.md.sal.trace.closetracker.impl.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.fail;
12
13 import java.util.List;
14 import java.util.Map;
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;
19
20 public class CloseTrackedRegistryTest {
21
22     private static class SomethingClosable extends AbstractCloseTracked<SomethingClosable> implements AutoCloseable {
23         SomethingClosable(CloseTrackedRegistry<SomethingClosable> transactionChainRegistry) {
24             super(transactionChainRegistry);
25         }
26
27         @Override
28         public void close() {
29             removeFromTrackedRegistry();
30         }
31     }
32
33     @Test
34     public void testDuplicateAllocationContexts() {
35         final CloseTrackedRegistry<SomethingClosable> registry =
36                 new CloseTrackedRegistry<>(this, "testDuplicateAllocationContexts", true);
37
38         for (int i = 0; i < 100; i++) {
39             SomethingClosable isClosedManyTimes = new SomethingClosable(registry);
40             isClosedManyTimes.close();
41             someOtherMethodWhichDoesNotClose(registry);
42         }
43         @SuppressWarnings({ "resource", "unused" })
44         SomethingClosable forgotToCloseOnce = new SomethingClosable(registry);
45
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"));
55             }
56         });
57         // one of the two has a StackTraceElement containing
58     }
59
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)) {
64                 return;
65             }
66         }
67         fail("Iterable did not contain any element matching predicate");
68     }
69
70     @SuppressWarnings({ "resource", "unused" })
71     private void someOtherMethodWhichDoesNotClose(CloseTrackedRegistry<SomethingClosable> registry) {
72         new SomethingClosable(registry);
73     }
74
75     @Test
76     @SuppressWarnings({ "unused", "resource" })
77     public void testDebugContextDisabled() {
78         final CloseTrackedRegistry<SomethingClosable> debugContextDisabledRegistry =
79                 new CloseTrackedRegistry<>(this, "testDebugContextDisabled", false);
80
81         SomethingClosable forgotToCloseOnce = new SomethingClosable(debugContextDisabledRegistry);
82
83         assertThat(debugContextDisabledRegistry.getAllUnique()).hasSize(1);
84         assertThat(debugContextDisabledRegistry.getAllUnique().values().iterator().next()).isEqualTo(1);
85         assertThat(debugContextDisabledRegistry.getAllUnique().keySet().iterator().next()).isEmpty();
86     }
87 }