Fix memory leak in BA mount service
[controller.git] / opendaylight / md-sal / sal-binding-broker / src / test / java / org / opendaylight / controller / md / sal / binding / impl / BindingDOMMountPointServiceAdapterTest.java
1 /*
2  * Copyright (c) 2019 FRINX 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.binding.impl;
9
10 import static org.mockito.Matchers.any;
11
12 import com.google.common.base.Optional;
13 import com.google.common.cache.LoadingCache;
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mock;
17 import org.mockito.Mockito;
18 import org.mockito.MockitoAnnotations;
19 import org.opendaylight.controller.md.sal.dom.api.DOMMountPoint;
20 import org.opendaylight.controller.md.sal.dom.api.DOMMountPointService;
21 import org.opendaylight.mdsal.binding.dom.codec.impl.BindingNormalizedNodeCodecRegistry;
22 import org.opendaylight.mdsal.binding.generator.api.ClassLoadingStrategy;
23 import org.opendaylight.yangtools.yang.binding.DataObject;
24 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
25 import org.opendaylight.yangtools.yang.common.QName;
26 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
27
28 public class BindingDOMMountPointServiceAdapterTest {
29
30     @Mock
31     private DOMMountPointService mountService;
32     // Use a real instance of codec, since its getCodecRegistry() method is final and cannot be mocked
33     private BindingToNormalizedNodeCodec codec;
34     @Mock
35     private BindingNormalizedNodeCodecRegistry codecRegistry;
36     @Mock
37     private ClassLoadingStrategy classLoadingStrategy;
38
39     @Before
40     public void setUp() {
41         MockitoAnnotations.initMocks(this);
42         codec = Mockito.spy(new BindingToNormalizedNodeCodec(classLoadingStrategy, codecRegistry));
43         Mockito.doAnswer(invocationOnMock -> Optional.of(Mockito.mock(DOMMountPoint.class)))
44                 .when(mountService).getMountPoint(any(YangInstanceIdentifier.class));
45         Mockito.doReturn(YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(QName.create("(a)b"))))
46                 .when(codec).toYangInstanceIdentifierBlocking(any(InstanceIdentifier.class));
47         Mockito.doReturn(InstanceIdentifier.create(DataObject.class))
48                 .when(codecRegistry).fromYangInstanceIdentifier(any(YangInstanceIdentifier.class));
49     }
50
51     @Test(timeout = 30 * 1000)
52     public void testCaching() throws Exception {
53         BindingDOMMountPointServiceAdapter baService = new BindingDOMMountPointServiceAdapter(mountService, codec);
54         LoadingCache<DOMMountPoint, BindingMountPointAdapter> cache = baService.bindingMountpoints;
55
56         baService.getMountPoint(InstanceIdentifier.create(DataObject.class));
57
58         while (true) {
59             cache.cleanUp();
60             System.gc();
61             Thread.sleep(100);
62             if (cache.asMap().keySet().size() == 0) {
63                 // Cache has been cleared, the single cache entry was garbage collected
64                 return;
65             }
66         }
67     }
68 }