Skip to content

Commit

Permalink
Update input threshold behavior
Browse files Browse the repository at this point in the history
Add separate line/timeout threshold fail options, and add
a success option if input threshold is met

[#482]
  • Loading branch information
gschueler committed Nov 23, 2011
1 parent 91d671b commit be2b3f8
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 36 deletions.
Expand Up @@ -67,9 +67,14 @@ public interface Responder {
public long getInputMaxTimeout();

/**
* Return true if input threshold indicates failure
* Return true if input max lines threshold indicates failure
*/
public boolean isFailOnInputThreshold();
public boolean isFailOnInputLinesThreshold();

/**
* Return true if input max timeout threshold indicates failure
*/
public boolean isFailOnInputTimeoutThreshold();

/**
* Return a regex to detect response to input was successful
Expand Down Expand Up @@ -100,4 +105,9 @@ public interface Responder {
* Return input string to send after successful input pattern (including any newline characters as necessary)
*/
public String getInputString();

/**
* Return true if input threshold indicates success
*/
public boolean isSuccessOnInputThreshold();
}
Expand Up @@ -115,10 +115,17 @@ private void runResponder() {
return;
}
} catch (ThreshholdException e) {
if (responder.isFailOnInputThreshold()) {
if (responder.isFailOnInputLinesThreshold() && e.getType() == ThresholdType.lines) {
logger.debug("Threshold met " + reason(e));
fail(step, reason(e));
return;
}else if (responder.isFailOnInputTimeoutThreshold() && e.getType() == ThresholdType.milliseconds) {
logger.debug("Threshold met " + reason(e));
fail(step, reason(e));
return;
} else if (responder.isSuccessOnInputThreshold()) {
success = true;
return;
}
}
}
Expand Down Expand Up @@ -201,8 +208,8 @@ public static interface ResultHandler {
* If a max timeout or max number of lines to read is exceeded, throw threshhold error.
*/
static boolean detect(final String detectPattern, final String failurePattern, final long timeout,
final int maxLines, final InputStreamReader reader,
final ResponderStopper thread, final PartialLineBuffer buffer) throws IOException, ThreshholdException {
final int maxLines, final InputStreamReader reader, final ResponderStopper thread,
final PartialLineBuffer buffer) throws IOException, ThreshholdException {
if (null == detectPattern && null == failurePattern) {
throw new IllegalArgumentException("detectPattern or failurePattern required");
}
Expand Down Expand Up @@ -248,14 +255,14 @@ static boolean detect(final String detectPattern, final String failurePattern, f
}
}

// if (!reader.ready()) {
// try {
// sleep(500);
// } catch (InterruptedException e) {
// //ignore
// }
// continue;
// }
if (!reader.ready()) {
try {
sleep(500);
} catch (InterruptedException e) {
//ignore
}
continue;
}
final int c = buffer.read(reader);
if (c < 0) {
//end of stream
Expand Down
Expand Up @@ -270,7 +270,9 @@ static class testResponder implements Responder{
private String responseFailurePattern;
private int inputMaxLines;
private long inputMaxTimeout;
private boolean failOnInputThreshold;
private boolean failOnInputLinesThreshold;
private boolean failOnInputTimeoutThreshold;
private boolean successOnInputThreshold;
private int responseMaxLines;
private long responseMaxTimeout;
private boolean failOnResponseThreshold;
Expand Down Expand Up @@ -300,8 +302,8 @@ public long getInputMaxTimeout() {
return inputMaxTimeout;
}

public boolean isFailOnInputThreshold() {
return failOnInputThreshold;
public boolean isFailOnInputLinesThreshold() {
return failOnInputLinesThreshold;
}

public int getResponseMaxLines() {
Expand All @@ -319,13 +321,21 @@ public boolean isFailOnResponseThreshold() {
public String getInputString() {
return inputString;
}

public boolean isSuccessOnInputThreshold() {
return successOnInputThreshold;
}

public boolean isFailOnInputTimeoutThreshold() {
return failOnInputTimeoutThreshold;
}
}
public void testRunResponderDefault(){
ByteArrayInputStream bais = new ByteArrayInputStream(
"Test1\nTest1\nTest1\nTest2: blah\nTest1\nTest3: blah\nTest4: bloo".getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final testResponder testResponder = new testResponder();
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
Expand All @@ -341,7 +351,7 @@ public void testRunResponderInputSuccess(){
testResponder.inputSuccessPattern = "^Test2: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
Expand All @@ -358,7 +368,8 @@ public void testRunResponderInputSuccessMiss(){
testResponder.inputSuccessPattern = "^TestZ: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnInputTimeoutThreshold = true;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
Expand All @@ -367,6 +378,24 @@ public void testRunResponderInputSuccessMiss(){
assertEquals("Failed waiting for input prompt: Expected input was not seen in 5 lines", responderThread.getFailureReason());
assertFalse(responderThread.isResponderStopped());
}
public void testRunResponderInputSuccessMissTimeout(){
ByteArrayInputStream bais = new ByteArrayInputStream(
"Test1\nTest1\nTest1\nTest2: blah\nTest1\nTest3: blah\nTest4: bloo".getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final testResponder testResponder = new testResponder();
testResponder.inputSuccessPattern = "^TestZ: .*";
testResponder.inputMaxLines = 20;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnInputTimeoutThreshold = true;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
assertFalse(responderThread.isSuccess());
assertTrue(responderThread.isFailed());
assertEquals("Failed waiting for input prompt: Expected input was not seen in 1000 milliseconds", responderThread.getFailureReason());
assertFalse(responderThread.isResponderStopped());
}

public void testRunResponderInputSuccessMissNofail(){
ByteArrayInputStream bais = new ByteArrayInputStream(
Expand All @@ -376,7 +405,8 @@ public void testRunResponderInputSuccessMissNofail(){
testResponder.inputSuccessPattern = "^TestZ: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputThreshold = false;
testResponder.failOnInputLinesThreshold = false;
testResponder.failOnInputTimeoutThreshold = false;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -388,6 +418,30 @@ public void testRunResponderInputSuccessMissNofail(){
assertEquals("Test", baos.toString());
}

public void testRunResponderInputSuccessOnInputThreshold() {
ByteArrayInputStream bais = new ByteArrayInputStream(
"Test1\nTest1\nTest1\nTest2: blah\nTest1\nTest3: blah\nTest4: bloo".getBytes());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
final testResponder testResponder = new testResponder();
testResponder.inputSuccessPattern = "^TestZ: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.successOnInputThreshold = true; //set to true
testResponder.failOnInputLinesThreshold = false;
testResponder.failOnInputTimeoutThreshold = false;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
assertTrue(responderThread.isSuccess());
assertFalse(responderThread.isFailed());
assertNull(responderThread.getFailureReason());
assertFalse(responderThread.isResponderStopped());

//result is no output
assertEquals("", baos.toString());
}

public void testRunResponderInputFailure() {
ByteArrayInputStream bais = new ByteArrayInputStream(
"Test1\nTest1\nTest1\nTest2: blah\nTest1\nTest3: blah\nTest4: bloo".getBytes());
Expand All @@ -396,7 +450,7 @@ public void testRunResponderInputFailure() {
testResponder.inputFailurePattern = "^Test2: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
Expand All @@ -414,7 +468,7 @@ public void testRunResponderInputFailureMiss() {
testResponder.inputFailurePattern = "^TestZ: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
Expand All @@ -432,7 +486,7 @@ public void testRunResponderInputFailureMissNoFail() {
testResponder.inputFailurePattern = "^TestZ: .*";
testResponder.inputMaxLines = 5;
testResponder.inputMaxTimeout = 1000;
testResponder.failOnInputThreshold = false;
testResponder.failOnInputLinesThreshold = false;
testResponder.failOnResponseThreshold = true;
final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
responderThread.run();
Expand All @@ -454,7 +508,7 @@ public void testRunResponderResponseSuccess() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;

final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -476,7 +530,7 @@ public void testRunResponderResponseSuccessMiss() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;

final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -499,7 +553,7 @@ public void testRunResponderResponseSuccessMissNoFail() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = false;

final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -521,7 +575,7 @@ public void testRunResponderResponseFailure() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;

final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -544,7 +598,7 @@ public void testRunResponderResponseFailureMiss() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;

final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -567,7 +621,7 @@ public void testRunResponderResponseFailureMissNoFail() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = false;

final ResponderThread responderThread = new ResponderThread(testResponder, bais, baos, null);
Expand All @@ -589,7 +643,7 @@ public void testRunResponderWrite() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand All @@ -613,7 +667,7 @@ public void testRunResponderWriteOnSuccess() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand All @@ -637,7 +691,7 @@ public void testRunResponderNoWriteOnFailure() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand All @@ -662,7 +716,7 @@ public void testRunResponderWriteAndResponseSuccess() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand All @@ -687,7 +741,7 @@ public void testRunResponderWriteAndResponseFailure() {
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand All @@ -711,7 +765,7 @@ public void testResultHandlerSuccess(){
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand Down Expand Up @@ -745,7 +799,7 @@ public void testResultHandlerInputFailure(){
testResponder.inputMaxTimeout = 1000;
testResponder.responseMaxLines = 5;
testResponder.responseMaxTimeout = 1000;
testResponder.failOnInputThreshold = true;
testResponder.failOnInputLinesThreshold = true;
testResponder.failOnResponseThreshold = true;
testResponder.inputString = "Test";

Expand Down

0 comments on commit be2b3f8

Please sign in to comment.