matcheventsitem.java
来自「SRI international 发布的OAA框架软件」· Java 代码 · 共 147 行
JAVA
147 行
/*
#=========================================================================
# Copyright 2003 SRI International. All rights reserved.
#
# The material contained in this file is confidential and proprietary to SRI
# International and may not be reproduced, published, or disclosed to others
# without authorization from SRI International.
#
# DISCLAIMER OF WARRANTIES
#
# SRI International MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE
# SUITABILITY OF THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
# LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
# PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SRI International SHALL NOT BE
# LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING
# OR DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES
#=========================================================================
Author : shardt
Date: Aug 11, 2003
*/
package com.sri.oaa2.tools.oaatest;
import java.util.*;
import junit.framework.*;
abstract class MatchEventsItem extends TestItem {
/** @param timeout Value in milliseconds, pass in 0 to use default */
protected MatchEventsItem(int timeout) {
if (timeout <= 0) {
throw new RuntimeException("Should have caught bad timeout value in parser.");
}
this.timeout = timeout;
}
abstract protected boolean matchEvent(Event event);
interface DescribeCallback {
void event(String e);
}
abstract protected void describe(DescribeCallback cb);
/** Read events off eventQ until empty or match found. Return whether a match
* was found. Assumes that we have already grabbed the lock on eventQ.
*/
private boolean matchEvents(List eventQ) {
while (eventQ.size() > 0) {
Event event = (Event)eventQ.remove(0);
if (matchEvent(event)) {
return true;
}
}
return false;
}
void runTest() {
// Make description string for the log.
// Optimization: Don't run this code if logging turned off.
final StringBuffer descBuf = new StringBuffer("Waiting for: ");
describe(new DescribeCallback() {
boolean first = true;
public void event(String s) {
if (first) {
first = false;
}
else {
descBuf.append(" AND\n ");
}
descBuf.append(s);
}
});
Log.singleton().info(descBuf.toString());
List eventQ = OaaConnector.get().getEventQ();
// Pull events off the queue until hit one that matches or timeout is reached.
// Grab lock here to make sure we don't miss any notification between remove()ing
// the event and wait()ing for the next event to come in.
synchronized (eventQ) {
// Check all events currently in queue before starting timer.
if (matchEvents(eventQ)) {
// Success
return;
}
// Record start time for timeout.
long startTime = System.currentTimeMillis();
long timeRemaining;
while ((timeRemaining = startTime + timeout - System.currentTimeMillis()) > 0) {
try {
eventQ.wait(timeRemaining);
// If we received a notification, there should be some events in the
// queue, and matchEvents() may succeed.
// If we get here because the time expired (not from a
// notification), eventQ will be empty, matchEvents() will fail, and
// the while loop should exit.
if (matchEvents(eventQ)) {
Log.singleton().info("Matched.");
return;
}
}
catch (InterruptedException ex) {
// If we're interrupted, just try it again.
}
}
}
// Make description string for JUnit failure exception.
final StringBuffer failBuf =
new StringBuffer("After " + timeout + " ms, could not match: ");
describe(new DescribeCallback() {
boolean first = true;
public void event(String s) {
if (first) {
first = false;
}
else {
failBuf.append(" AND ");
}
failBuf.append(s);
}
});
// If we get here, we couldn't find a matching event in the given time.
Log.singleton().info("No match after " + timeout + " ms");
Assert.fail(failBuf.toString());
}
int getTimeout() {
return timeout;
}
protected void setTimeout(int val) {
timeout = val;
}
// How long to wait before declaring failure, milliseconds.
private int timeout;
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?