From 234d3e224b70900284e980db469ebfcffd4346ed Mon Sep 17 00:00:00 2001 From: Joakim Erdfelt Date: Thu, 28 Feb 2019 08:47:16 -0500 Subject: [PATCH] Adding missing ClientWriteThread Signed-off-by: Joakim Erdfelt --- .../tests/client/ClientWriteThread.java | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientWriteThread.java diff --git a/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientWriteThread.java b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientWriteThread.java new file mode 100644 index 00000000000..105df992917 --- /dev/null +++ b/jetty-websocket/jetty-websocket-tests/src/test/java/org/eclipse/jetty/websocket/tests/client/ClientWriteThread.java @@ -0,0 +1,107 @@ +// +// ======================================================================== +// Copyright (c) 1995-2019 Mort Bay Consulting Pty. Ltd. +// ------------------------------------------------------------------------ +// All rights reserved. This program and the accompanying materials +// are made available under the terms of the Eclipse Public License v1.0 +// and Apache License v2.0 which accompanies this distribution. +// +// The Eclipse Public License is available at +// http://www.eclipse.org/legal/epl-v10.html +// +// The Apache License v2.0 is available at +// http://www.opensource.org/licenses/apache2.0.php +// +// You may elect to redistribute this code under either of these licenses. +// ======================================================================== +// + +package org.eclipse.jetty.websocket.tests.client; + +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; + +import org.eclipse.jetty.util.log.Log; +import org.eclipse.jetty.util.log.Logger; +import org.eclipse.jetty.websocket.api.BatchMode; +import org.eclipse.jetty.websocket.api.RemoteEndpoint; +import org.eclipse.jetty.websocket.api.Session; + +public class ClientWriteThread extends Thread +{ + private static final Logger LOG = Log.getLogger(ClientWriteThread.class); + private final Session session; + private int slowness = -1; + private int messageCount = 100; + private String message = "Hello"; + + public ClientWriteThread(Session session) + { + this.session = session; + } + + public String getMessage() + { + return message; + } + + public int getMessageCount() + { + return messageCount; + } + + public int getSlowness() + { + return slowness; + } + + @Override + public void run() + { + final AtomicInteger m = new AtomicInteger(); + + try + { + LOG.debug("Writing {} messages to connection {}",messageCount); + LOG.debug("Artificial Slowness {} ms",slowness); + Future lastMessage = null; + RemoteEndpoint remote = session.getRemote(); + while (m.get() < messageCount) + { + lastMessage = remote.sendStringByFuture(message + "/" + m.get() + "/"); + + m.incrementAndGet(); + + if (slowness > 0) + { + TimeUnit.MILLISECONDS.sleep(slowness); + } + } + if (remote.getBatchMode() == BatchMode.ON) + remote.flush(); + // block on write of last message + if (lastMessage != null) + lastMessage.get(2,TimeUnit.MINUTES); // block on write + } + catch (Exception e) + { + LOG.warn(e); + } + } + + public void setMessage(String message) + { + this.message = message; + } + + public void setMessageCount(int messageCount) + { + this.messageCount = messageCount; + } + + public void setSlowness(int slowness) + { + this.slowness = slowness; + } +} \ No newline at end of file