2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2022 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.cps.ncmp.api.inventory.sync
24 import org.onap.cps.ncmp.api.impl.yangmodels.YangModelCmHandle
25 import org.onap.cps.ncmp.api.inventory.sync.executor.AsyncTaskExecutor
26 import java.util.concurrent.ArrayBlockingQueue
27 import java.util.concurrent.BlockingQueue
28 import org.onap.cps.spi.model.DataNode
29 import spock.lang.Specification
31 class ModuleSyncWatchdogSpec extends Specification {
33 def mockSyncUtils = Mock(SyncUtils)
35 def static testQueueCapacity = 50 + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE
37 def moduleSyncWorkQueue = new ArrayBlockingQueue(testQueueCapacity)
39 def moduleSyncStartedOnCmHandles = [:]
41 def mockModuleSyncTasks = Mock(ModuleSyncTasks)
43 def spiedAsyncTaskExecutor = Spy(AsyncTaskExecutor)
45 def objectUnderTest = new ModuleSyncWatchdog(mockSyncUtils, moduleSyncWorkQueue , moduleSyncStartedOnCmHandles,
46 mockModuleSyncTasks, spiedAsyncTaskExecutor)
49 spiedAsyncTaskExecutor.setupThreadPool();
52 def 'Module sync advised cm handles with #scenario.'() {
53 given: 'sync utilities returns #numberOfAdvisedCmHandles advised cm handles'
54 mockSyncUtils.getAdvisedCmHandles() >> createDataNodes(numberOfAdvisedCmHandles)
55 and: 'the executor has enough available threads'
56 spiedAsyncTaskExecutor.getAsyncTaskParallelismLevel() >> 3
57 when: ' module sync is started'
58 objectUnderTest.moduleSyncAdvisedCmHandles()
59 then: 'it performs #expectedNumberOfTaskExecutions tasks'
60 expectedNumberOfTaskExecutions * spiedAsyncTaskExecutor.executeTask(*_)
61 where: ' the following parameter are used'
62 scenario | numberOfAdvisedCmHandles || expectedNumberOfTaskExecutions
63 'less then 1 batch' | 1 || 1
64 'exactly 1 batch' | ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 1
65 '2 batches' | 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 2
66 'queue capacity' | testQueueCapacity || 3
67 'over queue capacity' | testQueueCapacity + 2 * ModuleSyncWatchdog.MODULE_SYNC_BATCH_SIZE || 3
70 def 'Reset failed cm handles.'() {
71 given: 'sync utilities returns failed cm handles'
72 def failedCmHandles = [new YangModelCmHandle()]
73 mockSyncUtils.getModuleSyncFailedCmHandles() >> failedCmHandles
74 when: ' reset failed cm handles is started'
75 objectUnderTest.resetPreviouslyFailedCmHandles()
76 then: 'it is delegated to the module sync task (service)'
77 1 * mockModuleSyncTasks.resetFailedCmHandles(failedCmHandles)
80 def createDataNodes(numberOfDataNodes) {
82 (1..numberOfDataNodes).each {dataNodes.add(new DataNode())}