Skip to content

Commit

Permalink
Merge branch 'optionalsingleread'
Browse files Browse the repository at this point in the history
  • Loading branch information
ptrd committed Jun 14, 2017
2 parents ee94448 + e2c0fba commit 6002215
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.SocketTimeoutException;
import java.net.URL;


Expand Down Expand Up @@ -65,12 +66,27 @@ protected WebSocketClient prepareWebSocketClient(SampleResult result) {

@Override
protected Object doSample(WebSocketClient wsClient, SampleResult result) throws IOException, UnexpectedFrameException, SamplingAbortedException {
return getBinary() ? wsClient.receiveBinaryData(readTimeout) : wsClient.receiveText(readTimeout);
try {
return getBinary() ? wsClient.receiveBinaryData(readTimeout) : wsClient.receiveText(readTimeout);
}
catch (SocketTimeoutException readTimeout) {
if (getOptional())
return null;
else
throw readTimeout;
}
}

@Override
protected void postProcessResponse(Object response, SampleResult result) {
processDefaultReadResponse(response, getBinary(), result);
if (response == null && getOptional()) {
log.debug("Sampler '" + getName() + "' received no response (read timeout).");
result.setSuccessful(true);
result.setResponseCode("No response");
result.setResponseMessage("Read timeout, no response received.");
}
else
processDefaultReadResponse(response, getBinary(), result);
}


Expand Down Expand Up @@ -152,4 +168,12 @@ public String getReadTimeout() {
public void setReadTimeout(String readTimeout) {
setProperty("readTimeout", readTimeout, "" + WebSocketClient.DEFAULT_READ_TIMEOUT);
}

public boolean getOptional() {
return getPropertyAsBoolean("optional");
}

public void setOptional(boolean optional) {
setProperty("optional", optional);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ public void configure(TestElement element) {
settingsPanel.connectionTimeoutField.setText(sampler.getConnectTimeout());
settingsPanel.typeSelector.setSelectedItem(sampler.getBinary()? BINARY: TEXT);
settingsPanel.readTimeoutField.setText(sampler.getReadTimeout());
settingsPanel.optionalSampler.setSelected(sampler.getOptional());
}
}

Expand All @@ -92,6 +93,7 @@ public void modifyTestElement(TestElement element) {
sampler.setBinary(settingsPanel.typeSelector.getSelectedItem() == BINARY);
sampler.setCreateNewConnection(settingsPanel.newConnection.isSelected());
sampler.setReadTimeout(settingsPanel.readTimeoutField.getText());
sampler.setOptional(settingsPanel.optionalSampler.isSelected());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,8 @@
*/
package eu.luminis.jmeter.wssampler;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import javax.swing.*;
import java.awt.*;

import static javax.swing.BoxLayout.Y_AXIS;

Expand All @@ -38,6 +29,7 @@ public class SingleReadWebSocketSamplerGuiPanel extends WebSocketSamplerGuiPanel
public static final String TEXT = "Text";

JComboBox typeSelector;
JCheckBox optionalSampler;

public SingleReadWebSocketSamplerGuiPanel() {
init();
Expand Down Expand Up @@ -81,6 +73,14 @@ private void init() {
requestSettingsPanel.add(readTimeoutErrorField);
}
dataPanel.add(requestSettingsPanel);

JPanel optionalSettingsPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
{
optionalSampler = new JCheckBox("Optional read (read timeout will not lead to sampler error)");
optionalSettingsPanel.add(optionalSampler);
}

dataPanel.add(optionalSettingsPanel);
}
boxPanel.add(dataPanel);
boxPanel.add(createAboutPanel(this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package eu.luminis.jmeter.wssampler;

import eu.luminis.websocket.EndOfStreamException;
import eu.luminis.websocket.WebSocketClient;
import org.apache.jmeter.samplers.SampleResult;
import org.junit.Test;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.net.SocketTimeoutException;
import java.net.URL;

import static org.junit.Assert.assertEquals;
Expand All @@ -41,31 +43,93 @@ public void testSingleReadSamplerSample() throws Exception {
SingleReadWebSocketSampler sampler = new SingleReadWebSocketSampler() {
@Override
protected WebSocketClient prepareWebSocketClient(SampleResult result) {
return createDefaultWsClientMock();
return createDefaultWsClientMock(null);
}
};

SampleResult result = sampler.sample(null);
assertTrue(result.getTime() > 300);
assertTrue(result.getTime() >= 300);
assertTrue(result.getTime() < 400); // A bit tricky of course, but on decent computers the call should not take more than 100 ms....
assertEquals("ws-response-data", result.getResponseDataAsString());
assertFalse(result.getSamplerData().contains("Request data:"));
assertFalse(result.getSamplerData().contains("ws-response-data"));
}

@Test
public void readTimeoutLeadsToUnsccessfulResult() {
SingleReadWebSocketSampler sampler = new SingleReadWebSocketSampler() {
@Override
protected WebSocketClient prepareWebSocketClient(SampleResult result) {
return createDefaultWsClientMock(new SocketTimeoutException("Read timed out"));
}
};

SampleResult result = sampler.sample(null);
assertTrue(result.getTime() >= 300);
assertTrue(result.getTime() < 400);
assertFalse(result.isSuccessful());
}

@Test
public void lostConnectionLeadsToUnsuccessfulResult() {
SingleReadWebSocketSampler sampler = new SingleReadWebSocketSampler() {
@Override
protected WebSocketClient prepareWebSocketClient(SampleResult result) {
return createDefaultWsClientMock(new EndOfStreamException("end of stream"));
}
};

SampleResult result = sampler.sample(null);
assertTrue(result.getTime() >= 300);
assertTrue(result.getTime() < 400);
assertFalse(result.isSuccessful());
}

@Test
public void optionalReadShouldBeSuccessfulOnReadTimeout() {
SingleReadWebSocketSampler sampler = new SingleReadWebSocketSampler() {
@Override
protected WebSocketClient prepareWebSocketClient(SampleResult result) {
return createDefaultWsClientMock(new SocketTimeoutException("Read timed out"));
}
};
sampler.setOptional(true);

SampleResult result = sampler.sample(null);
assertTrue(result.getTime() >= 300);
assertTrue(result.getTime() < 400);
assertTrue(result.isSuccessful());
assertEquals("No response", result.getResponseCode());
assertEquals("Read timeout, no response received.", result.getResponseMessage());
}

@Test
public void lostConnectionOnOptionalReadShouldResultInUnsuccessfulResult() {
SingleReadWebSocketSampler sampler = new SingleReadWebSocketSampler() {
@Override
protected WebSocketClient prepareWebSocketClient(SampleResult result) {
return createDefaultWsClientMock(new EndOfStreamException("end of stream"));
}
};
sampler.setOptional(true);

SampleResult result = sampler.sample(null);
assertTrue(result.getTime() >= 300);
assertTrue(result.getTime() < 400);
assertFalse(result.isSuccessful());
}


WebSocketClient createDefaultWsClientMock() {
private WebSocketClient createDefaultWsClientMock(Exception exception) {
try {
WebSocketClient mockWsClient = Mockito.mock(WebSocketClient.class);
when(mockWsClient.getConnectUrl()).thenReturn(new URL("http://nowhere.com"));
//when(mockWsClient.receiveText(anyInt())).thenReturn("ws-response-data");
when(mockWsClient.receiveText(anyInt())).thenAnswer(new Answer<String>(){
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
Thread.sleep(300);
if (exception != null)
throw exception;
return "ws-response-data";
}
});
Expand Down

0 comments on commit 6002215

Please sign in to comment.