influxdb/test.go

161 lines
3.7 KiB
Go
Raw Normal View History

2013-04-17 02:28:08 +00:00
package raft
import (
2013-05-01 05:11:23 +00:00
"fmt"
2013-04-17 02:28:08 +00:00
"io/ioutil"
"os"
2013-05-01 05:21:56 +00:00
"time"
)
const (
2013-05-27 02:17:20 +00:00
testHeartbeatTimeout = 20 * time.Millisecond
testElectionTimeout = 60 * time.Millisecond
2013-04-17 02:28:08 +00:00
)
2013-05-10 03:50:57 +00:00
func init() {
RegisterCommand(&joinCommand{})
2013-05-10 03:50:57 +00:00
RegisterCommand(&TestCommand1{})
RegisterCommand(&TestCommand2{})
}
2013-04-17 02:28:08 +00:00
//------------------------------------------------------------------------------
//
// Helpers
//
//------------------------------------------------------------------------------
//--------------------------------------
// Logs
//--------------------------------------
func getLogPath() string {
f, _ := ioutil.TempFile("", "raft-log-")
f.Close()
os.Remove(f.Name())
return f.Name()
}
2013-04-30 04:13:50 +00:00
func setupLogFile(content string) string {
2013-04-17 02:28:08 +00:00
f, _ := ioutil.TempFile("", "raft-log-")
f.Write([]byte(content))
f.Close()
return f.Name()
}
2013-04-30 04:13:50 +00:00
func setupLog(content string) (*Log, string) {
path := setupLogFile(content)
log := NewLog()
log.ApplyFunc = func(c Command) error {
return nil
}
2013-04-30 04:13:50 +00:00
if err := log.Open(path); err != nil {
panic("Unable to open log")
}
return log, path
}
2013-04-17 02:28:08 +00:00
//--------------------------------------
// Servers
//--------------------------------------
2013-05-28 19:57:38 +00:00
func newTestServer(name string, transporter Transporter) *Server {
2013-04-17 02:28:08 +00:00
path, _ := ioutil.TempDir("", "raft-server-")
2013-06-03 02:43:40 +00:00
server, _ := NewServer(name, path, transporter, nil)
2013-04-17 02:28:08 +00:00
return server
}
2013-05-28 19:57:38 +00:00
func newTestServerWithLog(name string, transporter Transporter, content string) *Server {
server := newTestServer(name, transporter)
ioutil.WriteFile(server.LogPath(), []byte(content), 0644)
return server
}
2013-05-28 19:57:38 +00:00
func newTestCluster(names []string, transporter Transporter, lookup map[string]*Server) []*Server {
servers := []*Server{}
2013-05-01 05:11:23 +00:00
for _, name := range names {
if lookup[name] != nil {
panic(fmt.Sprintf("Duplicate server in test cluster! %v", name))
}
2013-05-28 19:57:38 +00:00
server := newTestServer(name, transporter)
2013-05-27 02:17:20 +00:00
server.SetElectionTimeout(testElectionTimeout)
2013-05-01 05:11:23 +00:00
servers = append(servers, server)
lookup[name] = server
}
for _, server := range servers {
server.SetHeartbeatTimeout(testHeartbeatTimeout)
2013-05-01 05:11:23 +00:00
for _, peer := range servers {
server.AddPeer(peer.Name())
2013-05-01 05:11:23 +00:00
}
server.Start()
}
2013-05-28 19:57:38 +00:00
return servers
2013-05-01 05:11:23 +00:00
}
2013-05-28 19:57:38 +00:00
//--------------------------------------
// Transporter
//--------------------------------------
type testTransporter struct {
sendVoteRequestFunc func(server *Server, peer *Peer, req *RequestVoteRequest) (*RequestVoteResponse, error)
sendAppendEntriesRequestFunc func(server *Server, peer *Peer, req *AppendEntriesRequest) (*AppendEntriesResponse, error)
}
func (t *testTransporter) SendVoteRequest(server *Server, peer *Peer, req *RequestVoteRequest) (*RequestVoteResponse, error) {
return t.sendVoteRequestFunc(server, peer, req)
}
func (t *testTransporter) SendAppendEntriesRequest(server *Server, peer *Peer, req *AppendEntriesRequest) (*AppendEntriesResponse, error) {
return t.sendAppendEntriesRequestFunc(server, peer, req)
}
//--------------------------------------
// Join Command
//--------------------------------------
type joinCommand struct {
Name string `json:"name"`
}
func (c *joinCommand) CommandName() string {
return "test:join"
}
func (c *joinCommand) Apply(server *Server) error {
err := server.AddPeer(c.Name)
return err
}
2013-04-17 02:28:08 +00:00
//--------------------------------------
// Command1
//--------------------------------------
type TestCommand1 struct {
Val string `json:"val"`
2013-04-28 21:23:21 +00:00
I int `json:"i"`
2013-04-17 02:28:08 +00:00
}
2013-04-28 04:51:17 +00:00
func (c TestCommand1) CommandName() string {
2013-04-17 02:28:08 +00:00
return "cmd_1"
}
func (c TestCommand1) Apply(server *Server) error {
2013-04-28 22:36:46 +00:00
return nil
}
2013-04-17 02:28:08 +00:00
//--------------------------------------
// Command2
//--------------------------------------
type TestCommand2 struct {
X int `json:"x"`
}
2013-04-28 04:51:17 +00:00
func (c TestCommand2) CommandName() string {
2013-04-17 02:28:08 +00:00
return "cmd_2"
}
2013-04-28 22:36:46 +00:00
func (c TestCommand2) Apply(server *Server) error {
2013-04-28 22:36:46 +00:00
return nil
}