Skip to content

Commit

Permalink
Revise the code
Browse files Browse the repository at this point in the history
  • Loading branch information
hackerwins committed Feb 17, 2025
1 parent d8c8c68 commit 3d12610
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 25 deletions.
36 changes: 26 additions & 10 deletions pkg/document/time/version_vector_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
/*
* Copyright 2025 The Yorkie Authors. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package time_test

import (
Expand Down Expand Up @@ -28,54 +44,54 @@ func TestVersionVector(t *testing.T) {
},
{
name: "v1 has values, v2 is empty",
v1: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
v1: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
v2: time.NewVersionVector(),
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 0,
actor2: 0,
}),
},
{
name: "v2 has values, v1 is empty",
v1: time.NewVersionVector(),
v2: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
v2: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 0,
actor2: 0,
}),
},
{
name: "both vectors have same keys with different values",
v1: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
v1: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
v2: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
v2: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 3,
actor2: 4,
}),
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 3,
actor2: 3,
}),
},
{
name: "vectors have different keys",
v1: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
v1: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
v2: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
v2: helper.VersionVectorOf(map[*time.ActorID]int64{
actor2: 4,
actor3: 6,
}),
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 0,
actor2: 3,
actor3: 0,
Expand Down
26 changes: 13 additions & 13 deletions server/backend/database/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ func TestFindMinVersionVector(t *testing.T) {
vvInfos: []database.VersionVectorInfo{
{
ClientID: "client1",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
},
},
excludeClientID: "",
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
Expand All @@ -82,21 +82,21 @@ func TestFindMinVersionVector(t *testing.T) {
vvInfos: []database.VersionVectorInfo{
{
ClientID: "client1",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
},
{
ClientID: "client2",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 3,
actor2: 4,
}),
},
},
excludeClientID: "client1",
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 3,
actor2: 4,
}),
Expand All @@ -106,7 +106,7 @@ func TestFindMinVersionVector(t *testing.T) {
vvInfos: []database.VersionVectorInfo{
{
ClientID: "client1",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
}),
},
Expand All @@ -119,21 +119,21 @@ func TestFindMinVersionVector(t *testing.T) {
vvInfos: []database.VersionVectorInfo{
{
ClientID: "client1",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
},
{
ClientID: "client2",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor2: 2,
actor3: 4,
}),
},
},
excludeClientID: "",
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 0,
actor2: 2,
actor3: 0,
Expand All @@ -144,28 +144,28 @@ func TestFindMinVersionVector(t *testing.T) {
vvInfos: []database.VersionVectorInfo{
{
ClientID: "client1",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 5,
actor2: 3,
}),
},
{
ClientID: "client2",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 3,
actor2: 4,
}),
},
{
ClientID: "client3",
VersionVector: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
VersionVector: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 4,
actor2: 2,
}),
},
},
excludeClientID: "",
expect: helper.NewVersionVectorFromActors(map[*time.ActorID]int64{
expect: helper.VersionVectorOf(map[*time.ActorID]int64{
actor1: 3,
actor2: 2,
}),
Expand Down
4 changes: 2 additions & 2 deletions test/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,8 @@ func MaxVersionVector(actors ...*time.ActorID) time.VersionVector {
return vector
}

// NewVersionVectorFromActors creates a new VersionVector from a map of ActorIDs and their lamport timestamps.
func NewVersionVectorFromActors(actors map[*time.ActorID]int64) time.VersionVector {
// VersionVectorOf creates a new version vector from the given actors.
func VersionVectorOf(actors map[*time.ActorID]int64) time.VersionVector {
vector := time.NewVersionVector()
for actor, lamport := range actors {
vector.Set(actor, lamport)
Expand Down

0 comments on commit 3d12610

Please sign in to comment.