Skip to content

Commit

Permalink
Merge pull request #35 from exuan/pipeline
Browse files Browse the repository at this point in the history
chore: TairCmdable add TairPipeline
  • Loading branch information
yangbodong22011 authored Mar 27, 2023
2 parents beb4d6c + bd9d5d8 commit cad6f5b
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,5 @@ jobs:
go test -v ./... -run TestTairStringTestSuite/TestTairStringTestSuite
go test -v ./... -run TestTairStringTestSuite/TestTairHashTestSuite
go test -v ./... -run TestTairStringTestSuite/TestTairZsetTestSuite
go test -v ./... -run TestTairPipelineTestSuite
12 changes: 12 additions & 0 deletions tair/taircluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,15 @@ func NewTairClusterClient(opt *TairClusterOptions) *TairClusterClient {
tc.tairCmdable = tc.Process
return tc
}

func (t *TairClusterClient) TairPipeline() TairPipeline {
pipe := TairPipeline{
Pipeline: t.ClusterClient.Pipeline().(*redis.Pipeline),
}
pipe.init()
return pipe
}

func (t *TairClusterClient) TairPipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error) {
return t.ClusterClient.Pipeline().Pipelined(ctx, fn)
}
3 changes: 3 additions & 0 deletions tair/taircommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ type TairCmdable interface {
BfMExists(ctx context.Context, key string, items ...string) *redis.BoolSliceCmd
BfInsert(ctx context.Context, key string, bfInsertArgs *BfInsertArgs, items ...string) *redis.BoolSliceCmd
BfDebug(ctx context.Context, key string) *redis.StringSliceCmd
// TairPipeline
TairPipeline() TairPipeline
TairPipelined(ctx context.Context, fn func(redis.Pipeliner) error) ([]redis.Cmder, error)
}

func toMs(dur time.Duration) int64 {
Expand Down
70 changes: 70 additions & 0 deletions tair/tairpipeline_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package tair_test

import (
"context"
"testing"

"github.com/alibaba/tair-go/tair"
"github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
)

type PipelineTestSuite struct {
suite.Suite
tairClient *tair.TairClient
tairClusterClient *tair.TairClusterClient
}

func (suite *PipelineTestSuite) SetupTest() {
suite.tairClient = tair.NewTairClient(redisOptions())
assert.Equal(suite.T(), "OK", suite.tairClient.FlushDB(ctx).Val())

suite.tairClusterClient = cluster.newClusterClient(ctx, redisClusterOptions())
err := suite.tairClusterClient.ForEachMaster(ctx, func(ctx context.Context, master *redis.Client) error {
return master.FlushDB(ctx).Err()
})
assert.NoError(suite.T(), err)
}

func (suite *PipelineTestSuite) TestTairPipeline() {
pipe := suite.tairClient.TairPipeline()
pipe.Set(ctx, "key", "value", 0)
pipe.Get(ctx, "key")
cmds, err := pipe.Exec(ctx)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "value", cmds[1].(*redis.StringCmd).Val())
}

func (suite *PipelineTestSuite) TestTairPipelined() {
cmds, err := suite.tairClient.TairPipelined(ctx, func(p redis.Pipeliner) error {
p.Set(ctx, "key", "value", 0)
p.Get(ctx, "key")
return nil
})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "value", cmds[1].(*redis.StringCmd).Val())
}

func (suite *PipelineTestSuite) TestTairClusterPipeline() {
pipe := suite.tairClusterClient.TairPipeline()
pipe.Set(ctx, "key", "value", 0)
pipe.Get(ctx, "key")
cmds, err := pipe.Exec(ctx)
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "value", cmds[1].(*redis.StringCmd).Val())
}

func (suite *PipelineTestSuite) TestTairClusterPipelined() {
cmds, err := suite.tairClusterClient.TairPipelined(ctx, func(p redis.Pipeliner) error {
p.Set(ctx, "key", "value", 0)
p.Get(ctx, "key")
return nil
})
assert.NoError(suite.T(), err)
assert.Equal(suite.T(), "value", cmds[1].(*redis.StringCmd).Val())
}

func TestTairPipelineTestSuite(t *testing.T) {
suite.Run(t, new(PipelineTestSuite))
}

0 comments on commit cad6f5b

Please sign in to comment.