Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Q
quasar-network
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
Container 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
Jens Nolte
quasar-network
Commits
ade3d5ac
Commit
ade3d5ac
authored
3 years ago
by
Jens Nolte
Browse files
Options
Downloads
Patches
Plain Diff
Add more tests for observe
parent
d4792b70
No related branches found
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
test/Quasar/NetworkSpec.hs
+50
-3
50 additions, 3 deletions
test/Quasar/NetworkSpec.hs
with
50 additions
and
3 deletions
test/Quasar/NetworkSpec.hs
+
50
−
3
View file @
ade3d5ac
...
...
@@ -10,6 +10,7 @@
module
Quasar.NetworkSpec
where
import
Control.Concurrent
(
threadDelay
)
import
Control.Concurrent.MVar
import
Control.Concurrent.STM
import
Control.Exception
(
toException
)
...
...
@@ -28,7 +29,6 @@ import Test.QuickCheck.Monadic
shouldReturnAsync
::
(
HasCallStack
,
IsAwaitable
r
a
,
Show
r
,
Eq
r
)
=>
AsyncIO
a
->
r
->
AsyncIO
()
action
`
shouldReturnAsync
`
expected
=
action
>>=
await
>>=
liftIO
.
(`
shouldBe
`
expected
)
$
(
makeRpc
$
rpcApi
"Example"
$
do
rpcFunction
"fixedHandler42"
$
do
addArgument
"arg"
[
t
|
Int
|]
...
...
@@ -132,7 +132,24 @@ spec = parallel $ do
setObservableVar
var
13
retrieveIO
observable
`
shouldReturn
`
13
it
"foobar"
$
do
it
"receives the current value when calling observe"
$
do
var
<-
newObservableVar
41
withStandaloneClient
@
ObservableExampleProtocol
(
ObservableExampleProtocolImpl
(
toObservable
var
))
$
\
client
->
do
resultVar
<-
newTVarIO
ObservableLoading
observable
<-
intObservable
client
-- Change the value before calling `observe`
setObservableVar
var
42
void
$
observe
observable
$
atomically
.
writeTVar
resultVar
join
$
atomically
$
readTVar
resultVar
>>=
\
case
ObservableUpdate
x
->
pure
$
x
`
shouldBe
`
42
ObservableLoading
->
retry
ObservableNotAvailable
ex
->
pure
$
throwIO
ex
it
"receives continuous updates when observing"
$
do
var
<-
newObservableVar
42
withStandaloneClient
@
ObservableExampleProtocol
(
ObservableExampleProtocolImpl
(
toObservable
var
))
$
\
client
->
do
resultVar
<-
newTVarIO
ObservableLoading
...
...
@@ -140,7 +157,7 @@ spec = parallel $ do
void
$
observe
observable
$
atomically
.
writeTVar
resultVar
let
latestShouldBe
=
\
expected
->
join
$
atomically
$
readTVar
resultVar
>>=
\
case
--
Network might be slow
, so this retries until the expected value is received.
--
Send and receive are running asynchronously
, so this retries until the expected value is received.
-- Blocks forever if the wrong or no value is received.
ObservableUpdate
x
->
if
(
x
==
expected
)
then
pure
(
pure
()
)
else
retry
ObservableLoading
->
retry
...
...
@@ -153,3 +170,33 @@ spec = parallel $ do
latestShouldBe
(
-
1
)
setObservableVar
var
42
latestShouldBe
42
it
"receives no further updates after disposing the callback registration"
$
do
var
<-
newObservableVar
42
withStandaloneClient
@
ObservableExampleProtocol
(
ObservableExampleProtocolImpl
(
toObservable
var
))
$
\
client
->
do
resultVar
<-
newTVarIO
ObservableLoading
observable
<-
intObservable
client
disposable
<-
observe
observable
$
atomically
.
writeTVar
resultVar
let
latestShouldBe
=
\
expected
->
join
$
atomically
$
readTVar
resultVar
>>=
\
case
-- Send and receive are running asynchronously, so this retries until the expected value is received.
-- Blocks forever if the wrong or no value is received.
ObservableUpdate
x
->
if
(
x
<
0
)
then
pure
(
fail
"received a message after unsubscribing"
)
else
if
(
x
==
expected
)
then
pure
(
pure
()
)
else
retry
ObservableLoading
->
retry
ObservableNotAvailable
ex
->
pure
$
throwIO
ex
latestShouldBe
42
setObservableVar
var
13
latestShouldBe
13
setObservableVar
var
42
latestShouldBe
42
disposeIO
disposable
setObservableVar
var
(
-
1
)
threadDelay
10000
latestShouldBe
42
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