Skip to content

Commit

Permalink
Fix nil pointer when cloning the cluster
Browse files Browse the repository at this point in the history
  • Loading branch information
git-hulk committed Jan 5, 2025
1 parent 851ae37 commit 856ca41
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
4 changes: 4 additions & 0 deletions store/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ func NewCluster(name string, nodes []string, replicas int) (*Cluster, error) {
}

func (cluster *Cluster) Clone() *Cluster {
if cluster == nil {
return nil
}

clone := &Cluster{
Name: cluster.Name,
Shards: make([]*Shard, 0),
Expand Down
13 changes: 13 additions & 0 deletions store/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ import (
"github.com/apache/kvrocks-controller/consts"
)

func TestCluster_Clone(t *testing.T) {
var cluster *Cluster
emptyCluster := cluster.Clone()
require.Nil(t, emptyCluster)

cluster, err := NewCluster("test", []string{"node1", "node2", "node3"}, 1)
require.NoError(t, err)

clusterCopy := cluster.Clone()
require.Equal(t, cluster.Name, clusterCopy.Name)
require.Equal(t, cluster.Shards, clusterCopy.Shards)
}

func TestCluster_FindIndexShardBySlot(t *testing.T) {
cluster, err := NewCluster("test", []string{"node1", "node2", "node3"}, 1)
require.NoError(t, err)
Expand Down

0 comments on commit 856ca41

Please sign in to comment.