Skip to content
Permalink
Browse files

BackgroundWorker thread fix

Issue #363
  • Loading branch information...
droppedcalls authored and gquerret committed Aug 15, 2019
1 parent f459acc commit da1eb3407fb9f8058aa47339b2d73ca22236ae88
@@ -13,5 +13,10 @@
<classpathentry kind="lib" path="lib/ast-dependencies.jar"/>
<classpathentry kind="lib" path="lib/ini4j-0.5.1.jar"/>
<classpathentry kind="lib" path="lib/commonmark-0.11.0.jar"/>
<classpathentry kind="lib" path="lib/mockito-core-3.0.0.jar"/>
<classpathentry kind="lib" path="lib/byte-buddy-1.9.14.jar"/>
<classpathentry kind="lib" path="lib/byte-buddy-agent-1.9.14.jar"/>
<classpathentry kind="lib" path="lib/hamcrest-2.1.jar"/>
<classpathentry kind="lib" path="lib/objenesis-2.6.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
@@ -102,6 +102,11 @@
<pathelement location="lib/testng-6.13.1.jar" />
<pathelement location="lib/ini4j-0.5.1.jar" />
<pathelement location="lib/guava.jar" />
<pathelement location="lib/mockito-core-3.0.0.jar" />
<pathelement location="lib/byte-buddy-1.9.14.jar" />
<pathelement location="lib/byte-buddy-agent-1.9.14.jar" />
<pathelement location="lib/hamcrest-2.1.jar" />
<pathelement location="lib/objenesis-2.6.jar" />
</path>

<target name="init" description="Initialize PCT builder" depends="init-properties">
@@ -404,7 +409,22 @@
<zipfileset dir="${build-test}" includes="**/*" prefix="build/test" />
<zipfileset dir="tests" includes="**/*" prefix="testbox" />
<zipfileset dir="${src.progress}" includes="Consultingwerk/**/*" prefix="testbox/ClassDocumentation/test3/src" />
<zipfileset dir="lib" includes="ast.jar,ast-dependencies.jar,ini4j-0.5.1.jar,guava.jar,testng-6.13.1.jar,jcommander-1.30.jar,jacocoant-0.8.4.jar,xmltask.jar,*.pl" prefix="lib" />
<zipfileset dir="lib" prefix="lib">
<include name="ast.jar" />
<include name="ast-dependencies.jar" />
<include name="ini4j-0.5.1.jar" />
<include name="guava.jar" />
<include name="testng-6.13.1.jar" />
<include name="jcommander-1.30.jar" />
<include name="jacocoant-0.8.4.jar" />
<include name="xmltask.jar" />
<include name="mockito-core-3.0.0.jar" />
<include name="objenesis-2.6.jar" />
<include name="hamcrest-2.1.jar" />
<include name="byte-buddy-1.9.14.jar" />
<include name="byte-buddy-agent-1.9.14.jar" />
<include name="*.pl" />
</zipfileset>
</zip>
</target>

Binary file not shown.
Binary file not shown.
BIN +120 KB lib/hamcrest-2.1.jar
Binary file not shown.
Binary file not shown.
BIN +54.4 KB lib/objenesis-2.6.jar
Binary file not shown.
@@ -102,33 +102,37 @@ public final void listen() {
while (!end) {
try {
String str = reader.readLine();
int idx = str.indexOf(':');
String result = (idx == -1 ? str : str.substring(0, idx));

if ("OK".equalsIgnoreCase(result)) {
if ("quit".equalsIgnoreCase(result)) {
status = 5;
if (str != null) {
int idx = str.indexOf(':');
String result = (idx == -1 ? str : str.substring(0, idx));

if ("OK".equalsIgnoreCase(result)) {
if ("quit".equalsIgnoreCase(lastCommand)) {
status = 5;
}
if ((idx != -1) && (idx < (str.length() - 1)))
customResponse = str.substring(idx + 1);
} else if ("ERR".equalsIgnoreCase(result)) {
err = true;
if ((idx != -1) && (idx < (str.length() - 1)))
customResponse = str.substring(idx + 1);
} else if ("MSG".equalsIgnoreCase(result)) {
// Everything after MSG: is logged
if ((idx != -1) && (idx < (str.length() - 1)))
retVals.add(new Message(str.substring(idx + 1)));
} else if ("END".equalsIgnoreCase(result)) {
end = true;
// Standard commands (i.e. sent by this class) cannnot be handled and overridden
if (!isStandardCommand(lastCommand)) {
handleResponse(lastCommand, lastCommandParameter, err, customResponse,
retVals);
} else {
handleStandardEventResponse(lastCommand, lastCommandParameter, err, customResponse,
retVals);
}
}
if ((idx != -1) && (idx < (str.length() - 1)))
customResponse = str.substring(idx + 1);
} else if ("ERR".equalsIgnoreCase(result)) {
err = true;
if ((idx != -1) && (idx < (str.length() - 1)))
customResponse = str.substring(idx + 1);
} else if ("MSG".equalsIgnoreCase(result)) {
// Everything after MSG: is logged
if ((idx != -1) && (idx < (str.length() - 1)))
retVals.add(new Message(str.substring(idx + 1)));
} else if ("END".equalsIgnoreCase(result)) {
} else {
end = true;
// Standard commands (i.e. sent by this class) cannnot be handled and overridden
if (!isStandardCommand(lastCommand)) {
handleResponse(lastCommand, lastCommandParameter, err, customResponse,
retVals);
} else {
handleStandardEventResponse(lastCommand, lastCommandParameter, err, customResponse,
retVals);
}
}
} catch (IOException ioe) {
parent.log(ioe, Project.MSG_ERR);
@@ -0,0 +1,120 @@
package com.phenix.pct;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.Charset;
import java.util.List;
import java.util.Map;

import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import com.phenix.pct.BackgroundWorker.Message;

public class BackgroundWorkerTest {
private SimpleBackgroundWorker backgroundWorker;

private static class SimpleBackgroundWorker extends BackgroundWorker {

private List<Message> returnValues;
private String customResponse;

public SimpleBackgroundWorker(PCTBgRun parent) {
super(parent);
}

@Override
protected boolean performCustomAction() throws IOException {
return false;
}

@Override
public void setCustomOptions(Map<String, String> options) {

}

@Override
public void handleResponse(String command, String parameter, boolean err,
String customResponse, List<Message> returnValues) {
this.returnValues = returnValues;
this.customResponse = customResponse;
}

public String getCustomResponse() {
return customResponse;
}

public List<Message> getReturnValues() {
return returnValues;
}

}

@BeforeTest(groups = {"v10"})
public void setUp() {
PCTBgRun parent = mock(PCTBgRun.class);
backgroundWorker = new SimpleBackgroundWorker(parent);

Charset utf8 = Charset.forName("utf-8");
when(parent.getCharset()).thenReturn(utf8);
}

public Socket setUpSocket(String contentsToRead) throws IOException {
Socket socket = mock(Socket.class);

InputStream bais = new ByteArrayInputStream(contentsToRead.getBytes());
OutputStream mockOS = mock(OutputStream.class);
when(socket.getInputStream()).thenReturn(bais);
when(socket.getOutputStream()).thenReturn(mockOS);

return socket;
}

@Test(groups = {"v10"})
public void readMSGFromResponsePacket() throws IOException {
Socket socket = setUpSocket( "\n" + "MSG:0:Message here\n" + "END");

// get more general overview
backgroundWorker.initialize(socket);
backgroundWorker.listen();
assertNotNull(backgroundWorker.getReturnValues());
assertEquals(1, backgroundWorker.getReturnValues().size(), "Expected one message");
Message m = backgroundWorker.getReturnValues().get(0);
assertEquals("Message here", m.getMsg());
assertEquals(0, m.getLevel());
}

@Test(groups = {"v10"})
public void readOKFromResponsePacket() throws IOException {
Socket socket = setUpSocket("\n" + "OK\n" + "END");

backgroundWorker.initialize(socket);
backgroundWorker.listen();
}

@Test(groups = {"v10"})
public void readERRFromResponsePacket() throws IOException {
Socket socket = setUpSocket("\n" + "ERR:Error message\n" + "END");

backgroundWorker.initialize(socket);
backgroundWorker.listen();

assertEquals("Error message", backgroundWorker.getCustomResponse());
}

@Test(groups = {"v10"})
public void testEmptyResponsePacketDoesNotThrowNPE() throws IOException {
Socket socket = setUpSocket("\n");

backgroundWorker.initialize(socket);
backgroundWorker.listen();
}
}

0 comments on commit da1eb34

Please sign in to comment.
You can’t perform that action at this time.