From 3cf29bfc3700adba1b5aa17a8c58db62babe20b7 Mon Sep 17 00:00:00 2001 From: raararaara Date: Tue, 19 Nov 2024 17:34:19 +0900 Subject: [PATCH] Add missing test --- test/integration/gc_test.go | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/test/integration/gc_test.go b/test/integration/gc_test.go index 18cc95c52..b1e4978f0 100644 --- a/test/integration/gc_test.go +++ b/test/integration/gc_test.go @@ -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))