c91080ceef0a2e14cbe1dc0708102519640b3327
[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 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         Set<CloseTrackedRegistryReportEntry<SomethingClosable>> uniqueNonClosed = registry.getAllUnique();
47         assertThat(uniqueNonClosed).hasSize(2);
48         assertThatIterableContains(uniqueNonClosed, entry ->
49             entry.getNumberAddedNotRemoved() == 100 || entry.getNumberAddedNotRemoved() == 1);
50         uniqueNonClosed.forEach(entry -> {
51             if (entry.getNumberAddedNotRemoved() == 100) {
52                 assertThatIterableContains(entry.getStackTraceElements(),
53                     element -> element.getMethodName().equals("someOtherMethodWhichDoesNotClose"));
54             } else if (entry.getNumberAddedNotRemoved() == 1) {
55                 assertThatIterableContains(entry.getStackTraceElements(),
56                     element -> element.getMethodName().equals("testDuplicateAllocationContexts"));
57             } else {
58                 fail("Unexpected number of added, not removed: " + entry.getNumberAddedNotRemoved());
59             }
60         });
61     }
62
63     // Something like this really should be in Google Truth...
64     private <T> void assertThatIterableContains(Iterable<T> iterable, Predicate<T> predicate) {
65         for (T element : iterable) {
66             if (predicate.test(element)) {
67                 return;
68             }
69         }
70         fail("Iterable did not contain any element matching predicate");
71     }
72
73     @SuppressWarnings({ "resource", "unused" })
74     private void someOtherMethodWhichDoesNotClose(CloseTrackedRegistry<SomethingClosable> registry) {
75         new SomethingClosable(registry);
76     }
77
78     @Test
79     @SuppressWarnings({ "unused", "resource" })
80     public void testDebugContextDisabled() {
81         final CloseTrackedRegistry<SomethingClosable> debugContextDisabledRegistry =
82                 new CloseTrackedRegistry<>(this, "testDebugContextDisabled", false);
83
84         SomethingClosable forgotToCloseOnce = new SomethingClosable(debugContextDisabledRegistry);
85
86         Set<CloseTrackedRegistryReportEntry<SomethingClosable>>
87             closeRegistryReport = debugContextDisabledRegistry.getAllUnique();
88         assertThat(closeRegistryReport).hasSize(1);
89
90         CloseTrackedRegistryReportEntry<SomethingClosable>
91             closeRegistryReportEntry1 = closeRegistryReport.iterator().next();
92         assertThat(closeRegistryReportEntry1.getNumberAddedNotRemoved()).isEqualTo(1);
93         assertThat(closeRegistryReportEntry1.getStackTraceElements()).isEmpty();
94     }
95 }

©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.