Skip to content

Commit

Permalink
fix: 修改cluster 单测 (#963)
Browse files Browse the repository at this point in the history
Co-authored-by: liwc1 <liwc1@jiguang.cn>
Co-authored-by: 大可 <hnlq.sysu@gmail.com>
  • Loading branch information
3 people authored Aug 14, 2023
1 parent 7d81c63 commit 5c2a25b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 15 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,10 @@ jobs:
runs-on: ubuntu-latest

steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3.5.3
- name: Checkout code
uses: actions/checkout@v3
with:
ref: ${{ github.event.pull_request.merge_commit_sha }}

- name: docker-compose
run: docker-compose -f test/docker-compose.yaml up -d
Expand Down
15 changes: 7 additions & 8 deletions pkg/client/redis/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package redis

import (
"context"
"errors"
"github.com/douyu/jupiter/pkg/core/constant"
"github.com/douyu/jupiter/pkg/core/singleton"
"github.com/douyu/jupiter/pkg/util/xdebug"
Expand Down Expand Up @@ -42,19 +43,18 @@ func (config *ClusterOptions) MustClusterBuild() *ClusterClient {

// BuildCluster ..
func (config *ClusterOptions) BuildCluster() (*ClusterClient, error) {
ins := new(ClusterClient)
var err error
if xdebug.IsDevelopmentMode() {
xdebug.PrettyJsonPrint("redis's config: "+config.name, config)
}

if len(config.Addr) > 0 {
ins, err = config.buildCluster()
if err != nil {
return ins, err
}
if len(config.Addr) <= 0 {
return nil, errors.New("cluster redis addr is empty")
}

ins, err := config.buildCluster()
if err != nil {
return nil, err
}
return ins, nil
}

Expand Down Expand Up @@ -93,7 +93,6 @@ func (config *ClusterOptions) buildCluster() (*ClusterClient, error) {
}
}

clusterClient.Ping(context.Background())
if err := clusterClient.Ping(context.Background()).Err(); err != nil {
if config.OnDialError == "panic" {
config.logger.Panic("redis cluster client start err: " + err.Error())
Expand Down
13 changes: 8 additions & 5 deletions pkg/client/redis/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ func Test_Cluster(t *testing.T) {
var client *ClusterClient
defer func() {
if r := recover(); r != nil {
assert.Equal(t, "no cluster for default", r.(string))
assert.Equal(t, "cluster redis addr is empty", r.(string))
assert.Nil(t, client)
}
}()
Expand All @@ -29,13 +29,16 @@ func Test_Cluster(t *testing.T) {
})
t.Run("normal start", func(t *testing.T) {
config.Addr = []string{"127.0.0.1:7000", "127.0.0.1:7001", "127.0.0.1:7002"}
config.name = "test"
client, err := config.BuildCluster()
assert.Nil(t, err)
assert.NotNil(t, client)
err = client.Ping(context.Background()).Err()
if err != nil {
t.Errorf("Test_Cluster ping err %v", err)
t.Fatalf("Failed to build cluster: %v", err)
}
if client == nil {
t.Fatal("Client is nil")
}
err = client.Ping(context.Background()).Err()
assert.Nil(t, err)
})

}

0 comments on commit 5c2a25b

Please sign in to comment.