Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
webapp-lib
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
CS-214
ul2024
webapp-lib
Merge requests
!32
Add simple tests
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Add simple tests
cpc/tests
into
main
Overview
6
Commits
2
Pipelines
0
Changes
1
Merged
Clément Pit-Claudel
requested to merge
cpc/tests
into
main
6 months ago
Overview
6
Commits
2
Pipelines
0
Changes
1
Expand
0
0
Merge request reports
Compare
main
version 1
e76fb78d
6 months ago
main (base)
and
latest version
latest version
df982193
2 commits,
6 months ago
version 1
e76fb78d
1 commit,
6 months ago
1 file
+
97
−
0
Inline
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
jvm/src/test/scala/cs214/webapp/ServerSuite.scala
0 → 100644
+
97
−
0
Options
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
object
ServerSuite
:
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
class
ServerSuite
extends
munit
.
FunSuite
:
import
sttp.client4.quick.*
import
ServerSuite.*
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
)
Loading