forked from alicebob/miniredis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ephemeral.go
89 lines (74 loc) · 1.9 KB
/
ephemeral.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// +build int
package main
// Start a redis server in memory-only mode on a random port.
import (
"fmt"
"net"
"os"
"os/exec"
"strconv"
"time"
)
const (
localSrc = "./redis_src/"
executable = "redis-server"
)
type ephemeral exec.Cmd
// Redis starts a memory-only redis on a random port. Will panic if that
// doesn't work.
// Returns something which you'll have to Close(), and a string to give to Dial()
func Redis() (*ephemeral, string) {
return runRedis("")
}
// RedisAuth starts a memory-only redis on a random port. The redis has
// authentication enabled. See Redis()
func RedisAuth(passwd string) (*ephemeral, string) {
return runRedis(fmt.Sprintf("requirepass %s", passwd))
}
func runRedis(extraConfig string) (*ephemeral, string) {
port := arbitraryPort()
// we prefer the executable from ./redis_src, if any. See ./get_redis.sh
os.Setenv("PATH", fmt.Sprintf("%s:%s", localSrc, os.Getenv("PATH")))
c := exec.Command(executable, "-")
stdin, err := c.StdinPipe()
if err != nil {
panic(err)
}
fmt.Fprintf(stdin, "port %d\nbind 127.0.0.1\nappendonly no\n%s", port, extraConfig)
stdin.Close()
if err := c.Start(); err != nil {
panic(err)
}
addr := fmt.Sprintf("127.0.0.1:%d", port)
// Wait until the thing is ready
timeout := time.Now().Add(1 * time.Second)
for time.Now().Before(timeout) {
conn, err := net.Dial("tcp", addr)
if err == nil {
conn.Close()
e := ephemeral(*c)
return &e, addr
}
time.Sleep(1 * time.Millisecond)
}
panic(fmt.Sprintf("No connection on port %d", port))
}
func (e *ephemeral) Close() {
((*exec.Cmd)(e)).Process.Kill()
((*exec.Cmd)(e)).Wait()
}
// arbitraryPort returns a non-used port.
func arbitraryPort() int {
l, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
defer l.Close()
addr := l.Addr().String()
_, port, err := net.SplitHostPort(addr)
if err != nil {
panic(err)
}
p, _ := strconv.Atoi(port)
return p
}