Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api-client/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ dependencies {
implementation("org.apache.commons:commons-compress:1.28.0")
// implementation("com.squareup.okhttp3:logging-interceptor:${libs.versions.okhttpVersionrange.get()}!!${libs.versions.okhttp.get()}")
implementation("de.gesellix:docker-remote-api-model-1-44:2.0.202611282237")
implementation("de.gesellix:docker-filesocket:2.0.202611282235")
implementation("de.gesellix:docker-filesocket:2.0.202612032315")

implementation(libs.slf4j)
testImplementation("ch.qos.logback:logback-classic:${libs.versions.logbackVersionrange.get()}!!${libs.versions.logback.get()}")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package de.gesellix.docker.remote.api.core
import com.squareup.moshi.JsonAdapter
import com.squareup.moshi.Types
import de.gesellix.docker.response.JsonChunksReader
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.runInterruptible
import kotlinx.coroutines.withContext
import okhttp3.Request
import okhttp3.Response
import okhttp3.ResponseBody
Expand Down Expand Up @@ -63,8 +66,13 @@ inline fun <reified T : Any?> ResponseBody?.consumeStream(mediaType: String?): F
"application/jsonl" -> {
val reader = JsonChunksReader<T>(source(), Serializer.moshi)
val events = flow {
while (reader.hasNext()) {
val next = reader.readNext(T::class.java)
// The blocking calls run via runInterruptible on Dispatchers.IO so that cancelling this flow's
// collector (e.g. a withTimeout() or an explicit Cancellable.cancel()) actually interrupts the
// thread blocked in the underlying read, rather than only taking effect at the next suspension
// point -- which never arrives if the read itself never returns. JsonChunksReader.hasNext()
// already checks Thread.currentThread().isInterrupted() for exactly this reason.
while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) {
val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(T::class.java) } }
emit(next)
}
this@consumeStream.closeQuietly()
Expand All @@ -87,8 +95,11 @@ fun Socket?.consumeFrames(mediaType: String?): Flow<Frame> {
ApiClient.DockerRawStreamMediaType -> {
val reader = FrameReader(source, mediaType)
val events = flow {
while (reader.hasNext()) {
val next = reader.readNext(Frame::class.java)
// See the comment in consumeStream(): runInterruptible+Dispatchers.IO makes cancellation
// (withTimeout(), Cancellable.cancel()) actually interrupt the thread blocked in the underlying
// socket read, which FrameReader.hasNext() already anticipates via its interrupted-flag check.
while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) {
val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(Frame::class.java) } }
emit(next)
}
source.closeQuietly()
Expand All @@ -112,8 +123,9 @@ fun ResponseBody?.consumeFrames(mediaType: String?): Flow<Frame> {
ApiClient.DockerRawStreamMediaType -> {
val reader = FrameReader(source(), mediaType)
val events = flow {
while (reader.hasNext()) {
val next = reader.readNext(Frame::class.java)
// See the comment in consumeStream()/Socket.consumeFrames() above.
while (withContext(Dispatchers.IO) { runInterruptible { reader.hasNext() } }) {
val next = withContext(Dispatchers.IO) { runInterruptible { reader.readNext(Frame::class.java) } }
emit(next)
}
this@consumeFrames.closeQuietly()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import de.gesellix.docker.remote.api.ContainerUpdateResponse;
import de.gesellix.docker.remote.api.EngineApiClient;
import de.gesellix.docker.remote.api.ExecConfig;
import de.gesellix.docker.remote.api.ExecInspectResponse;
import de.gesellix.docker.remote.api.ExecStartConfig;
import de.gesellix.docker.remote.api.HostConfig;
import de.gesellix.docker.remote.api.IdResponse;
Expand Down Expand Up @@ -225,6 +226,9 @@ public void containerArchiveInfoGetAndPut() throws IOException {
engineApiClient.getExecApi().execStart(
containerExec.getId(),
new ExecStartConfig(null, null, null));
// execStart doesn't wait for the command to finish: without waiting, the container might be stopped
// before the directory exists, which happens on slow (e.g. Windows CI) machines.
awaitExecFinished(containerExec.getId(), Duration.ofSeconds(30));

// filesystem operations against a running Hyper-V container are not supported,
// so we stop the container before using the archive container api
Expand All @@ -239,6 +243,23 @@ public void containerArchiveInfoGetAndPut() throws IOException {
removeContainer(engineApiClient, "container-archive-info-test");
}

private void awaitExecFinished(String execId, Duration timeout) {
long deadline = System.nanoTime() + timeout.toNanos();
ExecInspectResponse execInspect = engineApiClient.getExecApi().execInspect(execId);
while (Boolean.TRUE.equals(execInspect.getRunning()) && System.nanoTime() < deadline) {
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new IllegalStateException(e);
}
execInspect = engineApiClient.getExecApi().execInspect(execId);
}
assertFalse(Boolean.TRUE.equals(execInspect.getRunning()), "exec still running after " + timeout);
assertEquals(0, execInspect.getExitCode(), "exec failed");
}

@Test
public void containerInspect() {
imageApi.imageCreate(testImage.getImageName(), null, null, testImage.getImageTag(), null, null, null, null, null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ public void systemPingHead() {
@Test
public void systemVersion() {
SystemVersion systemVersion = systemApi.systemVersion();
assertTrue(asList("1.42", "1.43", "1.44", "1.45", "1.46", "1.47", "1.48", "1.49", "1.50", "1.51", "1.52", "1.53", "1.54").contains(systemVersion.getApiVersion()));
// any engine supporting at least our minimum API version, without having to list every newer release
String[] apiVersion = systemVersion.getApiVersion().split("\\.");
assertEquals("1", apiVersion[0], "unexpected API version " + systemVersion.getApiVersion());
assertTrue(Integer.parseInt(apiVersion[1]) >= 42, "unexpected API version " + systemVersion.getApiVersion());
}

static class SystemEventsCallback implements StreamCallback<EventMessage> {
Expand Down
Loading