influxdb/cluster/service_test.go

111 lines
2.5 KiB
Go
Raw Normal View History

2015-05-30 20:00:46 +00:00
package cluster_test
import (
"fmt"
2015-06-05 22:54:12 +00:00
"net"
2015-05-30 20:00:46 +00:00
"time"
2015-06-05 22:54:12 +00:00
"github.com/influxdb/influxdb/cluster"
"github.com/influxdb/influxdb/influxql"
"github.com/influxdb/influxdb/models"
2016-01-19 22:58:34 +00:00
"github.com/influxdb/influxdb/services/meta"
2015-06-05 22:54:12 +00:00
"github.com/influxdb/influxdb/tcp"
2015-05-30 20:00:46 +00:00
"github.com/influxdb/influxdb/tsdb"
)
2016-01-19 22:58:34 +00:00
type metaClient struct {
2015-05-30 20:00:46 +00:00
host string
}
2016-01-19 22:58:34 +00:00
func (m *metaClient) DataNode(nodeID uint64) (*meta.NodeInfo, error) {
2015-05-30 20:00:46 +00:00
return &meta.NodeInfo{
2016-01-19 22:58:34 +00:00
ID: nodeID,
TCPHost: m.host,
2015-05-30 20:00:46 +00:00
}, nil
}
type testService struct {
nodeID uint64
ln net.Listener
muxln net.Listener
writeShardFunc func(shardID uint64, points []models.Point) error
createShardFunc func(database, policy string, shardID uint64) error
createMapperFunc func(shardID uint64, stmt influxql.Statement, chunkSize int) (tsdb.Mapper, error)
2015-05-30 20:00:46 +00:00
}
func newTestWriteService(f func(shardID uint64, points []models.Point) error) testService {
2015-06-05 22:54:12 +00:00
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
panic(err)
}
mux := tcp.NewMux()
muxln := mux.Listen(cluster.MuxHeader)
go mux.Serve(ln)
2015-05-30 20:00:46 +00:00
return testService{
writeShardFunc: f,
2015-06-05 22:54:12 +00:00
ln: ln,
muxln: muxln,
}
}
func (ts *testService) Close() {
if ts.ln != nil {
ts.ln.Close()
2015-05-30 20:00:46 +00:00
}
}
type serviceResponses []serviceResponse
type serviceResponse struct {
shardID uint64
ownerID uint64
points []models.Point
2015-05-30 20:00:46 +00:00
}
func (t testService) WriteToShard(shardID uint64, points []models.Point) error {
return t.writeShardFunc(shardID, points)
2015-05-30 20:00:46 +00:00
}
func (t testService) CreateShard(database, policy string, shardID uint64) error {
return t.createShardFunc(database, policy, shardID)
}
func (t testService) CreateMapper(shardID uint64, stmt influxql.Statement, chunkSize int) (tsdb.Mapper, error) {
return t.createMapperFunc(shardID, stmt, chunkSize)
}
func writeShardSuccess(shardID uint64, points []models.Point) error {
2015-05-30 20:00:46 +00:00
responses <- &serviceResponse{
shardID: shardID,
points: points,
}
return nil
}
func writeShardFail(shardID uint64, points []models.Point) error {
2015-05-30 20:00:46 +00:00
return fmt.Errorf("failed to write")
}
func writeShardSlow(shardID uint64, points []models.Point) error {
time.Sleep(1 * time.Second)
return nil
}
2015-05-30 20:00:46 +00:00
var responses = make(chan *serviceResponse, 1024)
func (testService) ResponseN(n int) ([]*serviceResponse, error) {
var a []*serviceResponse
for {
select {
case r := <-responses:
a = append(a, r)
if len(a) == n {
return a, nil
}
case <-time.After(time.Second):
return a, fmt.Errorf("unexpected response count: expected: %d, actual: %d", n, len(a))
}
}
}