Skip to content

Commit

Permalink
Clean up codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Mar 17, 2023
1 parent b0c1b84 commit 9a2dc46
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 70 deletions.
14 changes: 0 additions & 14 deletions server/documents/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,20 +192,6 @@ func FindDocInfo(
return be.DB.FindDocInfoByID(ctx, project.ID, docID)
}

// CheckDocInProject checks if the document is in the project.
func CheckDocInProject(
ctx context.Context,
be *backend.Backend,
project *types.Project,
docID types.ID,
) error {
if _, err := FindDocInfo(ctx, be, project, docID); err != nil {
return err
}

return nil
}

// FindDocInfoByKeyAndOwner returns a document for the given document key. If
// createDocIfNotExist is true, it creates a new document if it does not exist.
func FindDocInfoByKeyAndOwner(
Expand Down
3 changes: 1 addition & 2 deletions server/rpc/yorkie_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ func (s *yorkieServer) WatchDocument(
return err
}

subscription, peersMap, err := s.watchDoc(stream.Context(), *cli, docID, docInfo.Key)
subscription, peersMap, err := s.watchDoc(stream.Context(), *cli, docID)
if err != nil {
logging.From(stream.Context()).Error(err)
return err
Expand Down Expand Up @@ -578,7 +578,6 @@ func (s *yorkieServer) watchDoc(
ctx context.Context,
client types.Client,
documentID types.ID,
documentKey key.Key,
) (*sync.Subscription, []types.Client, error) {
subscription, peers, err := s.backend.Coordinator.Subscribe(
ctx,
Expand Down
79 changes: 25 additions & 54 deletions test/integration/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import (
"github.com/yorkie-team/yorkie/pkg/document"
"github.com/yorkie-team/yorkie/pkg/document/json"
"github.com/yorkie-team/yorkie/pkg/document/key"
"github.com/yorkie-team/yorkie/server"
"github.com/yorkie-team/yorkie/test/helper"
)

Expand Down Expand Up @@ -364,61 +363,48 @@ func TestDocument(t *testing.T) {
})
}

func TestDocumentWithProject(t *testing.T) {
svr, err := server.New(helper.TestConfig())
assert.NoError(t, err)
assert.NoError(t, svr.Start())
defer func() { assert.NoError(t, svr.Shutdown(true)) }()

adminCli := helper.CreateAdminCli(t, svr.AdminAddr())
func TestDocumentWithProjects(t *testing.T) {
ctx := context.Background()
adminCli := helper.CreateAdminCli(t, defaultServer.AdminAddr())
defer func() { assert.NoError(t, adminCli.Close()) }()

project1, err := adminCli.CreateProject(context.Background(), "project1")
project1, err := adminCli.CreateProject(ctx, "project1")
assert.NoError(t, err)

project2, err := adminCli.CreateProject(context.Background(), "project2")
project2, err := adminCli.CreateProject(ctx, "project2")
assert.NoError(t, err)

t.Run("watch document with different project test", func(t *testing.T) {
t.Run("watch document with different projects test", func(t *testing.T) {
ctx := context.Background()

c1, err := client.Dial(
svr.RPCAddr(),
client.WithAPIKey(project1.PublicKey),
)
// 01. c1 and c2 are in the same project and c3 is in another project.
c1, err := client.Dial(defaultServer.RPCAddr(), client.WithAPIKey(project1.PublicKey))
assert.NoError(t, err)
defer func() { assert.NoError(t, c1.Close()) }()
assert.NoError(t, c1.Activate(ctx))
defer func() { assert.NoError(t, c1.Deactivate(ctx)) }()

c2, err := client.Dial(
svr.RPCAddr(),
client.WithAPIKey(project1.PublicKey),
)
c2, err := client.Dial(defaultServer.RPCAddr(), client.WithAPIKey(project1.PublicKey))
assert.NoError(t, err)
defer func() { assert.NoError(t, c2.Close()) }()
assert.NoError(t, c2.Activate(ctx))
defer func() { assert.NoError(t, c2.Deactivate(ctx)) }()

c3, err := client.Dial(
svr.RPCAddr(),
client.WithAPIKey(project2.PublicKey),
)
c3, err := client.Dial(defaultServer.RPCAddr(), client.WithAPIKey(project2.PublicKey))
assert.NoError(t, err)
defer func() { assert.NoError(t, c3.Close()) }()
assert.NoError(t, c3.Activate(ctx))
defer func() { assert.NoError(t, c3.Deactivate(ctx)) }()

d1 := document.New(key.Key(helper.TestDocKey(t)))
err = c1.Attach(ctx, d1)
assert.NoError(t, err)

// 02. c1 and c2 watch the same document and c3 watches another document but the same key.
var expected []watchResponsePair
var responsePairs []watchResponsePair
wg := sync.WaitGroup{}
wg.Add(1)

// 01. cli1 watches doc1.
d1 := document.New(key.Key(helper.TestDocKey(t)))
err = c1.Attach(ctx, d1)
assert.NoError(t, err)
watch1Ctx, cancel1 := context.WithCancel(ctx)
defer cancel1()
rch, err := c1.Watch(watch1Ctx, d1)
Expand Down Expand Up @@ -454,60 +440,45 @@ func TestDocumentWithProject(t *testing.T) {
}
}()

// 02. cli2 watches doc2.
// c2 watches the same document, so c1 receives a peers changed event.
expected = append(expected, watchResponsePair{
Type: client.PeersChanged,
Peers: map[string]types.Presence{
c1.ID().String(): nil,
c2.ID().String(): nil,
},
})

d2 := document.New(key.Key(helper.TestDocKey(t)))
err = c2.Attach(ctx, d2)
assert.NoError(t, err)

assert.NoError(t, c2.Attach(ctx, d2))
watch2Ctx, cancel2 := context.WithCancel(ctx)
defer cancel2()
_, err = c2.Watch(watch2Ctx, d2)
assert.NoError(t, err)

// 03. cli2 updates doc2.
// c2 updates the document, so c1 receives a documents changed event.
expected = append(expected, watchResponsePair{
Type: client.DocumentsChanged,
})
err = d2.Update(func(root *json.Object) error {
assert.NoError(t, d2.Update(func(root *json.Object) error {
root.SetString("key", "value")
return nil
})
assert.NoError(t, err)

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

// 04. cli3 watches doc3.
// Although the keys of the documents are the same,
// events should not be sent because the projects are different.
// d3 is in another project, so c1 and c2 should not receive events.
d3 := document.New(key.Key(helper.TestDocKey(t)))
err = c3.Attach(ctx, d3)
assert.NoError(t, err)

assert.NoError(t, c3.Attach(ctx, d3))
watch3Ctx, cancel3 := context.WithCancel(ctx)
defer cancel3()
_, err = c3.Watch(watch3Ctx, d3)
assert.NoError(t, err)

// 05. cli3 updates doc3.
err = d3.Update(func(root *json.Object) error {
assert.NoError(t, d3.Update(func(root *json.Object) error {
root.SetString("key3", "value3")
return nil
})
assert.NoError(t, err)

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

wg.Wait()

assert.Equal(t, expected, responsePairs)
assert.Equal(t, "{\"key\":\"value\"}", d1.Marshal())
assert.Equal(t, "{\"key\":\"value\"}", d2.Marshal())
Expand Down

1 comment on commit 9a2dc46

@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: 9a2dc46 Previous: 57d784c Ratio
BenchmarkDocument/constructor_test 1245 ns/op 752 B/op 12 allocs/op 1340 ns/op 752 B/op 12 allocs/op 0.93
BenchmarkDocument/status_test 625.1 ns/op 720 B/op 10 allocs/op 712.7 ns/op 720 B/op 10 allocs/op 0.88
BenchmarkDocument/equals_test 7475 ns/op 5072 B/op 85 allocs/op 7713 ns/op 5072 B/op 85 allocs/op 0.97
BenchmarkDocument/nested_update_test 21193 ns/op 11033 B/op 235 allocs/op 22351 ns/op 11033 B/op 235 allocs/op 0.95
BenchmarkDocument/delete_test 32231 ns/op 14162 B/op 310 allocs/op 29943 ns/op 14162 B/op 310 allocs/op 1.08
BenchmarkDocument/object_test 9853 ns/op 5792 B/op 97 allocs/op 10310 ns/op 5792 B/op 97 allocs/op 0.96
BenchmarkDocument/array_test 34788 ns/op 10889 B/op 251 allocs/op 38517 ns/op 10889 B/op 251 allocs/op 0.90
BenchmarkDocument/text_test 37834 ns/op 14058 B/op 456 allocs/op 41513 ns/op 14058 B/op 456 allocs/op 0.91
BenchmarkDocument/text_composition_test 37707 ns/op 17538 B/op 461 allocs/op 40381 ns/op 17538 B/op 461 allocs/op 0.93
BenchmarkDocument/rich_text_test 98904 ns/op 36005 B/op 1108 allocs/op 105974 ns/op 36014 B/op 1108 allocs/op 0.93
BenchmarkDocument/counter_test 20069 ns/op 9057 B/op 212 allocs/op 21176 ns/op 9057 B/op 212 allocs/op 0.95
BenchmarkDocument/text_edit_gc_100 3988970 ns/op 1552790 B/op 17147 allocs/op 4235403 ns/op 1552772 B/op 17148 allocs/op 0.94
BenchmarkDocument/text_edit_gc_1000 314124009 ns/op 136643044 B/op 210733 allocs/op 345600036 ns/op 136649740 B/op 210758 allocs/op 0.91
BenchmarkDocument/text_split_gc_100 4642166 ns/op 2217259 B/op 16576 allocs/op 5017804 ns/op 2217295 B/op 16577 allocs/op 0.93
BenchmarkDocument/text_split_gc_1000 373537231 ns/op 214879954 B/op 211531 allocs/op 403719911 ns/op 214863165 B/op 211438 allocs/op 0.93
BenchmarkDocument/text_delete_all_10000 15878681 ns/op 5904498 B/op 41122 allocs/op 20215492 ns/op 5904946 B/op 41126 allocs/op 0.79
BenchmarkDocument/text_delete_all_100000 188104115 ns/op 53827346 B/op 415922 allocs/op 256564675 ns/op 53914644 B/op 416114 allocs/op 0.73
BenchmarkDocument/text_100 299698 ns/op 117746 B/op 5064 allocs/op 329749 ns/op 117749 B/op 5064 allocs/op 0.91
BenchmarkDocument/text_1000 3247988 ns/op 1152348 B/op 50068 allocs/op 3541237 ns/op 1152356 B/op 50068 allocs/op 0.92
BenchmarkDocument/array_1000 1712751 ns/op 1102148 B/op 11854 allocs/op 1873575 ns/op 1102022 B/op 11854 allocs/op 0.91
BenchmarkDocument/array_10000 18606468 ns/op 9907452 B/op 120711 allocs/op 20216385 ns/op 9906585 B/op 120707 allocs/op 0.92
BenchmarkDocument/array_gc_100 177143 ns/op 97407 B/op 1226 allocs/op 188009 ns/op 97404 B/op 1226 allocs/op 0.94
BenchmarkDocument/array_gc_1000 1934911 ns/op 1169629 B/op 12889 allocs/op 2039826 ns/op 1169787 B/op 12890 allocs/op 0.95
BenchmarkDocument/counter_1000 270866 ns/op 197876 B/op 6490 allocs/op 301226 ns/op 197879 B/op 6490 allocs/op 0.90
BenchmarkDocument/counter_10000 2867668 ns/op 2164796 B/op 69497 allocs/op 3428664 ns/op 2164800 B/op 69497 allocs/op 0.84
BenchmarkDocument/object_1000 1831660 ns/op 1450552 B/op 9902 allocs/op 2012194 ns/op 1450576 B/op 9902 allocs/op 0.91
BenchmarkDocument/object_10000 20381911 ns/op 12370972 B/op 101214 allocs/op 24305701 ns/op 12368628 B/op 101206 allocs/op 0.84
BenchmarkRPC/client_to_server 741782608 ns/op 24737656 B/op 414665 allocs/op 951991694 ns/op 24700956 B/op 414215 allocs/op 0.78
BenchmarkRPC/client_to_client_via_server 4968251317 ns/op 44558872 B/op 720589 allocs/op 4991291393 ns/op 44390144 B/op 718098 allocs/op 1.00
BenchmarkRPC/attach_large_document 1546574895 ns/op 2125672176 B/op 13730 allocs/op 1733731725 ns/op 2125791336 B/op 13951 allocs/op 0.89
BenchmarkRPC/adminCli_to_server 519146836 ns/op 19075304 B/op 315184 allocs/op 605932650 ns/op 19060772 B/op 314965 allocs/op 0.86
BenchmarkLocker 113 ns/op 16 B/op 1 allocs/op 135.9 ns/op 16 B/op 1 allocs/op 0.83
BenchmarkLockerParallel 124.6 ns/op 0 B/op 0 allocs/op 166.8 ns/op 0 B/op 0 allocs/op 0.75
BenchmarkLockerMoreKeys 367.1 ns/op 14 B/op 0 allocs/op 357.7 ns/op 14 B/op 0 allocs/op 1.03
BenchmarkSync/memory_sync_10_test 7239 ns/op 1341 B/op 39 allocs/op 7992 ns/op 1339 B/op 39 allocs/op 0.91
BenchmarkSync/memory_sync_100_test 57607 ns/op 9105 B/op 299 allocs/op 74876 ns/op 8772 B/op 279 allocs/op 0.77
BenchmarkSync/memory_sync_1000_test 584716 ns/op 84173 B/op 2727 allocs/op 743812 ns/op 81489 B/op 2564 allocs/op 0.79
BenchmarkSync/memory_sync_10000_test 6239665 ns/op 862335 B/op 27636 allocs/op 8018558 ns/op 865876 B/op 26886 allocs/op 0.78
BenchmarkSync/etcd_sync_100_test 0.2647 ns/op 0 B/op 0 allocs/op 0.2943 ns/op 0 B/op 0 allocs/op 0.90
BenchmarkTextEditing 23824167185 ns/op 8436435512 B/op 19836599 allocs/op 28894853557 ns/op 8436118528 B/op 19834840 allocs/op 0.82

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

Please sign in to comment.