From c73d70491a912a663e64825d91478aae6ed9512c Mon Sep 17 00:00:00 2001 From: David Roberts Date: Tue, 5 Sep 2017 13:39:22 +0100 Subject: [PATCH] [TEST] Fix error if named pipe already connected (elastic/x-pack-elasticsearch#2423) On Windows a named pipe server must call ConnectNamedPipe() before using a named pipe. However, if the client has already connected then this function returns a failure code, with detailed error code ERROR_PIPE_CONNECTED. The server must check for this, as it means the connection will work fine. The Java test that emulates what the C++ would do in production did not have this logic. This was purely a test problem. The C++ code used in production already does the right thing. relates elastic/x-pack-elasticsearch#2359 Original commit: elastic/x-pack-elasticsearch@e162887f281b74ccd8c20c8a3ccf3675b7857601 --- .../ml/utils/NamedPipeHelperNoBootstrapTests.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/qa/ml-no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java b/qa/ml-no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java index b03c7919314..529f1a07d60 100644 --- a/qa/ml-no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java +++ b/qa/ml-no-bootstrap-tests/src/test/java/org/elasticsearch/xpack/ml/utils/NamedPipeHelperNoBootstrapTests.java @@ -55,6 +55,8 @@ public class NamedPipeHelperNoBootstrapTests extends LuceneTestCase { private static final long PIPE_REJECT_REMOTE_CLIENTS = 8; private static final long NMPWAIT_USE_DEFAULT_WAIT = 0; + private static final int ERROR_PIPE_CONNECTED = 535; + private static final Pointer INVALID_HANDLE_VALUE = Pointer.createConstant(Pointer.SIZE == 8 ? -1 : 0xFFFFFFFFL); static { @@ -131,7 +133,11 @@ public class NamedPipeHelperNoBootstrapTests extends LuceneTestCase { private static String readLineFromPipeWindows(String pipeName, Pointer handle) throws IOException { if (!ConnectNamedPipe(handle, Pointer.NULL)) { - throw new IOException("ConnectNamedPipe failed for pipe " + pipeName + " with error " + Native.getLastError()); + // ERROR_PIPE_CONNECTED means the pipe was already connected so + // there was no need to connect it again - not a problem + if (Native.getLastError() != ERROR_PIPE_CONNECTED) { + throw new IOException("ConnectNamedPipe failed for pipe " + pipeName + " with error " + Native.getLastError()); + } } IntByReference numberOfBytesRead = new IntByReference(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE); @@ -162,7 +168,11 @@ public class NamedPipeHelperNoBootstrapTests extends LuceneTestCase { private static void writeLineToPipeWindows(String pipeName, Pointer handle, String line) throws IOException { if (!ConnectNamedPipe(handle, Pointer.NULL)) { - throw new IOException("ConnectNamedPipe failed for pipe " + pipeName + " with error " + Native.getLastError()); + // ERROR_PIPE_CONNECTED means the pipe was already connected so + // there was no need to connect it again - not a problem + if (Native.getLastError() != ERROR_PIPE_CONNECTED) { + throw new IOException("ConnectNamedPipe failed for pipe " + pipeName + " with error " + Native.getLastError()); + } } IntByReference numberOfBytesWritten = new IntByReference(); ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);