Skip to content

Commit

Permalink
Refactor object test code
Browse files Browse the repository at this point in the history
  • Loading branch information
highcloud100 committed Jan 12, 2024
1 parent 0cc5001 commit 51de8a1
Showing 1 changed file with 33 additions and 36 deletions.
69 changes: 33 additions & 36 deletions test/integration/object_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -254,43 +254,52 @@ func TestObject(t *testing.T) {
"tree": json.CreateTree(&json.TreeNode{
Type: "doc",
Children: []json.TreeNode{{
Type: "p",
Children: []json.TreeNode{{Type: "text", Value: "ab"}},
}, {
Type: "ng",
Children: []json.TreeNode{
{Type: "note", Children: []json.TreeNode{{Type: "text", Value: "cd"}}},
{Type: "note", Children: []json.TreeNode{{Type: "text", Value: "ef"}}},
},
}, {
Type: "bp",
Children: []json.TreeNode{{Type: "text", Value: "gh"}},
Type: "p", Children: []json.TreeNode{{
Type: "tn", Children: []json.TreeNode{{
Type: "text", Value: "a",
}, {
Type: "text", Value: "b",
}},
}, {
Type: "tn", Children: []json.TreeNode{{
Type: "text", Value: "cd",
}},
}},
}},
}),
}

root.SetNewObject("shape", data)
assert.Equal(t, "<doc><p>ab</p><ng><note>cd</note><note>ef</note></ng><bp>gh</bp></doc>", root.GetObject("shape").GetTree("tree").ToXML())
assert.Equal(t, 18, root.GetObject("shape").GetTree("tree").Len())
root.SetNewObject("obj", data)
assert.Equal(t, `<doc><p><tn>ab</tn><tn>cd</tn></p></doc>`, root.GetObject("obj").GetTree("tree").ToXML())

root.GetObject("obj").GetTree("tree").EditByPath([]int{0, 0, 0}, []int{0, 0, 2}, &json.TreeNode{Type: "text", Value: "gh"}, 0)
assert.Equal(t, `<doc><p><tn>gh</tn><tn>cd</tn></p></doc>`, root.GetObject("obj").GetTree("tree").ToXML())

return nil
})

assert.NoError(t, err)
assert.Equal(t, 2, d1.GarbageLen())
assert.Equal(t, 2, d1.GarbageCollect(time.MaxTicket))
assert.Equal(t, 0, d1.GarbageLen())

err = c1.Sync(ctx)
assert.NoError(t, err)
err = c2.Sync(ctx)
assert.NoError(t, err)

assert.Equal(t, d1.Marshal(), d2.Marshal())

err = d1.Update(func(root *json.Object, p *presence.Presence) error {
root.Delete("shape")
root.Delete("obj")
return nil
})
assert.NoError(t, err)

assert.Equal(t, 2, d1.GarbageLen())
assert.Equal(t, 2, d1.GarbageCollect(time.MaxTicket))
assert.Equal(t, 2, d2.GarbageLen())
assert.Equal(t, 2, d2.GarbageCollect(time.MaxTicket))
})

t.Run("Set new object to json object literal sync test", func(t *testing.T) {
Expand Down Expand Up @@ -391,37 +400,24 @@ func TestObject(t *testing.T) {
err := c1.Attach(ctx, d1)
assert.NoError(t, err)

err = d1.Update(func(root *json.Object, p *presence.Presence) error {
json := map[string]interface{}{
"array": []interface{}{1, 2, 3},
}
root.SetNewObject("obj", json)
root.GetObject("obj").GetArray("array").AddInteger(4, 5)
return nil
})

assert.NoError(t, err)
assert.Equal(t, `{"obj":{"array":[1,2,3,4,5]}}`, d1.Marshal())
})

t.Run("Json object literal array in array type test", func(t *testing.T) {
ctx := context.Background()
d1 := document.New(helper.TestDocKey(t))
err := c1.Attach(ctx, d1)
assert.NoError(t, err)

array2 := []interface{}{7, 8}

err = d1.Update(func(root *json.Object, p *presence.Presence) error {
json := map[string]interface{}{
"array": []interface{}{1, 2, 3, array2},
}
root.SetNewObject("obj", json)
assert.Equal(t, `{"obj":{"array":[1,2,3,[7,8]]}}`, root.Marshal())

root.GetObject("obj").GetArray("array").Delete(3)
assert.Equal(t, `{"obj":{"array":[1,2,3]}}`, root.Marshal())
return nil
})

assert.NoError(t, err)
assert.Equal(t, `{"obj":{"array":[1,2,3,[7,8]]}}`, d1.Marshal())
assert.Equal(t, 3, d1.GarbageLen())
assert.Equal(t, 3, d1.GarbageCollect(time.MaxTicket))

})

t.Run("Json object literal counter type test", func(t *testing.T) {
Expand All @@ -436,11 +432,12 @@ func TestObject(t *testing.T) {
}
root.SetNewObject("obj", json)
root.GetObject("obj").GetCounter("counter").Increase(3)
root.GetObject("obj").GetCounter("counter").Increase(-2)
return nil
})

assert.NoError(t, err)
assert.Equal(t, `{"obj":{"counter":3}}`, d1.Marshal())
assert.Equal(t, `{"obj":{"counter":1}}`, d1.Marshal())
})

t.Run("Json object literal text type test", func(t *testing.T) {
Expand Down

1 comment on commit 51de8a1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: 51de8a1 Previous: a8c58f9 Ratio
BenchmarkDocument/constructor_test - ns/op 1453 ns/op 1457 ns/op 1.00
BenchmarkDocument/constructor_test - B/op 1224 B/op 1224 B/op 1
BenchmarkDocument/constructor_test - allocs/op 21 allocs/op 21 allocs/op 1
BenchmarkDocument/status_test - ns/op 846.2 ns/op 849.5 ns/op 1.00
BenchmarkDocument/status_test - B/op 1192 B/op 1192 B/op 1
BenchmarkDocument/status_test - allocs/op 19 allocs/op 19 allocs/op 1
BenchmarkDocument/equals_test - ns/op 7354 ns/op 7524 ns/op 0.98
BenchmarkDocument/equals_test - B/op 6977 B/op 6977 B/op 1
BenchmarkDocument/equals_test - allocs/op 124 allocs/op 124 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 16559 ns/op 18601 ns/op 0.89
BenchmarkDocument/nested_update_test - B/op 12059 B/op 12059 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 260 allocs/op 260 allocs/op 1
BenchmarkDocument/delete_test - ns/op 22361 ns/op 22335 ns/op 1.00
BenchmarkDocument/delete_test - B/op 15284 B/op 15284 B/op 1
BenchmarkDocument/delete_test - allocs/op 339 allocs/op 339 allocs/op 1
BenchmarkDocument/object_test - ns/op 8529 ns/op 8516 ns/op 1.00
BenchmarkDocument/object_test - B/op 6753 B/op 6753 B/op 1
BenchmarkDocument/object_test - allocs/op 118 allocs/op 118 allocs/op 1
BenchmarkDocument/array_test - ns/op 32595 ns/op 28781 ns/op 1.13
BenchmarkDocument/array_test - B/op 11883 B/op 11883 B/op 1
BenchmarkDocument/array_test - allocs/op 274 allocs/op 274 allocs/op 1
BenchmarkDocument/text_test - ns/op 30696 ns/op 30487 ns/op 1.01
BenchmarkDocument/text_test - B/op 14915 B/op 14828 B/op 1.01
BenchmarkDocument/text_test - allocs/op 470 allocs/op 470 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 29149 ns/op 28865 ns/op 1.01
BenchmarkDocument/text_composition_test - B/op 18430 B/op 18310 B/op 1.01
BenchmarkDocument/text_composition_test - allocs/op 479 allocs/op 479 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 79716 ns/op 80677 ns/op 0.99
BenchmarkDocument/rich_text_test - B/op 38677 B/op 38572 B/op 1.00
BenchmarkDocument/rich_text_test - allocs/op 1149 allocs/op 1149 allocs/op 1
BenchmarkDocument/counter_test - ns/op 16802 ns/op 16759 ns/op 1.00
BenchmarkDocument/counter_test - B/op 10466 B/op 10242 B/op 1.02
BenchmarkDocument/counter_test - allocs/op 238 allocs/op 238 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 2877688 ns/op 2887897 ns/op 1.00
BenchmarkDocument/text_edit_gc_100 - B/op 1658644 B/op 1655169 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17095 allocs/op 17094 allocs/op 1.00
BenchmarkDocument/text_edit_gc_1000 - ns/op 229161125 ns/op 229415441 ns/op 1.00
BenchmarkDocument/text_edit_gc_1000 - B/op 144383667 B/op 144344868 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 200939 allocs/op 200908 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 3386490 ns/op 3374708 ns/op 1.00
BenchmarkDocument/text_split_gc_100 - B/op 2316845 B/op 2313351 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16197 allocs/op 16195 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 288024726 ns/op 287430465 ns/op 1.00
BenchmarkDocument/text_split_gc_1000 - B/op 228928076 B/op 228891160 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 203986 allocs/op 203934 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 10668331 ns/op 10779697 ns/op 0.99
BenchmarkDocument/text_delete_all_10000 - B/op 5810777 B/op 5809238 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 40676 allocs/op 40669 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 201080688 ns/op 188925896 ns/op 1.06
BenchmarkDocument/text_delete_all_100000 - B/op 81912650 B/op 81910706 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 411684 allocs/op 411662 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 231739 ns/op 229354 ns/op 1.01
BenchmarkDocument/text_100 - B/op 120139 B/op 118514 B/op 1.01
BenchmarkDocument/text_100 - allocs/op 5082 allocs/op 5082 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 2481464 ns/op 2502759 ns/op 0.99
BenchmarkDocument/text_1000 - B/op 1169127 B/op 1153102 B/op 1.01
BenchmarkDocument/text_1000 - allocs/op 50086 allocs/op 50086 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1266017 ns/op 1262770 ns/op 1.00
BenchmarkDocument/array_1000 - B/op 1091404 B/op 1091147 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11830 allocs/op 11829 allocs/op 1.00
BenchmarkDocument/array_10000 - ns/op 13527262 ns/op 13221008 ns/op 1.02
BenchmarkDocument/array_10000 - B/op 9800227 B/op 9799195 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120295 allocs/op 120291 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 155852 ns/op 172372 ns/op 0.90
BenchmarkDocument/array_gc_100 - B/op 132654 B/op 139899 B/op 0.95
BenchmarkDocument/array_gc_100 - allocs/op 1258 allocs/op 1472 allocs/op 0.85
BenchmarkDocument/array_gc_1000 - ns/op 1444819 ns/op 1651513 ns/op 0.87
BenchmarkDocument/array_gc_1000 - B/op 1159107 B/op 1241466 B/op 0.93
BenchmarkDocument/array_gc_1000 - allocs/op 12875 allocs/op 14897 allocs/op 0.86
BenchmarkDocument/counter_1000 - ns/op 208575 ns/op 210894 ns/op 0.99
BenchmarkDocument/counter_1000 - B/op 192917 B/op 192884 B/op 1.00
BenchmarkDocument/counter_1000 - allocs/op 5767 allocs/op 5767 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 2190703 ns/op 2217464 ns/op 0.99
BenchmarkDocument/counter_10000 - B/op 2087847 B/op 2087814 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 59774 allocs/op 59774 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 1441140 ns/op 1442631 ns/op 1.00
BenchmarkDocument/object_1000 - B/op 1428153 B/op 1428083 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9847 allocs/op 9847 allocs/op 1
BenchmarkDocument/object_10000 - ns/op 15411863 ns/op 15149865 ns/op 1.02
BenchmarkDocument/object_10000 - B/op 12167330 B/op 12166338 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 100564 allocs/op 100562 allocs/op 1.00
BenchmarkDocument/tree_100 - ns/op 1064064 ns/op 1068412 ns/op 1.00
BenchmarkDocument/tree_100 - B/op 943709 B/op 943709 B/op 1
BenchmarkDocument/tree_100 - allocs/op 6101 allocs/op 6101 allocs/op 1
BenchmarkDocument/tree_1000 - ns/op 77858790 ns/op 79029252 ns/op 0.99
BenchmarkDocument/tree_1000 - B/op 86461029 B/op 86460602 B/op 1.00
BenchmarkDocument/tree_1000 - allocs/op 60116 allocs/op 60116 allocs/op 1
BenchmarkDocument/tree_10000 - ns/op 9638231981 ns/op 9661149264 ns/op 1.00
BenchmarkDocument/tree_10000 - B/op 8580988904 B/op 8580973784 B/op 1.00
BenchmarkDocument/tree_10000 - allocs/op 600222 allocs/op 600230 allocs/op 1.00
BenchmarkDocument/tree_delete_all_1000 - ns/op 77402370 ns/op 79816852 ns/op 0.97
BenchmarkDocument/tree_delete_all_1000 - B/op 86990770 B/op 86990889 B/op 1.00
BenchmarkDocument/tree_delete_all_1000 - allocs/op 67753 allocs/op 67750 allocs/op 1.00
BenchmarkDocument/tree_edit_gc_100 - ns/op 3791862 ns/op 3866691 ns/op 0.98
BenchmarkDocument/tree_edit_gc_100 - B/op 4120990 B/op 4121023 B/op 1.00
BenchmarkDocument/tree_edit_gc_100 - allocs/op 14358 allocs/op 14358 allocs/op 1
BenchmarkDocument/tree_edit_gc_1000 - ns/op 324337399 ns/op 326051394 ns/op 0.99
BenchmarkDocument/tree_edit_gc_1000 - B/op 383467850 B/op 383466086 B/op 1.00
BenchmarkDocument/tree_edit_gc_1000 - allocs/op 145426 allocs/op 145407 allocs/op 1.00
BenchmarkDocument/tree_split_gc_100 - ns/op 2635401 ns/op 2611696 ns/op 1.01
BenchmarkDocument/tree_split_gc_100 - B/op 2386901 B/op 2386898 B/op 1.00
BenchmarkDocument/tree_split_gc_100 - allocs/op 10343 allocs/op 10343 allocs/op 1
BenchmarkDocument/tree_split_gc_1000 - ns/op 195660384 ns/op 196739556 ns/op 0.99
BenchmarkDocument/tree_split_gc_1000 - B/op 221991973 B/op 221991590 B/op 1.00
BenchmarkDocument/tree_split_gc_1000 - allocs/op 112265 allocs/op 112260 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 359361756 ns/op 356144469 ns/op 1.01
BenchmarkRPC/client_to_server - B/op 18084576 B/op 17801216 B/op 1.02
BenchmarkRPC/client_to_server - allocs/op 166922 allocs/op 166911 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 617218707 ns/op 613079232 ns/op 1.01
BenchmarkRPC/client_to_client_via_server - B/op 32113888 B/op 31722656 B/op 1.01
BenchmarkRPC/client_to_client_via_server - allocs/op 312956 allocs/op 313048 allocs/op 1.00
BenchmarkRPC/attach_large_document - ns/op 1181973399 ns/op 1479934941 ns/op 0.80
BenchmarkRPC/attach_large_document - B/op 1869048128 B/op 1890029520 B/op 0.99
BenchmarkRPC/attach_large_document - allocs/op 7521 allocs/op 7567 allocs/op 0.99
BenchmarkRPC/adminCli_to_server - ns/op 539911585 ns/op 537051132 ns/op 1.01
BenchmarkRPC/adminCli_to_server - B/op 35976176 B/op 36806452 B/op 0.98
BenchmarkRPC/adminCli_to_server - allocs/op 289641 allocs/op 289659 allocs/op 1.00
BenchmarkLocker - ns/op 64.87 ns/op 65.34 ns/op 0.99
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 38.3 ns/op 38.48 ns/op 1.00
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 146.9 ns/op 145.1 ns/op 1.01
BenchmarkLockerMoreKeys - B/op 15 B/op 15 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkChange/Push_10_Changes - ns/op 3759414 ns/op 3768343 ns/op 1.00
BenchmarkChange/Push_10_Changes - B/op 126494 B/op 126223 B/op 1.00
BenchmarkChange/Push_10_Changes - allocs/op 1254 allocs/op 1254 allocs/op 1
BenchmarkChange/Push_100_Changes - ns/op 13950135 ns/op 14005221 ns/op 1.00
BenchmarkChange/Push_100_Changes - B/op 650682 B/op 648654 B/op 1.00
BenchmarkChange/Push_100_Changes - allocs/op 6541 allocs/op 6539 allocs/op 1.00
BenchmarkChange/Push_1000_Changes - ns/op 112552490 ns/op 114047673 ns/op 0.99
BenchmarkChange/Push_1000_Changes - B/op 6107218 B/op 6036337 B/op 1.01
BenchmarkChange/Push_1000_Changes - allocs/op 62162 allocs/op 62157 allocs/op 1.00
BenchmarkChange/Pull_10_Changes - ns/op 2856278 ns/op 2839363 ns/op 1.01
BenchmarkChange/Pull_10_Changes - B/op 100821 B/op 100881 B/op 1.00
BenchmarkChange/Pull_10_Changes - allocs/op 952 allocs/op 952 allocs/op 1
BenchmarkChange/Pull_100_Changes - ns/op 4339051 ns/op 4308547 ns/op 1.01
BenchmarkChange/Pull_100_Changes - B/op 258286 B/op 258255 B/op 1.00
BenchmarkChange/Pull_100_Changes - allocs/op 3155 allocs/op 3154 allocs/op 1.00
BenchmarkChange/Pull_1000_Changes - ns/op 8530188 ns/op 8372656 ns/op 1.02
BenchmarkChange/Pull_1000_Changes - B/op 1394750 B/op 1396187 B/op 1.00
BenchmarkChange/Pull_1000_Changes - allocs/op 26869 allocs/op 26871 allocs/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - ns/op 16870714 ns/op 16777319 ns/op 1.01
BenchmarkSnapshot/Push_3KB_snapshot - B/op 811707 B/op 809995 B/op 1.00
BenchmarkSnapshot/Push_3KB_snapshot - allocs/op 6546 allocs/op 6542 allocs/op 1.00
BenchmarkSnapshot/Push_30KB_snapshot - ns/op 117171779 ns/op 117069671 ns/op 1.00
BenchmarkSnapshot/Push_30KB_snapshot - B/op 6299798 B/op 6250016 B/op 1.01
BenchmarkSnapshot/Push_30KB_snapshot - allocs/op 62173 allocs/op 62161 allocs/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - ns/op 6605273 ns/op 6563310 ns/op 1.01
BenchmarkSnapshot/Pull_3KB_snapshot - B/op 904907 B/op 905174 B/op 1.00
BenchmarkSnapshot/Pull_3KB_snapshot - allocs/op 14883 allocs/op 14882 allocs/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - ns/op 15279855 ns/op 15000336 ns/op 1.02
BenchmarkSnapshot/Pull_30KB_snapshot - B/op 6981162 B/op 6977754 B/op 1.00
BenchmarkSnapshot/Pull_30KB_snapshot - allocs/op 144149 allocs/op 144148 allocs/op 1.00
BenchmarkSync/memory_sync_10_test - ns/op 6772 ns/op 6824 ns/op 0.99
BenchmarkSync/memory_sync_10_test - B/op 1286 B/op 1286 B/op 1
BenchmarkSync/memory_sync_10_test - allocs/op 38 allocs/op 38 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 51901 ns/op 51635 ns/op 1.01
BenchmarkSync/memory_sync_100_test - B/op 8647 B/op 8659 B/op 1.00
BenchmarkSync/memory_sync_100_test - allocs/op 273 allocs/op 274 allocs/op 1.00
BenchmarkSync/memory_sync_1000_test - ns/op 588222 ns/op 582724 ns/op 1.01
BenchmarkSync/memory_sync_1000_test - B/op 74564 B/op 74925 B/op 1.00
BenchmarkSync/memory_sync_1000_test - allocs/op 2120 allocs/op 2141 allocs/op 0.99
BenchmarkSync/memory_sync_10000_test - ns/op 7161475 ns/op 7320373 ns/op 0.98
BenchmarkSync/memory_sync_10000_test - B/op 762962 B/op 761425 B/op 1.00
BenchmarkSync/memory_sync_10000_test - allocs/op 20545 allocs/op 20593 allocs/op 1.00
BenchmarkTextEditing - ns/op 19264697164 ns/op 18585017456 ns/op 1.04
BenchmarkTextEditing - B/op 9042114360 B/op 9037385240 B/op 1.00
BenchmarkTextEditing - allocs/op 19923659 allocs/op 19920435 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.