f7a1e777bec89a9e652065fd9630d64060335a55
[controller.git] / opendaylight / md-sal / mdsal-trace / dom-impl / src / test / java / org / opendaylight / controller / md / sal / trace / 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.tests;
9
10 import static com.google.common.truth.Truth.assertThat;
11 import static org.junit.Assert.fail;
12
13 import java.util.Set;
14 import java.util.function.Predicate;
15 import org.junit.Test;
16 import org.opendaylight.controller.md.sal.trace.closetracker.impl.AbstractCloseTracked;
17 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTrackedRegistry;
18 import org.opendaylight.controller.md.sal.trace.closetracker.impl.CloseTrackedRegistryReportEntry;
19
20 @Deprecated
21 public class CloseTrackedRegistryTest {
22
23     private static class SomethingClosable extends AbstractCloseTracked<SomethingClosable> implements AutoCloseable {
24         SomethingClosable(CloseTrackedRegistry<SomethingClosable> transactionChainRegistry) {
25             super(transactionChainRegistry);
26         }
27
28         @Override
29         public void close() {
30             removeFromTrackedRegistry();
31         }
32     }
33
34     @Test
35     public void testDuplicateAllocationContexts() {
36         final CloseTrackedRegistry<SomethingClosable> registry =
37                 new CloseTrackedRegistry<>(this, "testDuplicateAllocationContexts", true);
38
39         for (int i = 0; i < 100; i++) {
40             SomethingClosable isClosedManyTimes = new SomethingClosable(registry);
41             isClosedManyTimes.close();
42             someOtherMethodWhichDoesNotClose(registry);
43         }
44         @SuppressWarnings({ "resource", "unused" })
45         SomethingClosable forgotToCloseOnce = new SomethingClosable(registry);
46
47         Set<CloseTrackedRegistryReportEntry<SomethingClosable>> uniqueNonClosed = registry.getAllUnique();
48         assertThat(uniqueNonClosed).hasSize(2);
49         assertThatIterableContains(uniqueNonClosed, entry ->
50             entry.getNumberAddedNotRemoved() == 100 || entry.getNumberAddedNotRemoved() == 1);
51         uniqueNonClosed.forEach(entry -> {
52             if (entry.getNumberAddedNotRemoved() == 100) {
53                 assertThatIterableContains(entry.getStackTraceElements(),
54                     element -> element.getMethodName().equals("someOtherMethodWhichDoesNotClose"));
55             } else if (entry.getNumberAddedNotRemoved() == 1) {
56                 assertThatIterableContains(entry.getStackTraceElements(),
57                     element -> element.getMethodName().equals("testDuplicateAllocationContexts"));
58             } else {
59                 fail("Unexpected number of added, not removed: " + entry.getNumberAddedNotRemoved());
60             }
61         });
62     }
63
64     // Something like this really should be in Google Truth...
65     private <T> void assertThatIterableContains(Iterable<T> iterable, Predicate<T> predicate) {
66         for (T element : iterable) {
67             if (predicate.test(element)) {
68                 return;
69             }
70         }
71         fail("Iterable did not contain any element matching predicate");
72     }
73
74     @SuppressWarnings({ "resource", "unused" })
75     private void someOtherMethodWhichDoesNotClose(CloseTrackedRegistry<SomethingClosable> registry) {
76         new SomethingClosable(registry);
77     }
78
79     @Test
80     @SuppressWarnings({ "unused", "resource" })
81     public void testDebugContextDisabled() {
82         final CloseTrackedRegistry<SomethingClosable> debugContextDisabledRegistry =
83                 new CloseTrackedRegistry<>(this, "testDebugContextDisabled", false);
84
85         SomethingClosable forgotToCloseOnce = new SomethingClosable(debugContextDisabledRegistry);
86
87         Set<CloseTrackedRegistryReportEntry<SomethingClosable>>
88             closeRegistryReport = debugContextDisabledRegistry.getAllUnique();
89         assertThat(closeRegistryReport).hasSize(1);
90
91         CloseTrackedRegistryReportEntry<SomethingClosable>
92             closeRegistryReportEntry1 = closeRegistryReport.iterator().next();
93         assertThat(closeRegistryReportEntry1.getNumberAddedNotRemoved()).isEqualTo(1);
94         assertThat(closeRegistryReportEntry1.getStackTraceElements()).isEmpty();
95     }
96 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.