Permalink
Browse files

Added test for SingleResultCallback

  • Loading branch information...
No3x committed Jan 10, 2019
1 parent 6930aa2 commit 64c6a1eb43ce33f4c4502e782e08b682923e8f59
@@ -0,0 +1,53 @@
package com.cloudcomputing.docker.limits.services.stats;

import com.cloudcomputing.docker.limits.services.util.ContainerIT;
import com.github.dockerjava.api.DockerClient;
import com.github.dockerjava.api.model.Statistics;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.io.IOException;
import java.util.Optional;

import static org.assertj.core.api.Assertions.assertThat;

@RunWith(SpringRunner.class)
@SpringBootTest
public class SingleStatCallbackTest extends ContainerIT {

@Autowired
private DockerClient dockerClient;

private SingleStatCallback singleStatCallback;

@Before
public void setUp() {
super.setUp();
singleStatCallback = new SingleStatCallback();
dockerClient.statsCmd(getContainerId()).exec(singleStatCallback);
}

@Test
public void getLatestStatsWithTimeout() throws InterruptedException, IOException {
final Optional<Statistics> latestStatsWithTimeout = singleStatCallback.getLatestStatsWithTimeout(3);
Boolean gotStats = singleStatCallback.gotStats();
singleStatCallback.close();

assertThat(gotStats).isTrue();
assertThat(latestStatsWithTimeout).isPresent();
}

@Test
public void getNoLatestStatsWithTimeout() throws InterruptedException, IOException {
final Optional<Statistics> latestStatsWithTimeout = singleStatCallback.getLatestStatsWithTimeout(0);
Boolean gotStats = singleStatCallback.gotStats();
singleStatCallback.close();

assertThat(gotStats).isFalse();
assertThat(latestStatsWithTimeout).isNotPresent();
}
}
@@ -17,18 +17,11 @@ public SingleStatCallback() {

final boolean await = countDownLatch.await(timeout, TimeUnit.SECONDS);
if(!await) {
timeout(timeout);
}
if(!gotStats()) {
return noResult();
}
return latestStat();
}

private void timeout(int timeout) {
throw new IllegalStateException("Did not get stats within " + timeout + "s");
}

private Optional<Statistics> noResult() {
return Optional.empty();
}

0 comments on commit 64c6a1e

Please sign in to comment.