Mercurial > stress-tester
changeset 1025:c0bf1f5ed2ee
NioEmitterTest extends AbstractWireMockTest
author | Devel 2 |
---|---|
date | Wed, 01 Apr 2020 10:57:59 +0200 |
parents | 14adc04b1b8d |
children | 1921cac1f89e |
files | stress-tester/src/test/java/com/passus/st/emitter/nio/NioEmitterTest.java |
diffstat | 1 files changed, 8 insertions(+), 137 deletions(-) [+] |
line wrap: on
line diff
--- a/stress-tester/src/test/java/com/passus/st/emitter/nio/NioEmitterTest.java Wed Apr 01 10:52:41 2020 +0200 +++ b/stress-tester/src/test/java/com/passus/st/emitter/nio/NioEmitterTest.java Wed Apr 01 10:57:59 2020 +0200 @@ -1,157 +1,28 @@ package com.passus.st.emitter.nio; -import static com.github.tomakehurst.wiremock.client.WireMock.*; -import com.passus.data.ByteBuff; -import com.passus.data.HeapByteBuff; -import com.passus.net.http.HttpConsts; -import com.passus.net.http.HttpRequest; -import com.passus.net.http.HttpRequestBuilder; -import com.passus.net.http.HttpRequestEncoder; -import com.passus.st.AbstractWireMockTest; import com.passus.st.client.TestClientHandler; import com.passus.st.client.TestClientHandler.ClientEvent; import com.passus.st.client.TestClientHandler.EventType; +import com.passus.st.emitter.AbstractEmitterTest; import com.passus.st.emitter.ChannelContext; +import com.passus.st.emitter.Emitter; import com.passus.st.emitter.SessionInfo; -import java.net.ConnectException; import org.testng.AssertJUnit; -import static org.testng.AssertJUnit.assertEquals; -import static org.testng.AssertJUnit.assertTrue; import org.testng.annotations.Test; +import java.net.ConnectException; + /** - * * @author Mirosław Hawrot */ -public class NioEmitterTest extends AbstractWireMockTest { - - private void waitConn(TestClientHandler handler, int expectedEventsSize) { - waitConn(handler, expectedEventsSize, 5_000); - } - - private void waitConn(TestClientHandler handler, int expectedEventsSize, long timeout) { - long endTime = System.currentTimeMillis() + timeout; - if (endTime <= 0) { - endTime = Long.MAX_VALUE; - } +public class NioEmitterTest extends AbstractEmitterTest { - while (true) { - if (handler.size() >= expectedEventsSize - || System.currentTimeMillis() >= endTime - || handler.isChannelUnregistered()) { - break; - } - - try { - Thread.sleep(100); - } catch (Exception ignore) { - } - } - } - - @Test(enabled = true) - public void testConnectAndClose() throws Exception { + @Override + public Emitter createEmitter() { NioEmitter emitter = new NioEmitter(); emitter.setMaxThreads(1); - emitter.start(); - SessionInfo info = new SessionInfo("1.1.1.1", 5000, HOST, port()); - - TestClientHandler handler = new TestClientHandler() { - @Override - protected void doChannelActive(ChannelContext context) throws Exception { - context.close(); - } - }; - - emitter.connect(info, handler, 0); - waitConn(handler, 4); - - AssertJUnit.assertEquals(4, handler.size()); - AssertJUnit.assertEquals(EventType.CHANNEL_REGISTERED, handler.get(0).getType()); - AssertJUnit.assertEquals(EventType.CHANNEL_ACTIVE, handler.get(1).getType()); - AssertJUnit.assertEquals(EventType.CHANNEL_INACTIVE, handler.get(2).getType()); - AssertJUnit.assertEquals(EventType.CHANNEL_UNREGISTERED, handler.get(3).getType()); + return emitter; } - @Test(enabled = true) - public void testWriteAndRead() throws Exception { - String path = "/some/thing"; - String url = "http://localhost" + path; - HttpRequest req = HttpRequestBuilder.get(url).version(HttpConsts.VERSION_1_0).build(); - String content = "Hello world!"; - stubFor(get(urlEqualTo("/some/thing")) - .willReturn(aResponse() - .withHeader("Content-Type", "text/plain") - .withHeader("Content-Length", "" + content.length()) - .withBody(content))); - - NioEmitter emitter = new NioEmitter(); - emitter.setMaxThreads(1); - emitter.start(); - SessionInfo info = new SessionInfo("1.1.1.1", 5000, HOST, port()); - - TestClientHandler handler = new TestClientHandler() { - - @Override - protected void doChannelActive(ChannelContext context) throws Exception { - ByteBuff buff = new HeapByteBuff(); - HttpRequestEncoder.getInstance().encode(req, buff); - context.writeAndFlush(buff); - } - - @Override - protected void doDataReceived(ChannelContext context, ByteBuff data) throws Exception { - if (data.toString().contains("!")) { - context.close(); - } - } - - }; - - emitter.connect(info, handler, 0); - waitConn(handler, 20, 5_000); - assertTrue(handler.size() >= 6); - int index = 0; - assertEquals(EventType.CHANNEL_REGISTERED, handler.get(index++).getType()); - assertEquals(EventType.CHANNEL_ACTIVE, handler.get(index++).getType()); - assertEquals(EventType.DATA_WRITTEN, handler.get(index++).getType()); - - ByteBuff resContentBuff = new HeapByteBuff(); - - do { - ClientEvent event = handler.get(index++); - assertEquals(EventType.DATA_RECEIVED, event.getType()); - resContentBuff.append(event.getData()); - } while (handler.get(index).getType() == EventType.DATA_RECEIVED); - - assertEquals(EventType.CHANNEL_INACTIVE, handler.get(index++).getType()); - assertEquals(EventType.CHANNEL_UNREGISTERED, handler.get(index++).getType()); - String responseContent = resContentBuff.toString(); - assertTrue(responseContent.endsWith(content)); - } - - @Test(enabled = true) - public void testError_ConnectionRefused() throws Exception { - NioEmitter emitter = new NioEmitter(); - emitter.setMaxThreads(1); - emitter.start(); - SessionInfo info = new SessionInfo("1.1.1.1", 5000, HOST, 10_000); - - TestClientHandler handler = new TestClientHandler() { - @Override - protected void doChannelActive(ChannelContext context) throws Exception { - context.close(); - } - }; - - emitter.connect(info, handler, 0); - waitConn(handler, 2); - - AssertJUnit.assertEquals(EventType.CHANNEL_REGISTERED, handler.get(0).getType()); - ClientEvent clientEvent = handler.get(1); - AssertJUnit.assertEquals(EventType.ERROR_OCCURRED, clientEvent.getType()); - AssertJUnit.assertTrue(clientEvent.getCause() instanceof ConnectException); - AssertJUnit.assertTrue(clientEvent.getCause().getMessage().startsWith("Connection refused")); - } }