diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2019-09-17 21:24:57 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2019-09-17 21:24:57 +0200 |
commit | 72008d4b095a6c94b42842e888fde0da05a0d81d (patch) | |
tree | 9990193eef44d00ec138049ca38c67ad834222cb /container-search/src | |
parent | 9aef6be6d9e06d6a0c048682d8a59331e8c0b505 (diff) |
More cleanup
Diffstat (limited to 'container-search/src')
-rw-r--r-- | container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/DispatchThread.java | 101 | ||||
-rw-r--r-- | container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/MockFDispatch.java | 212 | ||||
-rw-r--r-- | container-search/src/test/java/com/yahoo/search/query/test/RankFeaturesTestCase.java (renamed from container-search/src/test/java/com/yahoo/fs4/test/RankFeaturesTestCase.java) | 8 |
3 files changed, 6 insertions, 315 deletions
diff --git a/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/DispatchThread.java b/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/DispatchThread.java deleted file mode 100644 index d09e8856ee7..00000000000 --- a/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/DispatchThread.java +++ /dev/null @@ -1,101 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -// -*- mode: java; folded-file: t; c-basic-offset: 4 -*- -// -// -package com.yahoo.prelude.fastsearch.test.fs4mock; - - -import com.yahoo.prelude.ConfigurationException; - - -/** - * Thread-wrapper for MockFDispatch - * - * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a> - */ -public class DispatchThread extends Thread { - int listenPort; - long replyDelay; - long byteDelay; - MockFDispatch dispatch; - Object barrier = new Object(); - - /** - * Instantiate MockFDispatch; if the wanted port is taken we - * bump the port number. Note that the delays are not - * accurate: in reality they will be significantly longer for - * low values. - * - * @param listenPort Wanted port number, note that this may be - * bumped if someone is already running something - * on this port, so it is a starting point for - * scanning only - * @param replyDelay how many milliseconds we should delay when - * replying - * @param byteDelay how many milliseconds we delay for each byte - * written - */ - - public DispatchThread(int listenPort, long replyDelay, long byteDelay) { - this.listenPort = listenPort; - this.replyDelay = replyDelay; - this.byteDelay = byteDelay; - dispatch = new MockFDispatch(listenPort, replyDelay, byteDelay); - dispatch.setBarrier(barrier); - } - - /** - * Run the MockFDispatch and anticipate multiple instances of - * same running. - */ - public void run() { - int maxTries = 20; - // the following section is here to make sure that this - // test is somewhat robust, ie. if someone is already - // listening to the port in question, we'd like to NOT - // fail, but keep probing until we find a port we can use. - boolean up = false; - - while ((!up) && (maxTries-- != 0)) { - try { - dispatch.run(); - up = true; - } catch (ConfigurationException e) { - listenPort++; - dispatch.setListenPort(listenPort); - } - } - } - - /** - * Wait until MockFDispatch is ready to accept connections - * or we time out and indicate which of the two outcomes it was. - * - * @return If we time out we return <code>false</code>. Else we - * return <code>true</code> - * - */ - public boolean waitOnBarrier(long timeout) throws InterruptedException { - long start = System.currentTimeMillis(); - - synchronized (barrier) { - barrier.wait(timeout); - } - long diff = System.currentTimeMillis() - start; - - return (diff < timeout); - } - - /** - * Return the port on which the MockFDispatch actually listens. - * use this instead of assuming where it is since, if more than - * one application tries to use the port we've assigned to it - * we might have to up the port number. - * - * @return port number of active MockFDispatch instance - * - */ - public int listenPort() { - return listenPort; - } -} diff --git a/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/MockFDispatch.java b/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/MockFDispatch.java deleted file mode 100644 index 6956f288d1a..00000000000 --- a/container-search/src/test/java/com/yahoo/prelude/fastsearch/test/fs4mock/MockFDispatch.java +++ /dev/null @@ -1,212 +0,0 @@ -// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.prelude.fastsearch.test.fs4mock; - - -import java.io.IOException; -import java.net.InetAddress; -import java.net.InetSocketAddress; -import java.net.ServerSocket; -import java.nio.ByteBuffer; -import java.nio.channels.ClosedByInterruptException; -import java.nio.channels.ClosedChannelException; -import java.nio.channels.ServerSocketChannel; -import java.nio.channels.SocketChannel; -import java.util.HashSet; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; - -import com.yahoo.prelude.ConfigurationException; -import com.yahoo.prelude.fastsearch.test.DocsumDefinitionTestCase; - - -/** - * A server which replies to any query with the same query result after - * a configurable delay, with a configurable slowness (delay between each byte). - * Connections are never timed out. - * - * @author bratseth - */ -public class MockFDispatch { - - private static int connectionCount = 0; - - private static Logger log = Logger.getLogger(MockFDispatch.class.getName()); - - /** The port we accept incoming requests at */ - private int listenPort = 0; - - private long replyDelay; - - private long byteDelay; - - private Object barrier; - - private static byte[] queryResultPacketData = new byte[] { - 0, 0, 0, 64, 0, 0, - 0, 214 - 256, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, - 25, 0, 0, 0, 111, 0, 0, 0, 97, 0, 0, 0, 3, 0, 0, 0, 23, 0, 0, 0, 7, 0, 0, - 0, 36, 0, 0, 0, 4, 0, 0, 0, 21, 0, 0, 0, 8, 0, 0, 0, 37}; - - private static byte[] docsumData = DocsumDefinitionTestCase.makeDocsum(); - - private static byte[] docsumHeadPacketData = new byte[] { - 0, 0, 3, 39, 0, 0, - 0, 205 - 256, 0, 0, 0, 1, 0, 0, 0, 0}; - - private static byte[] eolPacketData = new byte[] { - 0, 0, 0, 8, 0, 0, 0, - 200 - 256, 0, 0, 0, 1 }; - - private Set<ConnectionThread> connectionThreads = new HashSet<>(); - - public MockFDispatch(int listenPort, long replyDelay, long byteDelay) { - this.replyDelay = replyDelay; - this.byteDelay = byteDelay; - this.listenPort = listenPort; - } - - public void setBarrier(Object barrier) { - this.barrier = barrier; - } - - public void setListenPort(int listenPort) { - this.listenPort = listenPort; - } - - public void run() { - try { - ServerSocketChannel channel = createServerSocket(listenPort); - - channel.socket().setReuseAddress(true); - while (!Thread.currentThread().isInterrupted()) { - try { - // notify those waiting at the barrier that they - // can now proceed and talk to us - synchronized (barrier) { - if (barrier != null) { - barrier.notify(); - } - } - SocketChannel socketChannel = channel.accept(); - - connectionThreads.add(new ConnectionThread(socketChannel)); - } catch (ClosedByInterruptException e) {// We'll exit - } catch (ClosedChannelException e) { - return; - } catch (Exception e) { - log.log(Level.WARNING, "Unexpected error reading request", e); - } - } - channel.close(); - } catch (IOException e) { - throw new ConfigurationException("Socket channel failure", e); - } - } - - private ServerSocketChannel createServerSocket(int listenPort) - throws IOException { - ServerSocketChannel channel = ServerSocketChannel.open(); - ServerSocket socket = channel.socket(); - - socket.bind( - new InetSocketAddress(InetAddress.getLocalHost(), listenPort)); - String host = socket.getInetAddress().getHostName(); - - log.fine("Accepting dfispatch requests at " + host + ":" + listenPort); - return channel; - } - - public static void main(String[] args) { - log.setLevel(Level.FINE); - MockFDispatch m = new MockFDispatch(7890, Integer.parseInt(args[0]), - Integer.parseInt(args[1])); - - m.run(); - } - - private class ConnectionThread extends Thread { - - private ByteBuffer writeBuffer = ByteBuffer.allocate(2000); - - private ByteBuffer readBuffer = ByteBuffer.allocate(2000); - - private int connectionNr = 0; - - private SocketChannel channel; - - public ConnectionThread(SocketChannel channel) { - this.channel = channel; - fillBuffer(writeBuffer); - start(); - } - - private void fillBuffer(ByteBuffer buffer) { - buffer.clear(); - buffer.put(queryResultPacketData); - buffer.put(docsumHeadPacketData); - buffer.put(docsumData); - buffer.put(docsumHeadPacketData); - buffer.put(docsumData); - buffer.put(eolPacketData); - } - - public void run() { - connectionNr = connectionCount++; - log.fine("Opened connection " + connectionNr); - - try { - long lastRequest = System.currentTimeMillis(); - - while ((System.currentTimeMillis() - lastRequest) <= 5000 - && (!isInterrupted())) { - readBuffer.clear(); - channel.read(readBuffer); - lastRequest = System.currentTimeMillis(); - delay(replyDelay); - - if (byteDelay > 0) { - writeSlow(writeBuffer); - } else { - write(writeBuffer); - } - log.fine( - "Replied in " - + (System.currentTimeMillis() - lastRequest) - + " ms"); - } - - log.fine("Closing timed out connection " + connectionNr); - connectionCount--; - channel.close(); - } catch (IOException e) {} - } - - private void write(ByteBuffer writeBuffer) throws IOException { - writeBuffer.flip(); - channel.write(writeBuffer); - } - - private void writeSlow(ByteBuffer writeBuffer) throws IOException { - writeBuffer.flip(); - int dataSize = writeBuffer.limit(); - - for (int i = 0; i < dataSize; i++) { - writeBuffer.position(i); - writeBuffer.limit(i + 1); - channel.write(writeBuffer); - delay(byteDelay); - } - writeBuffer.limit(dataSize); - } - - private void delay(long delay) { - - try { - Thread.sleep(delay); - } catch (InterruptedException e) {} - } - - } - -} diff --git a/container-search/src/test/java/com/yahoo/fs4/test/RankFeaturesTestCase.java b/container-search/src/test/java/com/yahoo/search/query/test/RankFeaturesTestCase.java index b52c708ce4b..8aff81a90db 100644 --- a/container-search/src/test/java/com/yahoo/fs4/test/RankFeaturesTestCase.java +++ b/container-search/src/test/java/com/yahoo/search/query/test/RankFeaturesTestCase.java @@ -1,5 +1,5 @@ // Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.fs4.test; +package com.yahoo.search.query.test; import com.yahoo.io.GrowableByteBuffer; import com.yahoo.search.query.ranking.RankFeatures; @@ -11,7 +11,11 @@ import com.yahoo.text.Utf8; import org.junit.Test; import java.nio.ByteBuffer; -import java.util.*; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Optional; import static org.junit.Assert.assertEquals; |