51f3b4a320426baaffd2eada00b4dd455be49a1b
[policy/clamp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 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.policy.clamp.controlloop.runtime.supervision;
22
23 import static org.assertj.core.api.Assertions.assertThat;
24
25 import org.junit.jupiter.api.Test;
26
27 class HandleCounterTest {
28
29     private static final int ID = 1;
30
31     @Test
32     void testCount() {
33         var handleCounter = new HandleCounter<Integer>();
34         handleCounter.setMaxRetryCount(2);
35         assertThat(handleCounter.count(ID)).isTrue();
36         assertThat(handleCounter.getCounter(ID)).isEqualTo(1);
37         assertThat(handleCounter.count(ID)).isTrue();
38         assertThat(handleCounter.getCounter(ID)).isEqualTo(2);
39         assertThat(handleCounter.count(ID)).isFalse();
40         assertThat(handleCounter.getCounter(ID)).isEqualTo(2);
41
42         handleCounter.clear(ID);
43         assertThat(handleCounter.count(ID)).isTrue();
44         assertThat(handleCounter.getCounter(ID)).isEqualTo(1);
45     }
46
47     @Test
48     void testFault() {
49         var handleCounter = new HandleCounter<Integer>();
50         handleCounter.setFault(ID);
51         assertThat(handleCounter.isFault(ID)).isTrue();
52         handleCounter.clear(ID);
53         assertThat(handleCounter.isFault(ID)).isFalse();
54     }
55
56     @Test
57     void testDuration() throws InterruptedException {
58
59         var handleCounter = new HandleCounter<Integer>() {
60             long epochMilli = 0;
61
62             @Override
63             protected long getEpochMilli() {
64                 return epochMilli;
65             }
66         };
67         handleCounter.epochMilli = 100;
68         var result = handleCounter.getDuration(ID);
69         assertThat(result).isZero();
70
71         handleCounter.epochMilli += 100;
72         result = handleCounter.getDuration(ID);
73         assertThat(result).isEqualTo(100);
74
75         handleCounter.epochMilli += 100;
76         result = handleCounter.getDuration(ID);
77         assertThat(result).isEqualTo(200);
78
79         handleCounter.epochMilli += 100;
80         handleCounter.clear(ID);
81         result = handleCounter.getDuration(ID);
82         assertThat(result).isZero();
83     }
84 }