Add memory usage to integration tests [UPDATED]
[cps.git] / cps-ncmp-service / src / test / groovy / org / onap / cps / ncmp / api / inventory / sync / ModuleSyncWatchdogSpec.groovy
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2022-2023 Nordix Foundation
4  *  Modifications Copyright (C) 2022 Bell Canada
5  *  ================================================================================
6  *  Licensed under the Apache License, Version 2.0 (the "License");
7  *  you may not use this file except in compliance with the License.
8  *  You may obtain a copy of the License at
9  *
10  *        http://www.apache.org/licenses/LICENSE-2.0
11  *
12  *  Unless required by applicable law or agreed to in writing, software
13  *  distributed under the License is distributed on an "AS IS" BASIS,
14  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  *  See the License for the specific language governing permissions and
16  *  limitations under the License.
17  *
18  *  SPDX-License-Identifier: Apache-2.0
19  *  ============LICENSE_END=========================================================
20  */
21
22 package org.onap.cps.ncmp.api.inventory.sync
23
24 import com.hazelcast.map.IMap
25 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncTasks
26 import org.onap.cps.ncmp.api.impl.inventory.sync.ModuleSyncWatchdog
27 import org.onap.cps.ncmp.api.impl.inventory.sync.SyncUtils
28 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
29 import org.onap.cps.ncmp.api.impl.inventory.sync.executor.AsyncTaskExecutor
30 import java.util.concurrent.ArrayBlockingQueue
31 import org.onap.cps.spi.model.DataNode
32 import spock.lang.Specification
33
34 class ModuleSyncWatchdogSpec extends Specification {
35
36     def mockSyncUtils = Mock(SyncUtils)
37
38     def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
39
40     def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
41
42     def mockModuleSyncStartedOnCmHandles = Mock(IMap<String, Object>)
43
44     def mockModuleSyncTasks = Mock(ModuleSyncTasks)
45
46     def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
47
48     def moduleSetTagCache = Mock(IMap<String, Set<String>>)
49
50     def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , mockModuleSyncStartedOnCmHandles, mockModuleSyncTasks, spiedAsyncTaskExecutor, moduleSetTagCache)
51
52     void setup() {
53         spiedAsyncTaskExecutor.setupThreadPool()
54     }
55
56     def 'Module sync advised cm handles with #scenario.'() {
57         given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
58             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
59         and: 'the executor has enough available threads'
60             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
61         when: ' module sync is started'
62             objectUnderTest.moduleSyncAdvisedCmHandles()
63         then: 'it performs #expectedNumberOfTaskExecutions tasks'
64             expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
65         where: 'the following parameter are used'
66             scenario              | numberOfAdvisedCmHandles                                          || expectedNumberOfTaskExecutions
67             'less then 1 batch'   | 1                                                                 || 1
68             'exactly 1 batch'     | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                         || 1
69             '2 batches'           | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE                     || 2
70             'queue capacity'      | testQueueCapacity                                                 || 3
71             'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
72     }
73
74     def 'Module sync advised cm handles starts with no available threads.'() {
75         given: 'sync utilities returns a advise cm handles'
76             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
77         and: 'the executor first has no threads but has one thread on the second attempt'
78             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >>> [ 0, 1 ]
79         when: ' module sync is started'
80             objectUnderTest.moduleSyncAdvisedCmHandles()
81         then: 'it performs one task'
82             1 * spiedAsyncTaskExecutor.executeTask(*_)
83     }
84
85     def 'Module sync advised cm handles already handled.'() {
86         given: 'sync utilities returns a advise cm handles'
87             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(1)
88         and: 'the executor has a thread available'
89             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 1
90         and: 'the semaphore cache indicates the cm handle is already being processed'
91             mockModuleSyncStartedOnCmHandles.putIfAbsent(*_) >> 'Started'
92         when: ' module sync is started'
93             objectUnderTest.moduleSyncAdvisedCmHandles()
94         then: 'it does NOT execute a task to process the (empty) batch'
95             0 * spiedAsyncTaskExecutor.executeTask(*_)
96     }
97
98     def 'Module sync with previous cm handle(s) left in work queue.'() {
99         given: 'there is still a cm handle in the queue'
100             moduleSyncWorkQueue.offer(new DataNode())
101         and: 'sync utilities returns many advise cm handles'
102             mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(500)
103         and: 'the executor has plenty threads available'
104             spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 10
105         when: ' module sync is started'
106             objectUnderTest.moduleSyncAdvisedCmHandles()
107         then: 'it does executes only one task to process the remaining handle in the queue'
108             1 * spiedAsyncTaskExecutor.executeTask(*_)
109     }
110
111     def 'Reset failed cm handles.'() {
112         given: 'sync utilities returns failed cm handles'
113             def failedCmHandles = [new YangModelCmHandle()]
114             mockSyncUtils.getModuleSyncFailedCmHandles() >> failedCmHandles
115         when: 'reset failed cm handles is started'
116             objectUnderTest.resetPreviouslyFailedCmHandles()
117         then: 'it is delegated to the module sync task (service)'
118             1 * mockModuleSyncTasks.resetFailedCmHandles(failedCmHandles)
119     }
120
121     def createDataNodes(numberOfDataNodes) {
122         def dataNodes = []
123         (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}
124         return dataNodes
125     }
126 }