Author: adietish
Date: 2011-09-26 11:55:10 -0400 (Mon, 26 Sep 2011)
New Revision: 35049
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ApplicationLogReader.java
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/ApplicationLogReaderIntegrationTest.java
Log:
[JBIDE-9773] correcting ApplicationLogReader implementation and adding new tests
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ApplicationLogReader.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ApplicationLogReader.java 2011-09-26
15:44:45 UTC (rev 35048)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/core/ApplicationLogReader.java 2011-09-26
15:55:10 UTC (rev 35049)
@@ -32,12 +32,13 @@
private static final Pattern LOG_REGEX = Pattern.compile("Tail of .+$",
Pattern.MULTILINE);
+ private static final long STATUS_REQUEST_DELAY = 4 * 1024;
+
private IOpenshiftService service;
private StringReader logReader;
private Application application;
- private int logIndex = 0;
-
private User user;
+ private String currentStatus;
public ApplicationLogReader(Application application, User user, IOpenshiftService
service) {
this.application = application;
@@ -48,18 +49,41 @@
@Override
public int read(char[] cbuf, int off, int len) throws IOException {
int charactersRead = -1;
+ try {
+ do {
+ charactersRead = readStatus(cbuf, off, len);
+ } while (charactersRead == -1);
+ } catch (InterruptedException e) {
+ throw new IOException(e);
+ }
+ return charactersRead;
+ }
+
+ protected int readStatus(char[] cbuf, int off, int len) throws IOException,
+ InterruptedException {
+ int charactersRead = -1;
if (logReader == null) {
- this.logReader = createLogReader(requestStatus());
+ String status = requestStatus();
+ if (isSameStatus(status, currentStatus)) {
+ Thread.sleep(STATUS_REQUEST_DELAY);
+ return -1;
+ }
+ this.currentStatus = status;
+ this.logReader = createLogReader(status);
}
+
charactersRead = logReader.read(cbuf, off, len);
- if (charactersRead != -1) {
- logIndex += charactersRead;
- return charactersRead;
+ if (charactersRead == -1) {
+ this.logReader = null;
}
- this.logReader = null;
- return -1;
+ return charactersRead;
}
+ private boolean isSameStatus(String status, String currentStatus) {
+ return currentStatus != null
+ && currentStatus.equals(status);
+ }
+
protected StringReader createLogReader(String status) throws IOException {
String log = getLog(status);
return new StringReader(log);
@@ -67,10 +91,11 @@
private String getLog(String status) throws IOException {
Matcher matcher = LOG_REGEX.matcher(status);
+ int logStart = 0;
if (matcher.find()) {
- logIndex = matcher.end() + 1;
+ logStart = matcher.end() + 1;
}
- return status.substring(logIndex);
+ return status.substring(logStart);
}
protected String requestStatus() throws IOException {
Modified:
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/ApplicationLogReaderIntegrationTest.java
===================================================================
---
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/ApplicationLogReaderIntegrationTest.java 2011-09-26
15:44:45 UTC (rev 35048)
+++
trunk/as/tests/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/test/internal/core/ApplicationLogReaderIntegrationTest.java 2011-09-26
15:55:10 UTC (rev 35049)
@@ -23,7 +23,6 @@
import org.jboss.ide.eclipse.as.openshift.core.internal.Application;
import org.jboss.ide.eclipse.as.openshift.core.internal.IOpenshiftService;
import org.jboss.ide.eclipse.as.openshift.core.internal.OpenshiftService;
-import org.jboss.ide.eclipse.as.openshift.core.internal.utils.StreamUtils;
import org.jboss.ide.eclipse.as.openshift.test.internal.core.fakes.TestUser;
import org.jboss.ide.eclipse.as.openshift.test.internal.core.utils.ApplicationUtils;
import org.junit.Before;
@@ -93,18 +92,18 @@
try {
application = user.createTestApplication();
ApplicationLogReader logReader = application.getLog();
- String log = StreamUtils.readToString(logReader);
- System.err.println(log);
LogReaderRunnable logReaderRunnable = new LogReaderRunnable(logReader);
executor = Executors.newSingleThreadExecutor();
executor.submit(logReaderRunnable);
- boolean logAvailable = waitForLog(startTime, System.currentTimeMillis() + TIMEOUT,
logReaderRunnable);
+ boolean logAvailable = waitForNewLogEntries(0, startTime, System.currentTimeMillis() +
TIMEOUT, logReaderRunnable);
+ int logLength = logReaderRunnable.getLog().length();
assertTrue(logReaderRunnable.isRunning());
- assertFalse(logAvailable);
+ assertTrue(logAvailable);
application.restart();
- logAvailable = waitForLog(startTime, System.currentTimeMillis() + TIMEOUT,
logReaderRunnable);
+ logAvailable = waitForNewLogEntries(logLength, startTime, System.currentTimeMillis() +
TIMEOUT, logReaderRunnable);
assertTrue(logAvailable);
assertTrue(logReaderRunnable.isRunning());
+ assertTrue(logReaderRunnable.getLog().length() > logLength);
} finally {
if (executor != null) {
executor.shutdownNow();
@@ -115,13 +114,14 @@
}
}
- protected boolean waitForLog(long startTime, long timeout, LogReaderRunnable
logReaderRunnable)
+ protected boolean waitForNewLogEntries(int logLength, long startTime, long timeout,
LogReaderRunnable logReaderRunnable)
throws InterruptedException {
while (logReaderRunnable.isEmpty()
+ && logReaderRunnable.getLog().length() <= logLength
&& System.currentTimeMillis() <= timeout) {
Thread.sleep(1 * 1024);
}
- return logReaderRunnable.isEmpty();
+ return logReaderRunnable.getLog().length() > logLength;
}
private static class LogReaderRunnable implements Runnable {
Show replies by date