Modernize TransactionRateLimitingCallbackTest
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionRateLimitingCallbackTest.java
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. 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.cluster.datastore;
9
10 import static org.junit.Assert.assertThrows;
11 import static org.mockito.ArgumentMatchers.any;
12 import static org.mockito.ArgumentMatchers.anyLong;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.never;
15 import static org.mockito.Mockito.verify;
16
17 import com.codahale.metrics.Timer;
18 import com.google.common.base.Ticker;
19 import java.util.concurrent.TimeUnit;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.runner.RunWith;
23 import org.mockito.Mock;
24 import org.mockito.junit.MockitoJUnitRunner;
25 import org.opendaylight.controller.cluster.datastore.utils.ActorUtils;
26
27 /**
28  * Unit tests for TransactionRateLimitingCallback.
29  *
30  * @author Thomas Pantelis
31  */
32 @RunWith(MockitoJUnitRunner.StrictStubs.class)
33 public class TransactionRateLimitingCallbackTest {
34     @Mock
35     ActorUtils mockContext;
36     @Mock
37     Timer mockTimer;
38     @Mock
39     Ticker mockTicker;
40
41     TransactionRateLimitingCallback callback;
42
43     @Before
44     public void setUp() {
45         doReturn(mockTimer).when(mockContext).getOperationTimer(ActorUtils.COMMIT);
46         callback = new TransactionRateLimitingCallback(mockContext);
47         TransactionRateLimitingCallback.setTicker(mockTicker);
48     }
49
50     @Test
51     public void testSuccessWithoutPause() {
52         doReturn(1L).doReturn(201L).when(mockTicker).read();
53
54         callback.run();
55         callback.success();
56
57         verify(mockTimer).update(200L, TimeUnit.NANOSECONDS);
58     }
59
60     @Test
61     public void testSuccessWithPause() {
62         doReturn(1L).doReturn(201L).doReturn(301L).doReturn(351L).when(mockTicker).read();
63
64         callback.run();
65         callback.pause();
66         callback.pause();
67         callback.resume();
68         callback.resume();
69         callback.success();
70
71         verify(mockTimer).update(250L, TimeUnit.NANOSECONDS);
72     }
73
74     @Test
75     public void testFailure() {
76         doReturn(1L).when(mockTicker).read();
77
78         callback.run();
79         callback.failure();
80
81         verify(mockTimer, never()).update(anyLong(), any(TimeUnit.class));
82     }
83
84     @Test
85     public void testSuccessWithoutRun() {
86         final var ex = assertThrows(IllegalStateException.class, callback::success);
87
88         verify(mockTimer, never()).update(anyLong(), any(TimeUnit.class));
89     }
90 }