Skip to content

Commit a028b16

Browse files
committed
fix errors in pull tests will also fix #939
1 parent 8d3c4f8 commit a028b16

File tree

5 files changed

+62
-64
lines changed

5 files changed

+62
-64
lines changed

src/main/java/com/github/dockerjava/core/async/ResultCallbackTemplate.java

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,16 @@
33
*/
44
package com.github.dockerjava.core.async;
55

6+
import com.github.dockerjava.api.async.ResultCallback;
7+
import com.google.common.base.Throwables;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
10+
611
import java.io.Closeable;
712
import java.io.IOException;
813
import java.util.concurrent.CountDownLatch;
914
import java.util.concurrent.TimeUnit;
1015

11-
import javax.annotation.CheckForNull;
12-
13-
import org.slf4j.Logger;
14-
import org.slf4j.LoggerFactory;
15-
16-
import com.github.dockerjava.api.async.ResultCallback;
17-
import com.google.common.base.Throwables;
18-
1916
/**
2017
* Abstract template implementation of {@link ResultCallback}
2118
*
@@ -91,7 +88,7 @@ public void close() throws IOException {
9188
public RC_T awaitCompletion() throws InterruptedException {
9289
completed.await();
9390
// eventually (re)throws RuntimeException
94-
getFirstError();
91+
throwFirstError();
9592
return (RC_T) this;
9693
}
9794

@@ -102,13 +99,14 @@ public RC_T awaitCompletion() throws InterruptedException {
10299
*/
103100
public boolean awaitCompletion(long timeout, TimeUnit timeUnit) throws InterruptedException {
104101
boolean result = completed.await(timeout, timeUnit);
105-
getFirstError();
102+
throwFirstError();
106103
return result;
107104
}
108105

109106
/**
110-
* Blocks until {@link ResultCallback#onStart()} was called. {@link ResultCallback#onStart()} is called when the request was processed
111-
* on the server side and the response is incoming.
107+
* Blocks until {@link ResultCallback#onStart(Closeable)} was called.
108+
* {@link ResultCallback#onStart(Closeable)} is called when the request was processed on the server
109+
* side and the response is incoming.
112110
*/
113111
@SuppressWarnings("unchecked")
114112
public RC_T awaitStarted() throws InterruptedException {
@@ -117,22 +115,25 @@ public RC_T awaitStarted() throws InterruptedException {
117115
}
118116

119117
/**
120-
* Blocks until {@link ResultCallback#onStart()} was called or the given timeout occurs. {@link ResultCallback#onStart()} is called when
121-
* the request was processed on the server side and the response is incoming.
118+
* Blocks until {@link ResultCallback#onStart(Closeable)} was called or the given timeout occurs.
119+
* {@link ResultCallback#onStart(Closeable)} is called when the request was processed on the server side
120+
* and the response is incoming.
122121
* @return {@code true} if started and {@code false} if the waiting time elapsed
123-
* before {@link ResultCallback#onStart()} was called.
122+
* before {@link ResultCallback#onStart(Closeable)} was called.
124123
*/
125124
public boolean awaitStarted(long timeout, TimeUnit timeUnit) throws InterruptedException {
126125
return started.await(timeout, timeUnit);
127126
}
128127

129-
@CheckForNull
130-
protected RuntimeException getFirstError() {
128+
/**
129+
* Throws the first occurred error as a runtime exception
130+
* @throws com.github.dockerjava.api.exception.DockerException The first docker based Error
131+
* @throws RuntimeException on any other occurred error
132+
*/
133+
protected void throwFirstError() {
131134
if (firstError != null) {
132135
// this call throws a RuntimeException
133-
return Throwables.propagate(firstError);
134-
} else {
135-
return null;
136+
Throwables.propagate(firstError);
136137
}
137138
}
138139
}

src/main/java/com/github/dockerjava/core/command/PushImageResultCallback.java

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
*/
44
package com.github.dockerjava.core.command;
55

6-
import javax.annotation.CheckForNull;
7-
8-
import org.slf4j.Logger;
9-
import org.slf4j.LoggerFactory;
10-
116
import com.github.dockerjava.api.exception.DockerClientException;
127
import com.github.dockerjava.api.model.PushResponseItem;
138
import com.github.dockerjava.core.async.ResultCallbackTemplate;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import javax.annotation.CheckForNull;
13+
import java.util.concurrent.TimeUnit;
1414

1515
/**
1616
*
@@ -30,9 +30,21 @@ public void onNext(PushResponseItem item) {
3030
LOGGER.debug(item.toString());
3131
}
3232

33+
@Override
34+
protected void throwFirstError() {
35+
super.throwFirstError();
36+
37+
if (latestItem == null) {
38+
throw new DockerClientException("Could not push image");
39+
} else if (latestItem.isErrorIndicated()) {
40+
throw new DockerClientException("Could not push image: " + latestItem.getError());
41+
}
42+
}
43+
3344
/**
3445
* Awaits the image to be pulled successful.
3546
*
47+
* @deprecated use {@link #awaitCompletion()} or {@link #awaitCompletion(long, TimeUnit)} instead
3648
* @throws DockerClientException
3749
* if the push fails.
3850
*/
@@ -42,11 +54,5 @@ public void awaitSuccess() {
4254
} catch (InterruptedException e) {
4355
throw new DockerClientException("", e);
4456
}
45-
46-
if (latestItem == null) {
47-
throw new DockerClientException("Could not push image");
48-
} else if (latestItem.isErrorIndicated()) {
49-
throw new DockerClientException("Could not push image: " + latestItem.getError());
50-
}
5157
}
5258
}

src/main/java/com/github/dockerjava/netty/InvocationBuilder.java

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,18 @@
11
package com.github.dockerjava.netty;
22

3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import com.github.dockerjava.api.async.ResultCallback;
7+
import com.github.dockerjava.api.exception.DockerClientException;
8+
import com.github.dockerjava.api.model.Frame;
9+
import com.github.dockerjava.core.async.ResultCallbackTemplate;
10+
import com.github.dockerjava.netty.handler.FramedResponseStreamHandler;
11+
import com.github.dockerjava.netty.handler.HttpConnectionHijackHandler;
12+
import com.github.dockerjava.netty.handler.HttpRequestProvider;
13+
import com.github.dockerjava.netty.handler.HttpResponseHandler;
14+
import com.github.dockerjava.netty.handler.HttpResponseStreamHandler;
15+
import com.github.dockerjava.netty.handler.JsonResponseCallbackHandler;
316
import io.netty.buffer.Unpooled;
417
import io.netty.channel.Channel;
518
import io.netty.channel.ChannelFuture;
@@ -29,20 +42,6 @@
2942
import java.util.Map;
3043
import java.util.concurrent.CountDownLatch;
3144

32-
import com.fasterxml.jackson.core.JsonProcessingException;
33-
import com.fasterxml.jackson.core.type.TypeReference;
34-
import com.fasterxml.jackson.databind.ObjectMapper;
35-
import com.github.dockerjava.api.async.ResultCallback;
36-
import com.github.dockerjava.api.exception.DockerClientException;
37-
import com.github.dockerjava.api.model.Frame;
38-
import com.github.dockerjava.core.async.ResultCallbackTemplate;
39-
import com.github.dockerjava.netty.handler.FramedResponseStreamHandler;
40-
import com.github.dockerjava.netty.handler.HttpConnectionHijackHandler;
41-
import com.github.dockerjava.netty.handler.HttpRequestProvider;
42-
import com.github.dockerjava.netty.handler.HttpResponseHandler;
43-
import com.github.dockerjava.netty.handler.HttpResponseStreamHandler;
44-
import com.github.dockerjava.netty.handler.JsonResponseCallbackHandler;
45-
4645
/**
4746
* This class is basically a replacement of javax.ws.rs.client.Invocation.Builder to allow simpler migration of JAX-RS code to a netty based
4847
* implementation.
@@ -122,7 +121,7 @@ public A_RES_T awaitResult() {
122121
} catch (InterruptedException e) {
123122
throw new RuntimeException(e);
124123
}
125-
getFirstError();
124+
throwFirstError();
126125
return result;
127126
}
128127
}

src/test/java/com/github/dockerjava/cmd/AttachContainerCmdIT.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,6 @@ public void onNext(Frame item) {
214214
super.onNext(item);
215215
}
216216

217-
@Override
218-
public RuntimeException getFirstError() {
219-
return super.getFirstError();
220-
}
221-
222217
@Override
223218
public String toString() {
224219
return log.toString();

src/test/java/com/github/dockerjava/cmd/PushImageCmdIT.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import com.github.dockerjava.api.command.CreateContainerResponse;
44
import com.github.dockerjava.api.exception.DockerClientException;
5+
import com.github.dockerjava.api.exception.NotFoundException;
56
import com.github.dockerjava.core.command.PullImageResultCallback;
67
import com.github.dockerjava.core.command.PushImageResultCallback;
78
import com.github.dockerjava.junit.category.AuthIntegration;
9+
import com.github.dockerjava.utils.TestUtils;
810
import org.junit.Before;
911
import org.junit.Rule;
1012
import org.junit.Test;
@@ -45,31 +47,26 @@ public void pushLatest() throws Exception {
4547
String imageId = dockerRule.getClient().commitCmd(container.getId()).withRepository(username + "/busybox").exec();
4648

4749
// we have to block until image is pushed
48-
dockerRule.getClient().pushImageCmd(username + "/busybox").exec(new PushImageResultCallback()).awaitSuccess();
50+
dockerRule.getClient().pushImageCmd(username + "/busybox").exec(new PushImageResultCallback()).awaitCompletion();
4951

5052
LOG.info("Removing image: {}", imageId);
5153
dockerRule.getClient().removeImageCmd(imageId).exec();
5254

53-
dockerRule.getClient().pullImageCmd(username + "/busybox").exec(new PullImageResultCallback()).awaitSuccess();
55+
dockerRule.getClient().pullImageCmd(username + "/busybox").exec(new PullImageResultCallback()).awaitCompletion();
5456
}
5557

5658
@Test
5759
public void pushNonExistentImage() throws Exception {
58-
59-
/*if (getVersion(dockerRule.getClient())
60-
.isGreaterOrEqual(RemoteApiVersion.VERSION_1_26)) {
61-
// no errors??
62-
} else if (getVersion(dockerRule.getClient())
63-
.isGreaterOrEqual(RemoteApiVersion.VERSION_1_24)) {
64-
exception.expect(DockerClientException.class);
65-
} else {
60+
//swarms throws a different error here
61+
if (TestUtils.isSwarm(dockerRule.getClient())) {
6662
exception.expect(NotFoundException.class);
67-
}*/
68-
exception.expect(DockerClientException.class);
63+
} else {
64+
exception.expect(DockerClientException.class);
65+
}
6966

7067
dockerRule.getClient().pushImageCmd(username + "/xxx")
7168
.exec(new PushImageResultCallback())
72-
.awaitSuccess(); // exclude infinite await sleep
69+
.awaitCompletion(); // exclude infinite await sleep
7370

7471
}
7572

0 commit comments

Comments
 (0)