From 5b30e4ecb835b52327484a4f82d71956fae97b49 Mon Sep 17 00:00:00 2001 From: ofekshenawa Date: Fri, 20 Sep 2024 18:44:49 +0300 Subject: [PATCH] Test extractKeys --- command.go | 11 +++++++--- commands_test.go | 23 +++++++++++++++++++++ redis.go | 2 +- redis_test.go | 53 ------------------------------------------------ 4 files changed, 32 insertions(+), 57 deletions(-) diff --git a/command.go b/command.go index 3deaec10..d3cee927 100644 --- a/command.go +++ b/command.go @@ -119,18 +119,23 @@ func cmdString(cmd Cmder, val interface{}) string { return util.BytesToString(b) } +// A wrapper around extract keys, just to make it easier to test. +func GetKeys(cmd Cmder) []string { + return extractKeys(cmd) +} + func extractKeys(cmd Cmder) []string { firstKeyPos := cmdFirstKeyPos(cmd) if firstKeyPos == -1 { return nil } - internal.Logger.Printf(context.Background(), "firstKeyPos: %d", firstKeyPos) + args := cmd.Args() keys := []string{} switch cmd.Name() { - case "MGET", "MSET": - for i := int(firstKeyPos); i < len(args); i += 2 { + case "mget": + for i := int(firstKeyPos); i < len(args); i++ { keys = append(keys, cmd.stringArg(i)) } diff --git a/commands_test.go b/commands_test.go index 9554bf9a..1e493de4 100644 --- a/commands_test.go +++ b/commands_test.go @@ -7279,6 +7279,29 @@ var _ = Describe("Commands", func() { }) }) +var _ = Describe("Get Keys from Command", func() { + var client *redis.Client + + BeforeEach(func() { + client = redis.NewClient(redisOptions()) + Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred()) + }) + + AfterEach(func() { + Expect(client.Close()).NotTo(HaveOccurred()) + }) + + It("returns the keys extracted from the command", func() { + cmd := redis.NewCmd(ctx, "SET", "key1", "value1") + keys := redis.GetKeys(cmd) + Expect(keys).To(Equal([]string{"key1"})) + + cmd = redis.NewCmd(ctx, "MGET", "key1", "key2", "key3") + keys = redis.GetKeys(cmd) + Expect(keys).To(Equal([]string{"key1", "key2", "key3"})) + }) +}) + type numberStruct struct { Number int } diff --git a/redis.go b/redis.go index 0705c744..c8b50080 100644 --- a/redis.go +++ b/redis.go @@ -431,7 +431,7 @@ func (c *baseClient) _process(ctx context.Context, cmd Cmder, attempt int) (bool return false, err } } - internal.Logger.Printf(ctx, "redis: keys in command %s", extractKeys(cmd)) + retryTimeout := uint32(0) if err := c.withConn(ctx, func(ctx context.Context, cn *pool.Conn) error { if err := cn.WithWriter(c.context(ctx), c.opt.WriteTimeout, func(wr *proto.Writer) error { diff --git a/redis_test.go b/redis_test.go index e0fb026e..ef212545 100644 --- a/redis_test.go +++ b/redis_test.go @@ -6,7 +6,6 @@ import ( "errors" "fmt" "net" - "strconv" "testing" "time" @@ -634,55 +633,3 @@ var _ = Describe("Hook with MinIdleConns", func() { })) }) }) - -var _ = Describe("Command Name", func() { - var client *redis.Client - - BeforeEach(func() { - client = redis.NewClient(redisOptions()) - Expect(client.FlushDB(ctx).Err()).NotTo(HaveOccurred()) - }) - - AfterEach(func() { - err := client.Close() - Expect(err).NotTo(HaveOccurred()) - }) - - It("should return key name", func() { - mSet := client.MSet(ctx, "key1", "hello1", "key2", "hello2") - Expect(mSet.Err()).NotTo(HaveOccurred()) - Expect(mSet.Val()).To(Equal("OK")) - Expect(mSet.Args()).To(Equal([]string{"MSET", "key1", "hello1", "key2", "hello2"})) - - mGet := client.MGet(ctx, "key1", "key2", "_") - Expect(mGet.Err()).NotTo(HaveOccurred()) - Expect(mGet.Val()).To(Equal([]interface{}{"hello1", "hello2", nil})) - - // MSet struct - type set struct { - Set1 string `redis:"set1"` - Set2 int16 `redis:"set2"` - Set3 time.Duration `redis:"set3"` - Set4 interface{} `redis:"set4"` - Set5 map[string]interface{} `redis:"-"` - } - mSet = client.MSet(ctx, &set{ - Set1: "val1", - Set2: 1024, - Set3: 2 * time.Millisecond, - Set4: nil, - Set5: map[string]interface{}{"k1": 1}, - }) - Expect(mSet.Err()).NotTo(HaveOccurred()) - Expect(mSet.Val()).To(Equal("OK")) - - mGet = client.MGet(ctx, "set1", "set2", "set3", "set4") - Expect(mGet.Err()).NotTo(HaveOccurred()) - Expect(mGet.Val()).To(Equal([]interface{}{ - "val1", - "1024", - strconv.Itoa(int(2 * time.Millisecond.Nanoseconds())), - "", - })) - }) -})