Skip to content

Commit

Permalink
Add missing test
Browse files Browse the repository at this point in the history
  • Loading branch information
raararaara committed Nov 19, 2024
1 parent fce25c1 commit 3cf29bf
Showing 1 changed file with 82 additions and 0 deletions.
82 changes: 82 additions & 0 deletions test/integration/gc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,88 @@ func TestGarbageCollection(t *testing.T) {
assert.Equal(t, 0, d2.GarbageLen())
})

t.Run("garbage collection for text type test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
assert.NoError(t, c1.Attach(ctx, d1))
// d1.vv = [c1:1], minvv = [c1:1], db.vv {c1: [c1:1]}
assert.Equal(t, true, checkVV(d1.VersionVector(), versionOf(d1.ActorID(), 1)))

d2 := document.New(helper.TestDocKey(t))
assert.NoError(t, c2.Attach(ctx, d2))
// d2.vv = [c1: 1, c2:2], minvv = [c1: 0, c2:0], db.vv = {c1: [c1:1], c2: [c2:1]}
assert.Equal(t, true, checkVV(d2.VersionVector(), versionOf(d1.ActorID(), 1), versionOf(d2.ActorID(), 2)))

assert.NoError(t,
d1.Update(func(root *json.Object, p *presence.Presence) error {
root.SetNewText("text").
Edit(0, 0, "Hello world")
root.SetNewText("richText").
Edit(0, 0, "Hello world", nil)
return nil
}, "sets test and richText"),
)
// d1.vv = [c1:2]
assert.Equal(t, true, checkVV(d1.VersionVector(), versionOf(d1.ActorID(), 2)))
assert.Equal(t, 0, d1.GarbageLen())
assert.Equal(t, 0, d2.GarbageLen())

assert.NoError(t, c1.Sync(ctx))
// d1.vv = [c1:3, c2:1], minvv = [c1:0, c2:0], db.vv = {c1: [c1:2], c2: [c2:1]}
assert.Equal(t, true, checkVV(d1.VersionVector(), versionOf(d1.ActorID(), 3), versionOf(d2.ActorID(), 1)))

assert.NoError(t, c2.Sync(ctx))
// d2.vv = [c1:2, c2:3], minvv = [c1:1, c2:0], db.vv = {c1: [c1:2], c2: [c1:1 c2:2]}
assert.Equal(t, true, checkVV(d2.VersionVector(), versionOf(d1.ActorID(), 2), versionOf(d2.ActorID(), 3)))

assert.NoError(t,
d2.Update(func(root *json.Object, p *presence.Presence) error {
root.GetText("text").
Edit(0, 1, "a").
Edit(1, 2, "b")
root.GetText("richText").
Edit(0, 1, "a", map[string]string{"b": "1"})
return nil
}, "edit text type elements"),
)
// d2.vv = [c1:2, c2:4]
assert.Equal(t, true, checkVV(d2.VersionVector(), versionOf(d1.ActorID(), 2), versionOf(d2.ActorID(), 4)))
assert.Equal(t, 0, d1.GarbageLen())
assert.Equal(t, 3, d2.GarbageLen())

assert.NoError(t, c2.Sync(ctx))
// d2.vv = [c1:2, c2:4], minvv = [c1:2, c2:0], db.vv = {c1: [c1:2], c2: [c1:2 c2:4]}
assert.Equal(t, true, checkVV(d2.VersionVector(), versionOf(d1.ActorID(), 2), versionOf(d2.ActorID(), 4)))
assert.Equal(t, 0, d1.GarbageLen())
assert.Equal(t, 3, d2.GarbageLen())

assert.NoError(t, c1.Sync(ctx))
// d1.vv = [c1:5, c2:4], minvv = [c1:2, c2:1], db.vv = {c1: [c1:3, c2:1], c2: [c1:2 c2:4]}
assert.Equal(t, true, checkVV(d1.VersionVector(), versionOf(d1.ActorID(), 5), versionOf(d2.ActorID(), 4)))
assert.Equal(t, 3, d1.GarbageLen())
assert.Equal(t, 3, d2.GarbageLen())

assert.NoError(t, c2.Sync(ctx))
// d2.vv = [c1:2, c2:4], minvv = [c1:2, c2:0], db.vv = {c1: [c1:3, c2:1], c2: [c1:2 c2:4]}
assert.Equal(t, true, checkVV(d2.VersionVector(), versionOf(d1.ActorID(), 2), versionOf(d2.ActorID(), 4)))
assert.Equal(t, 3, d1.GarbageLen())
assert.Equal(t, 3, d2.GarbageLen())

assert.NoError(t, c1.Sync(ctx))
// d1.vv = [c1:5, c2:4], minvv = [c1:2, c2:4], db.vv = {c1: [c1:5, c2:4], c2: [c1:2 c2:4]}
assert.Equal(t, true, checkVV(d1.VersionVector(), versionOf(d1.ActorID(), 5), versionOf(d2.ActorID(), 4)))
// node removedAt = 4@c2, minVV[c2] = 4 meet GC condition
assert.Equal(t, 0, d1.GarbageLen())
assert.Equal(t, 3, d2.GarbageLen())

assert.NoError(t, c2.Sync(ctx))
// d2.vv = [c1:2, c2:4], minvv = [c1:2, c2:4], db.vv = {c1: [c1:5, c2:4], c2: [c1:2 c2:4]}
assert.Equal(t, true, checkVV(d2.VersionVector(), versionOf(d1.ActorID(), 2), versionOf(d2.ActorID(), 4)))
assert.Equal(t, 0, d1.GarbageLen())
// node removedAt = 4@c2, minVV[c2] = 4 meet GC condition
assert.Equal(t, 0, d2.GarbageLen())
})

t.Run("garbage collection for tree type test", func(t *testing.T) {
doc := document.New(helper.TestDocKey(t))

Expand Down

0 comments on commit 3cf29bf

Please sign in to comment.