MCPcopy Create free account
hub / github.com/pace/bricks / TestGet

Method TestGet

pkg/cache/testsuite/cache.go:90–192  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

88}
89
90func (suite *CacheTestSuite) TestGet() {
91 c := suite.Cache
92 ctx := log.WithContext(context.Background())
93
94 _ = c.Forget(ctx, "foo") // make sure it doesn't exist
95 suite.Run("returns the ttl if set", func() {
96 _ = c.Put(ctx, "foo", []byte("bar"), time.Minute)
97 _, ttl, _ := c.Get(ctx, "foo")
98 // expect ttl to be something between 59 and 60 seconds
99 suite.LessOrEqual(int64(ttl), int64(time.Minute))
100 suite.Greater(int64(ttl), int64(time.Minute-time.Second))
101 })
102 _ = c.Forget(ctx, "foo") // clean up
103
104 suite.Run("returns 0 as ttl if ttl not set", func() {
105 _ = c.Put(ctx, "foo", []byte("bar"), 0)
106 _, ttl, _ := c.Get(ctx, "foo")
107 suite.Equal(time.Duration(0), ttl)
108 })
109 _ = c.Forget(ctx, "foo") // clean up
110
111 suite.Run("returns not found error", func() {
112 _, _, err := c.Get(ctx, "foo")
113 suite.True(errors.Is(err, cache.ErrNotFound))
114 })
115 _ = c.Forget(ctx, "foo") // clean up
116
117 suite.Run("returns not found if ttl ran out", func() {
118 err := c.Put(ctx, "foo", []byte("bar"), time.Millisecond) // minimum ttl
119 suite.NoError(err)
120 <-time.After(2 * time.Millisecond)
121 _, _, err = c.Get(ctx, "foo")
122 suite.True(errors.Is(err, cache.ErrNotFound))
123 })
124 _ = c.Forget(ctx, "foo") // clean up
125
126 _ = c.Forget(ctx, "foo1") // make sure it doesn't exist
127 _ = c.Forget(ctx, "foo2") // make sure it doesn't exist
128 suite.Run("retrieves the right value", func() {
129 _ = c.Put(ctx, "foo1", []byte("bar1"), 0)
130 _ = c.Put(ctx, "foo2", []byte("bar2"), 0)
131 value1, _, _ := c.Get(ctx, "foo1")
132 value2, _, _ := c.Get(ctx, "foo2")
133 suite.Equal([]byte("bar1"), value1)
134 suite.Equal([]byte("bar2"), value2)
135 })
136 _ = c.Forget(ctx, "foo1") // clean up
137 _ = c.Forget(ctx, "foo2") // clean up
138
139 suite.Run("is unaffected from manipulating the output", func() {
140 _ = c.Put(ctx, "foo", []byte("bar"), 0)
141 output, _, _ := c.Get(ctx, "foo")
142 output[0]++ // output manipulation
143 value, _, _ := c.Get(ctx, "foo")
144 suite.Equal([]byte("bar"), value)
145 })
146 _ = c.Forget(ctx, "foo") // clean up
147

Callers

nothing calls this directly

Calls 8

WithContextFunction · 0.92
DurationMethod · 0.80
WaitMethod · 0.80
ForgetMethod · 0.65
PutMethod · 0.65
GetMethod · 0.65
AddMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected