forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
54 lines (45 loc) · 1.25 KB
/
example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package miniredis_test
import (
"time"
"github.com/alicebob/miniredis/v2"
"github.com/gomodule/redigo/redis"
)
func Example() {
s, err := miniredis.Run()
if err != nil {
panic(err)
}
defer s.Close()
// Configure you application to connect to redis at s.Addr()
// Any redis client should work, as long as you use redis commands which
// miniredis implements.
c, err := redis.Dial("tcp", s.Addr())
if err != nil {
panic(err)
}
if _, err = c.Do("SET", "foo", "bar"); err != nil {
panic(err)
}
// You can ask miniredis about keys directly, without going over the network.
if got, err := s.Get("foo"); err != nil || got != "bar" {
panic("Didn't get 'bar' back")
}
// Or with a DB id
if _, err := s.DB(42).Get("foo"); err != miniredis.ErrKeyNotFound {
panic("didn't use a different database")
}
// Test key with expiration
s.SetTTL("foo", 60*time.Second)
s.FastForward(60 * time.Second)
if s.Exists("foo") {
panic("expect key to be expired")
}
// Or use a Check* function which Fail()s if the key is not what we expect
// (checks for existence, key type and the value)
// s.CheckGet(t, "foo", "bar")
// Check if there really was only one connection.
if s.TotalConnectionCount() != 1 {
panic("too many connections made")
}
// Output:
}