Use OptCommandServerOptions to inject mock TranslateStore

This commit is contained in:
Cody Soyland 2018-06-25 17:22:34 -05:00
parent 324028a8c2
commit 9f68ea4663
4 changed files with 37 additions and 44 deletions

View file

@ -4,13 +4,13 @@ import (
"context"
"io"
"io/ioutil"
"net/http/httptest"
"testing"
"time"
"github.com/pilosa/pilosa"
"github.com/pilosa/pilosa/http"
"github.com/pilosa/pilosa/mock"
"github.com/pilosa/pilosa/server"
"github.com/pilosa/pilosa/test"
)
@ -52,13 +52,14 @@ func TestTranslateStore_Reader(t *testing.T) {
}
return &mrc, nil
}
h := test.MustNewHandler()
h.API.TranslateStore = &translateStore
s := httptest.NewServer(h)
defer s.Close()
opts := server.OptCommandServerOptions(pilosa.OptServerPrimaryTranslateStore(translateStore))
main := test.MustRunMainWithCluster(t, 1, opts)[0]
defer main.Close()
// Connect to server and stream all available data.
store := http.NewTranslateStore(s.URL)
store := http.NewTranslateStore(main.Server.URI.String())
rc, err := store.Reader(context.Background(), 100)
if err != nil {
t.Fatal(err)
@ -95,15 +96,16 @@ func TestTranslateStore_Reader(t *testing.T) {
translateStore.ReaderFunc = func(ctx context.Context, off int64) (io.ReadCloser, error) {
return &mrc, nil
}
h := test.MustNewHandler()
h.API.TranslateStore = &translateStore
s := httptest.NewServer(h)
defer s.Close()
opts := server.OptCommandServerOptions(pilosa.OptServerPrimaryTranslateStore(translateStore))
main := test.MustRunMainWithCluster(t, 1, opts)[0]
defer main.Close()
defer close(done)
// Connect to server and begin streaming.
ctx, cancel := context.WithCancel(context.Background())
store := http.NewTranslateStore(s.URL)
store := http.NewTranslateStore(main.Server.URI.String())
if _, err := store.Reader(ctx, 0); err != nil {
t.Fatal(err)
}
@ -123,12 +125,11 @@ func TestTranslateStore_Reader(t *testing.T) {
translateStore.ReaderFunc = func(ctx context.Context, off int64) (io.ReadCloser, error) {
return nil, pilosa.ErrNotImplemented
}
h := test.MustNewHandler()
h.API.TranslateStore = &translateStore
s := httptest.NewServer(h)
defer s.Close()
_, err := http.NewTranslateStore(s.URL).Reader(context.Background(), 0)
opts := server.OptCommandServerOptions(pilosa.OptServerPrimaryTranslateStore(translateStore))
main := test.MustRunMainWithCluster(t, 1, opts)[0]
_, err := http.NewTranslateStore(main.Server.URI.String()).Reader(context.Background(), 0)
if err != pilosa.ErrNotImplemented {
t.Fatalf("unexpected error: %s", err)
}

View file

@ -17,22 +17,22 @@ type TranslateStore struct {
ReaderFunc func(ctx context.Context, off int64) (io.ReadCloser, error)
}
func (s *TranslateStore) TranslateColumnsToUint64(index string, values []string) ([]uint64, error) {
func (s TranslateStore) TranslateColumnsToUint64(index string, values []string) ([]uint64, error) {
return s.TranslateColumnsToUint64Func(index, values)
}
func (s *TranslateStore) TranslateColumnToString(index string, values uint64) (string, error) {
func (s TranslateStore) TranslateColumnToString(index string, values uint64) (string, error) {
return s.TranslateColumnToStringFunc(index, values)
}
func (s *TranslateStore) TranslateRowsToUint64(index, frame string, values []string) ([]uint64, error) {
func (s TranslateStore) TranslateRowsToUint64(index, frame string, values []string) ([]uint64, error) {
return s.TranslateRowsToUint64Func(index, frame, values)
}
func (s *TranslateStore) TranslateRowToString(index, frame string, value uint64) (string, error) {
func (s TranslateStore) TranslateRowToString(index, frame string, value uint64) (string, error) {
return s.TranslateRowToStringFunc(index, frame, value)
}
func (s *TranslateStore) Reader(ctx context.Context, off int64) (io.ReadCloser, error) {
func (s TranslateStore) Reader(ctx context.Context, off int64) (io.ReadCloser, error) {
return s.ReaderFunc(ctx, off)
}

View file

@ -77,14 +77,14 @@ type Command struct {
Handler pilosa.Handler
ln net.Listener
serverOptions []pilosa.ServerOption
ServerOptions []pilosa.ServerOption
}
type CommandOption func(c *Command) error
func OptCommandServerOptions(opts ...pilosa.ServerOption) CommandOption {
return func(c *Command) error {
c.serverOptions = append(c.serverOptions, opts...)
c.ServerOptions = append(c.ServerOptions, opts...)
return nil
}
}
@ -266,7 +266,7 @@ func (m *Command) SetupServer() error {
pilosa.OptServerClusterDisabled(m.Config.Cluster.Disabled, m.Config.Cluster.Hosts),
}
serverOptions = append(serverOptions, m.serverOptions...)
serverOptions = append(serverOptions, m.ServerOptions...)
m.Server, err = pilosa.NewServer(serverOptions...)

View file

@ -34,7 +34,7 @@ import (
)
////////////////////////////////////////////////////////////////////////////////////
// Main represents a test wrapper for main.Main.
// Main represents a test wrapper for server.Command.
type Main struct {
*server.Command
@ -43,43 +43,35 @@ type Main struct {
Stderr bytes.Buffer
}
type MainOpt func(m *Main) error
func OptAntiEntropyInterval(dur time.Duration) MainOpt {
return func(m *Main) error {
m.Command.Config.AntiEntropy.Interval = toml.Duration(dur)
func OptAntiEntropyInterval(dur time.Duration) server.CommandOption {
return func(m *server.Command) error {
m.Config.AntiEntropy.Interval = toml.Duration(dur)
return nil
}
}
func OptAllowedOrigins(origins []string) MainOpt {
return func(m *Main) error {
func OptAllowedOrigins(origins []string) server.CommandOption {
return func(m *server.Command) error {
m.Config.Handler.AllowedOrigins = origins
return nil
}
}
// NewMain returns a new instance of Main with a temporary data directory and random port.
func NewMain(opts ...MainOpt) *Main {
func NewMain(opts ...server.CommandOption) *Main {
path, err := ioutil.TempDir("", "pilosa-")
if err != nil {
panic(err)
}
m := &Main{Command: server.NewCommand(os.Stdin, os.Stdout, os.Stderr)}
m := &Main{Command: server.NewCommand(os.Stdin, os.Stdout, os.Stderr, opts...)}
m.Config.DataDir = path
m.Config.Bind = "http://localhost:0"
m.Config.Cluster.Disabled = true
m.Command.Stdin = &m.Stdin
m.Command.Stdout = &m.Stdout
m.Command.Stderr = &m.Stderr
for _, opt := range opts {
err := opt(m)
if err != nil {
panic(err)
}
}
err = m.SetupServer()
if err != nil {
panic(err)
@ -94,7 +86,7 @@ func NewMain(opts ...MainOpt) *Main {
}
// NewMainWithCluster returns a new instance of Main with clustering enabled.
func NewMainWithCluster(isCoordinator bool, opts ...MainOpt) *Main {
func NewMainWithCluster(isCoordinator bool, opts ...server.CommandOption) *Main {
m := NewMain(opts...)
m.Config.Cluster.Disabled = false
m.Config.Cluster.Coordinator = isCoordinator
@ -103,7 +95,7 @@ func NewMainWithCluster(isCoordinator bool, opts ...MainOpt) *Main {
// MustRunMainWithCluster ruturns a running array of *Main where
// all nodes are joined via memberlist (i.e. clustering enabled).
func MustRunMainWithCluster(t *testing.T, size int, opts ...MainOpt) []*Main {
func MustRunMainWithCluster(t *testing.T, size int, opts ...server.CommandOption) []*Main {
ma, err := runMainWithCluster(size, opts...)
if err != nil {
t.Fatalf("new main array with cluster: %v", err)
@ -113,7 +105,7 @@ func MustRunMainWithCluster(t *testing.T, size int, opts ...MainOpt) []*Main {
// runMainWithCluster runs an array of *Main where all nodes are
// joined via memberlist (i.e. clustering enabled).
func runMainWithCluster(size int, opts ...MainOpt) ([]*Main, error) {
func runMainWithCluster(size int, opts ...server.CommandOption) ([]*Main, error) {
if size == 0 {
return nil, errors.New("cluster must contain at least one node")
}
@ -164,7 +156,7 @@ func (m *Main) Reopen() error {
// Create new main with the same config.
config := m.Command.Config
m.Command = server.NewCommand(os.Stdin, os.Stdout, os.Stderr)
m.Command = server.NewCommand(os.Stdin, os.Stdout, os.Stderr, server.OptCommandServerOptions(m.ServerOptions...))
m.Command.Config = config
err := m.SetupServer()
if err != nil {