Skip to content

Commit

Permalink
Merge pull request #1467 from wind57/some-clean-ups
Browse files Browse the repository at this point in the history
clean-up, fix a bug, tests
  • Loading branch information
k8s-ci-robot committed Jan 4, 2021
2 parents a8e52f7 + e63a256 commit 5c35615
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,30 +18,26 @@
import org.slf4j.LoggerFactory;

/**
* ResyncRunnable class implements Runnable interface. It calls the resync function of Store
* interface which is actually always implemented by DeltaFIFO.
* ResyncRunnable class implements Runnable interface. It calls the resync function of {@link
* DeltaFIFO}
*/
public class ResyncRunnable implements Runnable {

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

private DeltaFIFO store;
private Supplier<Boolean> shouldResyncFunc;
private final DeltaFIFO store;
private final Supplier<Boolean> shouldResyncFunc;

public ResyncRunnable(DeltaFIFO store, Supplier<Boolean> shouldResyncFunc) {
this.store = store;
this.shouldResyncFunc = shouldResyncFunc;
}

public void run() {
if (log.isDebugEnabled()) {
log.debug("ResyncRunnable#resync ticker tick");
}
log.debug("ResyncRunnable#resync ticker tick");

if (shouldResyncFunc == null || shouldResyncFunc.get()) {
if (log.isDebugEnabled()) {
log.debug("ResyncRunnable#force resync");
}
if (shouldResyncFunc == null || (shouldResyncFunc.get() != null && shouldResyncFunc.get())) {
log.debug("ResyncRunnable#force resync");
this.store.resync();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
Copyright 2021 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package io.kubernetes.client.informer;

import static org.mockito.Mockito.verify;

import io.kubernetes.client.informer.cache.DeltaFIFO;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;

/** @author wind57 */
@RunWith(MockitoJUnitRunner.class)
public class ResyncRunnableTest {

@Mock private DeltaFIFO deltaFIFO;

@Test
public void testNullSupplier() {
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, null);
underTest.run();
verify(deltaFIFO, Mockito.times(1)).resync();
}

@Test
public void testSupplierReturnsFalse() {
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, () -> false);
underTest.run();
verify(deltaFIFO, Mockito.never()).resync();
}

@Test
public void testSupplierReturnsTrue() {
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, () -> true);
underTest.run();
verify(deltaFIFO, Mockito.times(1)).resync();
}

// "() -> null" is going to be treated as false
@Test
public void testSupplierReturnsNull() {
ResyncRunnable underTest = new ResyncRunnable(deltaFIFO, () -> null);
underTest.run();
verify(deltaFIFO, Mockito.never()).resync();
}
}

0 comments on commit 5c35615

Please sign in to comment.