From 3fd9dfebc80e4081423654dd6941fa746a693244 Mon Sep 17 00:00:00 2001 From: Jan Becker Date: Tue, 15 Dec 2015 14:15:09 +0100 Subject: [PATCH] added option to reset streaming connection in sampler; shutdown executor on test end to prevent the JVM not exiting after test end --- .../samplers/websocket/WebSocketSampler.java | 28 +++++++++++++++---- .../websocket/WebSocketSamplerGui.java | 2 ++ .../websocket/WebSocketSamplerPanel.form | 8 ++++++ .../websocket/WebSocketSamplerPanel.java | 23 ++++++++++++--- 4 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSampler.java b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSampler.java index 2747394..be15a1b 100644 --- a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSampler.java +++ b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSampler.java @@ -57,8 +57,8 @@ public class WebSocketSampler extends AbstractSampler implements TestStateListen private static Map connectionList; - private static ExecutorService executor = Executors.newCachedThreadPool(); - + private static ExecutorService executor; + public WebSocketSampler() { super(); setName("WebSocket sampler"); @@ -70,14 +70,20 @@ private ServiceSocket getConnectionSocket() throws URISyntaxException, Exception String connectionId = getThreadName() + getConnectionId(); if (isStreamingConnection() && connectionList.containsKey(connectionId)) { - ServiceSocket socket = connectionList.get(connectionId); - socket.initialize(); - return socket; + ServiceSocket socket = connectionList.get(connectionId); + if (!isResetStreamingConnection()) { + socket.initialize(); + return socket; + } else { + connectionList.remove(connectionId); + socket.close(); + } } //Create WebSocket client SslContextFactory sslContexFactory = new SslContextFactory(); sslContexFactory.setTrustAll(isIgnoreSslErrors()); +// WebSocketClient webSocketClient = new WebSocketClient(sslContexFactory, executor); WebSocketClient webSocketClient = new WebSocketClient(sslContexFactory, executor); ServiceSocket socket = new ServiceSocket(this, webSocketClient); @@ -354,10 +360,18 @@ public Boolean isIgnoreSslErrors() { public void setStreamingConnection(Boolean streamingConnection) { setProperty("streamingConnection", streamingConnection); } - + public Boolean isStreamingConnection() { return getPropertyAsBoolean("streamingConnection"); } + + public void setResetStreamingConnection(Boolean resetStreamingConnection) { + setProperty("resetStreamingConnection", resetStreamingConnection); + } + + public Boolean isResetStreamingConnection() { + return getPropertyAsBoolean("resetStreamingConnection"); + } public void setConnectionId(String connectionId) { setProperty("connectionId", connectionId); @@ -486,6 +500,7 @@ public void testStarted() { @Override public void testStarted(String host) { connectionList = new ConcurrentHashMap(); + executor = Executors.newCachedThreadPool(); } @Override @@ -498,6 +513,7 @@ public void testEnded(String host) { for (ServiceSocket socket : connectionList.values()) { socket.close(); } + executor.shutdown(); } diff --git a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerGui.java b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerGui.java index 0b6933b..bf029c8 100644 --- a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerGui.java +++ b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerGui.java @@ -59,6 +59,7 @@ public void configure(TestElement element) { webSocketSamplerPanel.setConnectionTimeout(webSocketSamplerTestElement.getConnectionTimeout()); webSocketSamplerPanel.setIgnoreSslErrors(webSocketSamplerTestElement.isIgnoreSslErrors()); webSocketSamplerPanel.setStreamingConnection(webSocketSamplerTestElement.isStreamingConnection()); + webSocketSamplerPanel.setResetStreamingConnection(webSocketSamplerTestElement.isResetStreamingConnection()); webSocketSamplerPanel.setConnectionId(webSocketSamplerTestElement.getConnectionId()); webSocketSamplerPanel.setResponsePattern(webSocketSamplerTestElement.getResponsePattern()); webSocketSamplerPanel.setCloseConncectionPattern(webSocketSamplerTestElement.getCloseConncectionPattern()); @@ -98,6 +99,7 @@ public void modifyTestElement(TestElement te) { webSocketSamplerTestElement.setResponseTimeout(webSocketSamplerPanel.getResponseTimeout()); webSocketSamplerTestElement.setIgnoreSslErrors(webSocketSamplerPanel.isIgnoreSslErrors()); webSocketSamplerTestElement.setStreamingConnection(webSocketSamplerPanel.isStreamingConnection()); + webSocketSamplerTestElement.setResetStreamingConnection(webSocketSamplerPanel.isResetStreamingConnection()); webSocketSamplerTestElement.setConnectionId(webSocketSamplerPanel.getConnectionId()); webSocketSamplerTestElement.setResponsePattern(webSocketSamplerPanel.getResponsePattern()); webSocketSamplerTestElement.setCloseConncectionPattern(webSocketSamplerPanel.getCloseConncectionPattern()); diff --git a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.form b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.form index 2ed45b8..d5d9513 100644 --- a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.form +++ b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.form @@ -207,6 +207,8 @@ + + @@ -244,6 +246,7 @@ + @@ -332,6 +335,11 @@ + + + + + diff --git a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.java b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.java index 4185aa6..51460ae 100644 --- a/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.java +++ b/src/main/java/JMeter/plugins/functional/samplers/websocket/WebSocketSamplerPanel.java @@ -64,6 +64,7 @@ private void initComponents() { jLabel15 = new javax.swing.JLabel(); implementationComboBox = new javax.swing.JComboBox(); streamingConnectionCheckBox = new javax.swing.JCheckBox(); + resetStreamingConnectionCheckBox = new javax.swing.JCheckBox(); jPanel5 = new javax.swing.JPanel(); jLabel7 = new javax.swing.JLabel(); responsePatternTextField = new javax.swing.JTextField(); @@ -172,6 +173,8 @@ private void initComponents() { implementationComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "RFC6455 (v13)" })); streamingConnectionCheckBox.setText("Streaming connection"); + + resetStreamingConnectionCheckBox.setText("Reset streaming connection"); javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3); jPanel3.setLayout(jPanel3Layout); @@ -204,7 +207,9 @@ private void initComponents() { .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(ignoreSslErrorsCheckBox) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) - .addComponent(streamingConnectionCheckBox))) + .addComponent(streamingConnectionCheckBox) + .addComponent(resetStreamingConnectionCheckBox) + )) .addGap(0, 0, Short.MAX_VALUE)) .addGroup(jPanel3Layout.createSequentialGroup() .addComponent(jLabel5) @@ -232,7 +237,8 @@ private void initComponents() { .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED) .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE) .addComponent(ignoreSslErrorsCheckBox) - .addComponent(streamingConnectionCheckBox)) + .addComponent(streamingConnectionCheckBox) + .addComponent(resetStreamingConnectionCheckBox)) .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED) .addComponent(querystringAttributesPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 102, Short.MAX_VALUE) .addGap(8, 8, 8) @@ -425,6 +431,7 @@ private void initComponents() { private javax.swing.JTextField serverAddressTextField; private javax.swing.JTextField serverPortTextField; private javax.swing.JCheckBox streamingConnectionCheckBox; + private javax.swing.JCheckBox resetStreamingConnectionCheckBox; // End of variables declaration//GEN-END:variables public void initFields() { @@ -549,15 +556,23 @@ public void setRequestPayload(String requestPayload) { public String getRequestPayload() { return requestPayloadEditorPane.getText(); } - + public void setStreamingConnection(Boolean streamingConnection) { streamingConnectionCheckBox.setSelected(streamingConnection); } - + public Boolean isStreamingConnection() { return streamingConnectionCheckBox.isSelected(); } + public void setResetStreamingConnection(Boolean resetStreamingConnection) { + resetStreamingConnectionCheckBox.setSelected(resetStreamingConnection); + } + + public Boolean isResetStreamingConnection() { + return resetStreamingConnectionCheckBox.isSelected(); + } + public void setIgnoreSslErrors(Boolean ignoreSslErrors) { ignoreSslErrorsCheckBox.setSelected(ignoreSslErrors); }