Mentions légales du service
Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
c-client
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
Service Desk
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
concordant
Software
c-client
Commits
03143181
Commit
03143181
authored
4 years ago
by
Yannick Li
Browse files
Options
Downloads
Patches
Plain Diff
Update the CServiceAdapter to the new C-Service using crdtlib v1
parent
578ded06
No related branches found
No related tags found
1 merge request
!6
Resolve "Update to c-crdtlib v1.0.0"
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
src/commonMain/kotlin/client/utils/CServiceAdapter.kt
+24
-4
24 additions, 4 deletions
src/commonMain/kotlin/client/utils/CServiceAdapter.kt
src/commonTest/kotlin/client/utils/CServiceAdapterTest.kt
+25
-8
25 additions, 8 deletions
src/commonTest/kotlin/client/utils/CServiceAdapterTest.kt
with
49 additions
and
12 deletions
src/commonMain/kotlin/client/utils/CServiceAdapter.kt
+
24
−
4
View file @
03143181
...
...
@@ -19,11 +19,15 @@
package
client.utils
import
crdtlib.crdt.
DeltaCRDT
import
crdtlib.crdt.
*
import
io.ktor.client.HttpClient
import
io.ktor.client.request.url
import
io.ktor.client.request.post
import
io.ktor.http.*
import
kotlinx.coroutines.GlobalScope
import
kotlinx.coroutines.async
import
kotlinx.coroutines.delay
import
kotlinx.coroutines.launch
/**
*
...
...
@@ -43,6 +47,7 @@ class CServiceAdapter {
}
client
.
close
()
return
resp
==
"\"OK\""
}
/**
...
...
@@ -115,13 +120,13 @@ class CServiceAdapter {
* @param objectUId CRDT id
* @param crdt new crdt
*/
suspend
fun
updateObject
(
dbName
:
String
,
objectUId
:
CObjectUId
,
crdt
:
DeltaCRDT
):
Boolean
{
suspend
fun
updateObject
(
dbName
:
String
,
myid
:
String
,
crdt
:
DeltaCRDT
):
Boolean
{
val
client
=
HttpClient
()
val
crdtJson
=
crdt
.
toJson
()
val
crdtJson
=
crdt
.
toJson
()
.
replace
(
"\""
,
"\\\""
)
val
resp
=
client
.
post
<
String
>{
url
(
"http://127.0.0.1:4000/api/update-object"
)
contentType
(
ContentType
.
Application
.
Json
)
body
=
"""{"appName":"$dbName","id":"$
objectUId.name
", "document":"$crdtJson"}"""
body
=
"""{"appName":"$dbName","id":"$
myid
", "document":"$crdtJson"}"""
}
client
.
close
()
return
resp
==
"\"OK\""
...
...
@@ -134,5 +139,20 @@ class CServiceAdapter {
suspend
fun
close
(
dbName
:
String
):
Boolean
{
return
true
}
/**
* Delete the database
* @param dbName database name
*/
suspend
fun
delete
(
dbName
:
String
):
Boolean
{
val
client
=
HttpClient
()
val
resp
=
client
.
post
<
String
>
{
url
(
"http://127.0.0.1:4000/api/delete-app"
)
contentType
(
ContentType
.
Application
.
Json
)
body
=
"""{"appName":"$dbName"}"""
}
client
.
close
()
return
resp
==
"\"OK\""
}
}
}
This diff is collapsed.
Click to expand it.
src/commonTest/kotlin/client/utils/CServiceAdapterTest.kt
+
25
−
8
View file @
03143181
...
...
@@ -19,8 +19,13 @@
package
client.utils
import
crdtlib.crdt.PNCounter
import
crdtlib.crdt.DeltaCRDTFactory
import
crdtlib.utils.ClientUId
import
crdtlib.utils.SimpleEnvironment
import
io.kotest.core.spec.style.StringSpec
import
io.kotest.matchers.booleans.shouldBeTrue
import
io.kotest.matchers.shouldBe
import
io.kotest.matchers.string.shouldMatch
import
kotlinx.coroutines.delay
...
...
@@ -28,16 +33,28 @@ class CServiceAdapterTest : StringSpec({
"connect to c-service create, write twice, read and delete"
{
CServiceAdapter
.
connect
(
"myapp"
).
shouldBeTrue
()
CServiceAdapter
.
updateObject
(
"myapp"
,
"myid"
,
"""{\"key\":\"value1\"}"""
)
delay
(
500
)
val
regex1
=
"""\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"key\\\":\\\"value1\\\"\}\"\]"""
.
toRegex
()
delay
(
200
)
val
uid
=
ClientUId
(
"clientid"
)
val
my_env
=
SimpleEnvironment
(
uid
)
val
my_crdt
=
DeltaCRDTFactory
.
createDeltaCRDT
(
"PNCounter"
,
my_env
)
CServiceAdapter
.
updateObject
(
"myapp"
,
"myid"
,
my_crdt
).
shouldBeTrue
()
delay
(
200
)
val
regex1
=
"""\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"type\\\":\\\"PNCounter\\\",\\\"metadata\\\":\{\\\"increment\\\":\[\],\\\"decrement\\\":\[\]\},\\\"value\\\":0\}\"\]"""
.
toRegex
()
CServiceAdapter
.
getObjects
(
"myapp"
).
shouldMatch
(
regex1
)
delay
(
200
)
delay
(
500
)
if
(
my_crdt
is
PNCounter
)
{
my_crdt
.
increment
(
10
)
CServiceAdapter
.
updateObject
(
"myapp"
,
"myid"
,
my_crdt
)
delay
(
200
)
val
regex2
=
"""\[\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"type\\\":\\\"PNCounter\\\",\\\"metadata\\\":\{\\\"increment\\\":\[\{\\\"name\\\":\\\"clientid\\\"},\{\\\"first\\\":10,\\\"second\\\":\{\\\"uid\\\":\{\\\"name\\\":\\\"clientid\\\"\},\\\"cnt\\\":-2147483647\}\}\],\\\"decrement\\\":\[\]\},\\\"value\\\":10\}\"\]"""
.
toRegex
()
CServiceAdapter
.
getObjects
(
"myapp"
).
shouldMatch
(
regex2
)
delay
(
200
)
}
CServiceAdapter
.
updateObject
(
"myapp"
,
"myid"
,
"""{\"key\":\"value2\"}"""
)
delay
(
500
)
val
regex2
=
"""\"\{\\\"_id\\\":\\\"myid\\\",\\\"_rev\\\":\\\"(\d+)-(\w+)\\\",\\\"key\\\":\\\"value2\\\"\}\"""".toRegex()
CServiceAdapter
.
getObject
(
"myapp"
,
"myid"
).
shouldMatch
(
regex2
)
CServiceAdapter
.
delete
(
"myapp"
).
shouldBeTrue
()
delay
(
200
)
}
})
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