e0bb437a7c37d69b4ee65b94058ef689e6d1d046
[cps.git] /
1 /*
2  *  ============LICENSE_START=======================================================
3  *  Copyright (C) 2024 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the 'License');
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *  Unless required by applicable law or agreed to in writing, software
12  *  distributed under the License is distributed on an 'AS IS' BASIS,
13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *  See the License for the specific language governing permissions and
15  *  limitations under the License.
16  *
17  *  SPDX-License-Identifier: Apache-2.0
18  *  ============LICENSE_END=========================================================
19  */
20
21 package org.onap.cps.integration.functional.ncmp
22
23 import org.onap.cps.integration.base.CpsIntegrationSpecBase
24 import org.onap.cps.ncmp.impl.inventory.sync.ModuleSyncWatchdog
25
26 import java.util.concurrent.Executors
27
28 class ModuleSyncWatchdogIntegrationSpec extends CpsIntegrationSpecBase {
29
30     ModuleSyncWatchdog objectUnderTest
31
32     def executorService = Executors.newFixedThreadPool(2)
33     def SYNC_SAMPLE_SIZE = 100
34
35     def setup() {
36         objectUnderTest = moduleSyncWatchdog
37         registerSequenceOfCmHandlesWithoutWaitForReady(DMI1_URL, NO_MODULE_SET_TAG, SYNC_SAMPLE_SIZE)
38     }
39
40     def cleanup() {
41         try {
42             deregisterSequenceOfCmHandles(DMI1_URL, SYNC_SAMPLE_SIZE)
43             moduleSyncWorkQueue.clear()
44         } finally {
45             executorService.shutdownNow()
46         }
47     }
48
49     def 'Watchdog is disabled for test.'() {
50         when: 'wait a while but less then the initial delay of 10 minutes'
51             Thread.sleep(3000)
52         then: 'the work queue remains empty'
53             assert moduleSyncWorkQueue.isEmpty()
54     }
55
56     def 'Populate module sync work queue simultaneously on two parallel threads (CPS-2403).'() {
57         // This test failed before bug https://lf-onap.atlassian.net/browse/CPS-2403 was fixed
58         given: 'the queue is empty at the start'
59             assert moduleSyncWorkQueue.isEmpty()
60         when: 'attempt to populate the queue on the main (test) and another parallel thread at the same time'
61             objectUnderTest.populateWorkQueueIfNeeded()
62             executorService.execute(populateQueueWithoutDelay)
63         and: 'wait a little (to give all threads time to complete their task)'
64             Thread.sleep(50)
65         then: 'the queue size is exactly the sample size'
66             assert moduleSyncWorkQueue.size() == SYNC_SAMPLE_SIZE
67     }
68
69     def 'Populate module sync work queue on two parallel threads with a slight difference in start time.'() {
70         // This test proved that the issue in CPS-2403 did not arise if the the queue was populated and given time to be distributed
71         given: 'the queue is empty at the start'
72             assert moduleSyncWorkQueue.isEmpty()
73         when: 'attempt to populate the queue on the main (test) and another parallel thread a little later'
74             objectUnderTest.populateWorkQueueIfNeeded()
75             executorService.execute(populateQueueWithDelay)
76         and: 'wait a little (to give all threads time to complete their task)'
77             Thread.sleep(50)
78         then: 'the queue size is exactly the sample size'
79             assert moduleSyncWorkQueue.size() == SYNC_SAMPLE_SIZE
80     }
81
82     def populateQueueWithoutDelay = () -> {
83         try {
84             objectUnderTest.populateWorkQueueIfNeeded()
85         } catch (InterruptedException e) {
86             e.printStackTrace()
87         }
88     }
89
90     def populateQueueWithDelay = () -> {
91         try {
92             Thread.sleep(10)
93             objectUnderTest.populateWorkQueueIfNeeded()
94         } catch (InterruptedException e) {
95             e.printStackTrace()
96         }
97     }
98
99 }