From 5dcccca848d87795d005d5500f1c404f54819f0b Mon Sep 17 00:00:00 2001 From: Robert Muir Date: Tue, 15 Dec 2015 22:59:36 -0500 Subject: [PATCH] add example fixture --- .../jvm-example/example-fixture/build.gradle | 20 ++++++ .../main/java/example/ExampleTestFixture.java | 72 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 plugins/jvm-example/example-fixture/build.gradle create mode 100644 plugins/jvm-example/example-fixture/src/main/java/example/ExampleTestFixture.java diff --git a/plugins/jvm-example/example-fixture/build.gradle b/plugins/jvm-example/example-fixture/build.gradle new file mode 100644 index 00000000000..7761437b8f8 --- /dev/null +++ b/plugins/jvm-example/example-fixture/build.gradle @@ -0,0 +1,20 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +apply plugin: 'java' diff --git a/plugins/jvm-example/example-fixture/src/main/java/example/ExampleTestFixture.java b/plugins/jvm-example/example-fixture/src/main/java/example/ExampleTestFixture.java new file mode 100644 index 00000000000..e69bb97d7ca --- /dev/null +++ b/plugins/jvm-example/example-fixture/src/main/java/example/ExampleTestFixture.java @@ -0,0 +1,72 @@ +/* + * Licensed to Elasticsearch under one or more contributor + * license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright + * ownership. Elasticsearch licenses this file to you under + * the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package example; + +import java.net.Inet6Address; +import java.net.InetAddress; +import java.net.InetSocketAddress; +import java.nio.ByteBuffer; +import java.nio.channels.AsynchronousServerSocketChannel; +import java.nio.channels.AsynchronousSocketChannel; +import java.nio.channels.CompletionHandler; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.StandardCopyOption; +import java.util.Collections; + +/** Crappy example test fixture that responds with TEST and closes the connection */ +public class ExampleTestFixture { + public static void main(String args[]) throws Exception { + if (args.length != 1) { + throw new IllegalArgumentException("ExampleTestFixture "); + } + AsynchronousServerSocketChannel server = AsynchronousServerSocketChannel + .open() + .bind(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + // write port file + Path tmp = Files.createTempFile(null, null); + InetSocketAddress bound = (InetSocketAddress) server.getLocalAddress(); + if (bound.getAddress() instanceof Inet6Address) { + Files.write(tmp, Collections.singleton("[" + bound.getHostString() + "]:" + bound.getPort())); + } else { + Files.write(tmp, Collections.singleton(bound.getHostString() + ":" + bound.getPort())); + } + Files.move(tmp, Paths.get(args[0]), StandardCopyOption.ATOMIC_MOVE); + // go time + server.accept(null, new CompletionHandler() { + @Override + public void completed(AsynchronousSocketChannel socket, Void attachment) { + server.accept(null, this); + try (AsynchronousSocketChannel ch = socket) { + ch.write(ByteBuffer.wrap("TEST\n".getBytes(StandardCharsets.UTF_8))).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public void failed(Throwable exc, Void attachment) {} + }); + // wait forever, until you kill me + Thread.sleep(Long.MAX_VALUE); + } +}