Skip to content
Snippets Groups Projects
Commit 4a4490f0 authored by Clément Pit-Claudel's avatar Clément Pit-Claudel
Browse files

Add simple tests

parent 0d71d6fb
Branches
No related tags found
No related merge requests found
package cs214.webapp
package server
package web
import scala.util.Try
import ujson.Value
import io.undertow.Undertow
import sttp.client4.quick.*
import sttp.model.Uri
import cs214.webapp.server.StateMachine
import cs214.webapp.server.web.WebServer
import scala.language.implicitConversions
given unsafeURI: Conversion[String, Uri] with
def apply(uri: String): Uri = Uri.unsafeParse(uri)
case class WebServerInfo(uri: String):
override def toString = uri
class ServerSuite extends munit.FunSuite:
type Ping = String
type Pong = String
type State = String
object PingPong extends StateMachine[Ping, State, Pong]:
override def appInfo: AppInfo = AppInfo(
id = "stress", name = "Stress test", description = "", year = 2024
)
override object wire extends AppWire[Ping, Pong]:
override val eventFormat = StringWire
override val viewFormat = StringWire
override def init(clients: Seq[UserId]): String = ""
override def project(state: String)(userId: UserId): Pong = state
override def transition(state: String)(userId: UserId, event: Ping): Try[Seq[Action[State]]] = Try:
Seq(Action.Render(state))
WebServer.register(PingPong)
def withServer[T](body: WebServerInfo ?=> T): T =
object caskServer extends cask.Main:
def allRoutes = Seq(WebServerRoutes())
val (address, port) = ("localhost", 8091)
val server = Undertow.builder
.addHttpListener(port, address)
.setHandler(caskServer.defaultHandler)
.build
server.start()
val t =
try body(using WebServerInfo(f"http://${address}:${port}"))
finally server.stop()
t
def time[T](t: => T) =
val start = System.nanoTime
val result = t
val end = System.nanoTime
println(f"Elapsed: ${(end - start) / 1000000} ms")
result
import sttp.client4.quick.*
test("The main and app-selection pages respond to requests"):
withServer: ws ?=>
assert(quickRequest.get(ws.uri).send().code.isSuccess)
assert(quickRequest.get(f"$ws/app/${PingPong.appInfo.id}").send().code.isSuccess)
def createInstance(using ws: WebServerInfo) =
val users = Seq("yak", "hut", "kik")
val js = CreateInstanceRequest.Wire.encode:
CreateInstanceRequest(PingPong.appInfo.id, users)
val jsResponse = quickRequest
.post(f"$ws${Endpoints.Api.createInstance}")
.header("Content-Type", "application/json")
.body(ujson.write(js))
.send()
.body
CreateInstanceResponse.Wire.decode(ujson.read(jsResponse)).get
def instanceInfo(instanceId: String)(using ws: WebServerInfo) =
val resp = InstanceInfoResponse.Wire.decode:
ujson.read:
quickRequest.get(f"$ws${Endpoints.Api.instanceInfo}?instanceId=$instanceId").send().body
resp.get
test("Multiple instances can be created at the same time"):
withServer:
time:
for _ <- 1 to 10 do
val inst = createInstance
instanceInfo(inst.instanceId)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment