MCPcopy
hub / github.com/redis/go-redis / TestCmdFirstKeyPosWithInfo_UsesCommandInfoWhenWarm

Function TestCmdFirstKeyPosWithInfo_UsesCommandInfoWhenWarm

unit_test.go:33–76  ·  view source on GitHub ↗

TestCmdFirstKeyPosWithInfo_UsesCommandInfoWhenWarm checks that once the cmdsInfoCache has been populated, cmdFirstKeyPosWithInfo returns CommandInfo.FirstKeyPos rather than the hardcoded fallback of 1. Uses an injected cache fn so no Redis server is required.

(t *testing.T)

Source from the content-addressed store, hash-verified

31// CommandInfo.FirstKeyPos rather than the hardcoded fallback of 1.
32// Uses an injected cache fn so no Redis server is required.
33func TestCmdFirstKeyPosWithInfo_UsesCommandInfoWhenWarm(t *testing.T) {
34 // "mymodule.cmd" is not in keylessCommands and has no firstKeyPos set,
35 // so cold-cache behaviour falls back to the default of 1.
36 const name = "mymodule.cmd"
37 ctx := context.Background()
38 cmd := NewCmd(ctx, name, "arg1")
39
40 if got := cmdFirstKeyPosWithInfo(cmd, nil); got != 1 {
41 t.Fatalf("cold cache: got %d, want 1", got)
42 }
43
44 // With a warm CommandInfo saying FirstKeyPos=0 (e.g. a module keyless command
45 // not in our hand-maintained table), the function must return 0.
46 info := &CommandInfo{Name: name, FirstKeyPos: 0}
47 if got := cmdFirstKeyPosWithInfo(cmd, info); got != 0 {
48 t.Fatalf("warm cache FirstKeyPos=0: got %d, want 0", got)
49 }
50
51 info2 := &CommandInfo{Name: name, FirstKeyPos: 2}
52 if got := cmdFirstKeyPosWithInfo(cmd, info2); got != 2 {
53 t.Fatalf("warm cache FirstKeyPos=2: got %d, want 2", got)
54 }
55
56 // End-to-end: Peek returns nil before Get, the real entry after.
57 cache := newCmdsInfoCache(func(_ context.Context) (map[string]*CommandInfo, error) {
58 return map[string]*CommandInfo{name: {Name: name, FirstKeyPos: 0}}, nil
59 })
60
61 if cache.Peek() != nil {
62 t.Fatal("Peek() must return nil before Get()")
63 }
64
65 if _, err := cache.Get(ctx); err != nil {
66 t.Fatalf("cache.Get: %v", err)
67 }
68
69 warmInfo := cache.Peek()[name]
70 if warmInfo == nil {
71 t.Fatalf("Peek()[%q] is nil after Get()", name)
72 }
73 if pos := cmdFirstKeyPosWithInfo(cmd, warmInfo); pos != 0 {
74 t.Fatalf("post-Get: got %d, want 0", pos)
75 }
76}

Callers

nothing calls this directly

Calls 5

NewCmdFunction · 0.85
cmdFirstKeyPosWithInfoFunction · 0.85
newCmdsInfoCacheFunction · 0.85
GetMethod · 0.65
PeekMethod · 0.45

Tested by

no test coverage detected