-
Notifications
You must be signed in to change notification settings - Fork 0
/
const_test.go
61 lines (51 loc) · 1.45 KB
/
const_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
55
56
57
58
59
60
61
// contains common test fixtures.
package failsafe
import (
"github.com/goraft/raft"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"path/filepath"
)
var testdir = "testdata"
var testRaftdir = filepath.Join(testdir, "server/0")
var listAddr = "localhost:4000"
var servAddr = "http://" + listAddr
var smallJSONfile = filepath.Join(testdir, "small.json")
var smallJSON, _ = ioutil.ReadFile(smallJSONfile)
var dummyFile = filepath.Join(testdir, "_dummy.json")
// servdir -> {*Server, net.Listener, *http.Server}
var activeServers map[string][]interface{}
// register commands for testing and start a server.
func init() {
raft.RegisterCommand(&SetCommand{})
raft.RegisterCommand(&DeleteCommand{})
activeServers = make(map[string][]interface{})
}
func startTestServer(servdir string) (*Server, net.Listener, *http.Server, error) {
log.SetOutput(ioutil.Discard)
if v, ok := activeServers[servdir]; ok {
return v[0].(*Server), v[1].(net.Listener), v[2].(*http.Server), nil
}
if err := os.RemoveAll(servdir); err != nil {
log.Fatal(err)
}
mux := http.NewServeMux()
srv, err := NewServer("test", servdir, listAddr, mux)
if err != nil {
log.Fatal(err)
}
if err := srv.Install(""); err != nil {
return nil, nil, nil, err
}
daemon := &http.Server{Addr: listAddr, Handler: mux}
lis, err := net.Listen("tcp", listAddr)
if err != nil {
log.Fatal(err)
}
go daemon.Serve(lis)
activeServers[servdir] = []interface{}{srv, lis, daemon}
return srv, lis, daemon, nil
}