influxdb/server_test.go

75 lines
1.8 KiB
Go
Raw Normal View History

2014-10-22 05:32:19 +00:00
package influxdb_test
import (
"fmt"
"io/ioutil"
"os"
"testing"
"github.com/influxdb/influxdb"
"github.com/influxdb/influxdb/messaging"
)
// Ensure the server can be successfully opened and closed.
func TestServer_Open(t *testing.T) {
c := NewMessagingClient()
s := NewServer(c)
defer s.Close()
2014-10-24 00:54:12 +00:00
if err := s.Server.Open(tempfile()); err != nil {
t.Fatal(err)
}
if err := s.Server.Close(); err != nil {
t.Fatal(err)
}
2014-10-22 05:32:19 +00:00
}
// Server is a wrapping test struct for influxdb.Server.
type Server struct {
*influxdb.Server
}
// NewServer returns a new test server instance.
func NewServer(client influxdb.MessagingClient) *Server {
return &Server{influxdb.NewServer(client)}
}
// OpenServer returns a new, open test server instance.
func OpenServer(client influxdb.MessagingClient) *Server {
s := NewServer(client)
if err := s.Open(tempfile()); err != nil {
panic(err.Error())
}
return s
}
// Close shuts down the server and removes all temporary files.
func (s *Server) Close() {
defer os.RemoveAll(s.Path())
s.Server.Close()
}
// MessagingClient represents a test client for the messaging broker.
type MessagingClient struct {
c chan *messaging.Message
}
// NewMessagingClient returns a new instance of MessagingClient.
func NewMessagingClient() *MessagingClient {
return &MessagingClient{c: make(chan *messaging.Message, 1)}
}
// Returns a channel for streaming message.
func (c *MessagingClient) C() <-chan *messaging.Message { return c.c }
// tempfile returns a temporary path.
func tempfile() string {
f, _ := ioutil.TempFile("", "influxdb-")
path := f.Name()
f.Close()
os.Remove(path)
return path
}
func warn(v ...interface{}) { fmt.Fprintln(os.Stderr, v...) }
func warnf(msg string, v ...interface{}) { fmt.Fprintf(os.Stderr, msg+"\n", v...) }