[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@e162887f28
This commit is contained in:
parent
500b4ac6b9
commit
c73d70491a
|
@ -55,6 +55,8 @@ public class NamedPipeHelperNoBootstrapTests extends LuceneTestCase {
|
||||||
private static final long PIPE_REJECT_REMOTE_CLIENTS = 8;
|
private static final long PIPE_REJECT_REMOTE_CLIENTS = 8;
|
||||||
private static final long NMPWAIT_USE_DEFAULT_WAIT = 0;
|
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);
|
private static final Pointer INVALID_HANDLE_VALUE = Pointer.createConstant(Pointer.SIZE == 8 ? -1 : 0xFFFFFFFFL);
|
||||||
|
|
||||||
static {
|
static {
|
||||||
|
@ -131,8 +133,12 @@ public class NamedPipeHelperNoBootstrapTests extends LuceneTestCase {
|
||||||
|
|
||||||
private static String readLineFromPipeWindows(String pipeName, Pointer handle) throws IOException {
|
private static String readLineFromPipeWindows(String pipeName, Pointer handle) throws IOException {
|
||||||
if (!ConnectNamedPipe(handle, Pointer.NULL)) {
|
if (!ConnectNamedPipe(handle, Pointer.NULL)) {
|
||||||
|
// 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());
|
throw new IOException("ConnectNamedPipe failed for pipe " + pipeName + " with error " + Native.getLastError());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
IntByReference numberOfBytesRead = new IntByReference();
|
IntByReference numberOfBytesRead = new IntByReference();
|
||||||
ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
|
ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
|
||||||
if (!ReadFile(handle, Native.getDirectBufferPointer(buf), new DWord(BUFFER_SIZE), numberOfBytesRead, Pointer.NULL)) {
|
if (!ReadFile(handle, Native.getDirectBufferPointer(buf), new DWord(BUFFER_SIZE), numberOfBytesRead, Pointer.NULL)) {
|
||||||
|
@ -162,8 +168,12 @@ public class NamedPipeHelperNoBootstrapTests extends LuceneTestCase {
|
||||||
|
|
||||||
private static void writeLineToPipeWindows(String pipeName, Pointer handle, String line) throws IOException {
|
private static void writeLineToPipeWindows(String pipeName, Pointer handle, String line) throws IOException {
|
||||||
if (!ConnectNamedPipe(handle, Pointer.NULL)) {
|
if (!ConnectNamedPipe(handle, Pointer.NULL)) {
|
||||||
|
// 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());
|
throw new IOException("ConnectNamedPipe failed for pipe " + pipeName + " with error " + Native.getLastError());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
IntByReference numberOfBytesWritten = new IntByReference();
|
IntByReference numberOfBytesWritten = new IntByReference();
|
||||||
ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
|
ByteBuffer buf = ByteBuffer.allocateDirect(BUFFER_SIZE);
|
||||||
buf.put((line + '\n').getBytes(StandardCharsets.UTF_8));
|
buf.put((line + '\n').getBytes(StandardCharsets.UTF_8));
|
||||||
|
|
Loading…
Reference in New Issue