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
Francesco Gramegna
webapp-lib
Commits
4a4490f0
Commit
4a4490f0
authored
6 months ago
by
Clément Pit-Claudel
Browse files
Options
Downloads
Patches
Plain Diff
Add simple tests
parent
0d71d6fb
Branches
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
jvm/src/test/scala/cs214/webapp/ServerSuite.scala
+95
-0
95 additions, 0 deletions
jvm/src/test/scala/cs214/webapp/ServerSuite.scala
with
95 additions
and
0 deletions
jvm/src/test/scala/cs214/webapp/ServerSuite.scala
0 → 100644
+
95
−
0
View file @
4a4490f0
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
)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment