Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Don't wait for output prefix if it is already present
  • Loading branch information
Marek Żarnowski committed Dec 4, 2019
1 parent 6498b4b commit 37c33b7
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
Expand Up @@ -3,13 +3,13 @@ package scala.meta.internal.metals.debug
import scala.collection.mutable
import scala.concurrent.Future
import scala.concurrent.Promise
import scala.meta.internal.metals.debug.DebuggeeOutput.OutputListener
import scala.meta.internal.metals.debug.DebuggeeOutput.PrefixPattern

final class DebuggeeOutput {
private val output = new StringBuilder
@volatile private var listeners = mutable.Buffer.empty[OutputListener]
@volatile private var listeners = mutable.Buffer.empty[PrefixPattern]

def apply(): String = output.toString()
def apply(): String = synchronized(output.toString())

def append(message: String): Unit = synchronized {
output.append(message)
Expand All @@ -18,15 +18,19 @@ final class DebuggeeOutput {
listeners = remaining
}

def awaitPrefix(category: String, prefix: String): Future[Unit] = {
val promise = Promise[Unit]()
listeners.append(new OutputListener(prefix, promise))
promise.future
def awaitPrefix(prefix: String): Future[Unit] = synchronized {
if (output.startsWith(prefix)) {
Future.unit
} else {
val promise = Promise[Unit]()
listeners.append(new PrefixPattern(prefix, promise))
promise.future
}
}
}

object DebuggeeOutput {
protected final class OutputListener(prefix: String, promise: Promise[Unit]) {
protected final class PrefixPattern(prefix: String, promise: Promise[Unit]) {
def matches(output: String): Boolean = {
if (output.startsWith(prefix)) {
promise.success(())
Expand All @@ -35,5 +39,9 @@ object DebuggeeOutput {
false
}
}

def foo(): Unit = {
promise.success(())
}
}
}
Expand Up @@ -60,11 +60,12 @@ final class TestDebugger(connect: RemoteServer.Listener => Debugger)(
import org.eclipse.lsp4j.debug.{OutputEventArgumentsCategory => Category}
ifNotFailed {
output
.awaitPrefix(Category.STDOUT, prefix)
.awaitPrefix(prefix)
.withTimeout(seconds, TimeUnit.SECONDS)
.recoverWith {
case timeout: TimeoutException =>
val error = s"No prefix [$prefix] in [${output()}]"
val error =
s"No prefix [${prefix.getBytes()}] in [${output().getBytes()}]"
Future.failed(new Exception(error, timeout))
}
}
Expand Down

0 comments on commit 37c33b7

Please sign in to comment.