Skip to content

Commit 5c35615

Browse files
authored
Merge pull request #1467 from wind57/some-clean-ups
clean-up, fix a bug, tests
2 parents a8e52f7 + e63a256 commit 5c35615

File tree

2 files changed

+65
-11
lines changed

2 files changed

+65
-11
lines changed

util/src/main/java/io/kubernetes/client/informer/ResyncRunnable.java

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,26 @@
1818
import org.slf4j.LoggerFactory;
1919

2020
/**
21-
* ResyncRunnable class implements Runnable interface. It calls the resync function of Store
22-
* interface which is actually always implemented by DeltaFIFO.
21+
* ResyncRunnable class implements Runnable interface. It calls the resync function of {@link
22+
* DeltaFIFO}
2323
*/
2424
public class ResyncRunnable implements Runnable {
2525

2626
private static final Logger log = LoggerFactory.getLogger(ResyncRunnable.class);
2727

28-
private DeltaFIFO store;
29-
private Supplier<Boolean> shouldResyncFunc;
28+
private final DeltaFIFO store;
29+
private final Supplier<Boolean> shouldResyncFunc;
3030

3131
public ResyncRunnable(DeltaFIFO store, Supplier<Boolean> shouldResyncFunc) {
3232
this.store = store;
3333
this.shouldResyncFunc = shouldResyncFunc;
3434
}
3535

3636
public void run() {
37-
if (log.isDebugEnabled()) {
38-
log.debug("ResyncRunnable#resync ticker tick");
39-
}
37+
log.debug("ResyncRunnable#resync ticker tick");
4038

41-
if (shouldResyncFunc == null || shouldResyncFunc.get()) {
42-
if (log.isDebugEnabled()) {
43-
log.debug("ResyncRunnable#force resync");
44-
}
39+
if (shouldResyncFunc == null || (shouldResyncFunc.get() != null && shouldResyncFunc.get())) {
40+
log.debug("ResyncRunnable#force resync");
4541
this.store.resync();
4642
}
4743
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
Copyright 2021 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
package io.kubernetes.client.informer;
14+
15+
import static org.mockito.Mockito.verify;
16+
17+
import io.kubernetes.client.informer.cache.DeltaFIFO;
18+
import org.junit.Test;
19+
import org.junit.runner.RunWith;
20+
import org.mockito.Mock;
21+
import org.mockito.Mockito;
22+
import org.mockito.junit.MockitoJUnitRunner;
23+
24+
/** @author wind57 */
25+
@RunWith(MockitoJUnitRunner.class)
26+
public class ResyncRunnableTest {
27+
28+
@Mock private DeltaFIFO deltaFIFO;
29+
30+
@Test
31+
public void testNullSupplier() {
32+
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, null);
33+
underTest.run();
34+
verify(deltaFIFO, Mockito.times(1)).resync();
35+
}
36+
37+
@Test
38+
public void testSupplierReturnsFalse() {
39+
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, () -> false);
40+
underTest.run();
41+
verify(deltaFIFO, Mockito.never()).resync();
42+
}
43+
44+
@Test
45+
public void testSupplierReturnsTrue() {
46+
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, () -> true);
47+
underTest.run();
48+
verify(deltaFIFO, Mockito.times(1)).resync();
49+
}
50+
51+
// "() -> null" is going to be treated as false
52+
@Test
53+
public void testSupplierReturnsNull() {
54+
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, () -> null);
55+
underTest.run();
56+
verify(deltaFIFO, Mockito.never()).resync();
57+
}
58+
}

0 commit comments

Comments
 (0)