Skip to content

Commit

Permalink
ExecDumpClient should report error when no data is retrieved (#538)
Browse files Browse the repository at this point in the history
  • Loading branch information
marchof authored and Godin committed May 24, 2017
1 parent 938b0b4 commit 10f3ff0
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 4 deletions.
Expand Up @@ -17,6 +17,7 @@
import static org.junit.Assert.fail;

import java.io.IOException;
import java.io.InputStream;
import java.net.ConnectException;
import java.net.InetAddress;
import java.net.ServerSocket;
Expand All @@ -31,7 +32,9 @@
import org.jacoco.core.runtime.RemoteControlWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;

/**
* Unit tests for {@link ExecDumpClient}.
Expand All @@ -46,6 +49,9 @@ public class ExecDumpClientTest {

private ServerSocket server;

@Rule
public ExpectedException exception = ExpectedException.none();

@Before
public void setup() {
callbacks = new ArrayList<String>();
Expand Down Expand Up @@ -101,7 +107,7 @@ public void testWithRetries() throws IOException {
// 3. Retry
"onConnectionFailure", "onConnecting")

, callbacks);
, callbacks);
}

@Test
Expand All @@ -126,6 +132,16 @@ public void testReset() throws IOException {
assertTrue(resetRequested);
}

@Test
public void should_throw_IOException_when_server_closes_connection_without_response()
throws IOException {
exception.expect(IOException.class);
exception.expectMessage("Socket closed unexpectedly.");

int port = createNopServer();
client.dump((String) null, port);
}

private int getFreePort() throws IOException {
final ServerSocket server = new ServerSocket(0, 0,
InetAddress.getByName(null));
Expand Down Expand Up @@ -159,11 +175,40 @@ public void visitDumpCommand(boolean dump, boolean reset)
dumpRequested = dump;
resetRequested = reset;
if (dump) {
writer.visitSessionInfo(new SessionInfo("TestId", 100, 200));
writer.visitSessionInfo(
new SessionInfo("TestId", 100, 200));
}
writer.sendCmdOk();
}
});
reader.read();
}

private int createNopServer() throws IOException {
server = new ServerSocket(0, 0, InetAddress.getByName(null));
new Thread(new Runnable() {
public void run() {
try {
Socket socket = server.accept();
InputStream in = socket.getInputStream();
// Read Header:
in.read();
in.read();
in.read();
in.read();
in.read();
// Read Dump Command:
in.read();
in.read();
in.read();
// Then just close connection without any response:
socket.close();
} catch (IOException e) {
// ignore
}
}
}).start();
return server.getLocalPort();
}

}
Expand Up @@ -123,7 +123,10 @@ public ExecFileLoader dump(final InetAddress address, final int port)
.setExecutionDataVisitor(loader.getExecutionDataStore());

remoteWriter.visitDumpCommand(dump, reset);
remoteReader.read();

if (!remoteReader.read()) {
throw new IOException("Socket closed unexpectedly.");
}

} finally {
socket.close();
Expand Down
7 changes: 7 additions & 0 deletions org.jacoco.doc/docroot/doc/changes.html
Expand Up @@ -43,6 +43,13 @@ <h3>New Features</h3>
(GitHub <a href="https://github.com/jacoco/jacoco/issues/498">#498</a>).</li>
</ul>

<h3>Fixed Bugs</h3>
<ul>
<li><code>dump</code> commands now report error when server unexpectedly
closes connection without sending response
(GitHub <a href="https://github.com/jacoco/jacoco/issues/538">#538</a>).</li>
</ul>

<h3>Non-functional Changes</h3>
<ul>
<li>More information about context is provided when unable to read input during
Expand Down
Expand Up @@ -55,7 +55,9 @@ public static void main(final String[] args) throws IOException {

// Send a dump command and read the response:
writer.visitDumpCommand(true, false);
reader.read();
if (!reader.read()) {
throw new IOException("Socket closed unexpectedly.");
}

socket.close();
localFile.close();
Expand Down

0 comments on commit 10f3ff0

Please sign in to comment.