Skip to content

Commit

Permalink
Address some FIXMEs:
Browse files Browse the repository at this point in the history
* Change GeekDocWriter from FIXME to TODO since it is just
  pending work.
* Attempt to implement an Aawait.result equivalent for
  JS but there's no way to make it actually wait. The
  asynchronous behavior does run, observed from output.
  • Loading branch information
reidspencer committed Dec 14, 2024
1 parent 4ccc283 commit eaf6952
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ case class GeekDocWriter(
Some(brief),
Map(
"geekdocCollapseSection" -> "true"
// FIXME: "geekdocFilePath" -> s"${generator.makeFilePath(cont).getOrElse("no-such-file")}"
// TODO: "geekdocFilePath" -> s"${generator.makeFilePath(cont).getOrElse("no-such-file")}"
)
)
}
Expand Down
63 changes: 56 additions & 7 deletions utils/js/src/test/scala/com/ossuminc/riddl/utils/LoaderTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,69 @@ package com.ossuminc.riddl.utils
import org.scalatest.matchers.must.Matchers
import org.scalatest.wordspec.AnyWordSpec

import scala.scalajs.concurrent.JSExecutionContext.Implicits.queue
import scala.scalajs.js.timers._

import java.time.Clock
import scala.util.{Failure, Success, Try}
import scala.concurrent.Future
import scala.concurrent.duration.FiniteDuration
import scala.concurrent.duration.DurationInt
import java.util.concurrent.TimeoutException

private case class AwaitFuture[T](
future: Future[T],
duration: FiniteDuration,
onSuccess: T => Unit,
onFailure: Throwable => Unit = (t: Throwable) => throw t,
) {

val start: Long = Clock.systemUTC().millis()
var done: Boolean = false
checkCompleteness
if !done then
setTimeout(0.1) { checkCompleteness }
end if
def checkCompleteness: Unit =
future.value match
case None =>
val now: Long = Clock.systemUTC().millis()
if duration.toMillis < now - start then
throw new TimeoutException(s"Timeout after ${now-start}ms")
else
setTimeout(0.1) { checkCompleteness }
end if
case Some(t: Try[T]) =>
done = true
t match
case Success(result) => onSuccess(result)
case Failure(error) => onFailure(error)
end match
end match
end checkCompleteness
}

class LoaderTest extends AnyWordSpec with Matchers {

def onSuccess(result: String): Unit = {
result must not be (empty)
result must startWith("domain ReactiveBBQ")
println(result.split("\n").head)
}

"Loader" must {
"load" in {
val url = URL(
"https://raw.githubusercontent.com/ossuminc/riddl/language/input/domains/rbbq.riddl"
"https://raw.githubusercontent.com/ossuminc/riddl/refs/heads/main/language/input/domains/rbbq.riddl"
)
val io = DOMPlatformContext()
val future = io.load(url).map[String] { (content: String) =>
content must not be (empty)
content.startsWith("domain ReactiveBBQ") must be(true)
content
}
// FIXME: JS version of Await fails Await.result(future, 5.seconds)
info(s"Loading from: ${url.toExternalForm}")
val future = io.load(url)
AwaitFuture[String](future, 10.seconds, onSuccess)
info(s"Done handling: ${url.toExternalForm}")
}
"run a fake test" in {
pending
}
}
}

0 comments on commit eaf6952

Please sign in to comment.