Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
black-adder committed Jan 25, 2019
1 parent 519c1b5 commit 95544a0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 33 deletions.
2 changes: 1 addition & 1 deletion integration/config/config_01.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
http:
listenAddress: 0.0.0.0:0
listenAddress: 0.0.0.0:5678
readTimeout: 1m
writeTimeout: 1m
handler:
Expand Down
9 changes: 6 additions & 3 deletions integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ func TestRawQueryOrderBy(t *testing.T) {
expectedResults: 10,
},
}
client, closer := setup(t, "config/config_01.yaml")
_ = closer
// defer closer() // TODO(wjang): closer() is panic-ing in storage/shard.go
ts := newTestServerSetup(t, "config/config_01.yaml")
// defer ts.close(t) // TODO(wjang): close() is panic-ing in storage/shard.go
ts.startServer()
defer ts.stopServer(t)
ts.writeTestFixture(t)
client := ts.newClient()

for _, test := range tests {
resp, err := client.query([]byte(test.queryJSON))
Expand Down
85 changes: 56 additions & 29 deletions integration/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,17 @@ var (
logger = log.NullLogger
)

type closer func()
type testServerSetup struct {
db storage.Database
opts instrument.Options
cfg configuration

// Signals.
doneCh chan struct{}
closedCh chan struct{}
}

// setup sets up the database, a http server, and a client from the given config and returns the
// client and a closer that should be called once the tests are complete.
func setup(t *testing.T, configFname string) (client, closer) {
func newTestServerSetup(t *testing.T, configFname string) *testServerSetup {
var cfg configuration
if err := xconfig.LoadFile(&cfg, configFname, xconfig.Options{}); err != nil {
t.Fatalf("error loading config file %s: %v\n", configFname, err)
Expand All @@ -52,55 +58,76 @@ func setup(t *testing.T, configFname string) (client, closer) {
if err := db.Open(); err != nil {
t.Fatalf("error opening database: %v", err)
}
return &testServerSetup{
db: db,
opts: iOpts,
cfg: cfg,
doneCh: make(chan struct{}),
closedCh: make(chan struct{}),
}
}

doneCh := make(chan struct{})
closedCh := make(chan struct{})
func (ts *testServerSetup) startServer() {
go func() {
// TODO (wjang): pass in 0.0.0.0:0 instead, have an automatically generated port and use it.
if err := serve.Serve(
cfg.HTTP.ListenAddress,
cfg.HTTP.Handler.NewOptions(iOpts),
cfg.HTTP.NewServerOptions(),
db,
ts.cfg.HTTP.ListenAddress,
ts.cfg.HTTP.Handler.NewOptions(ts.opts),
ts.cfg.HTTP.NewServerOptions(),
ts.db,
logger,
doneCh,
ts.doneCh,
); err != nil {
logger.Fatalf("could not start serving traffic: %v", err)
}
close(closedCh)
close(ts.closedCh)
}()
}

closer := func() {
close(doneCh)
func (ts *testServerSetup) stopServer(t *testing.T) {
close(ts.doneCh)

select {
case <-closedCh:
t.Log("server closed clean")
case <-time.After(gracefulShutdownTimeout):
t.Logf("server closed due to %s timeout", gracefulShutdownTimeout.String())
}
select {
case <-ts.closedCh:
t.Log("server closed clean")
case <-time.After(gracefulShutdownTimeout):
t.Logf("server closed due to %s timeout", gracefulShutdownTimeout.String())
}
}

db.Close()
// TODO(wjang): delete the database files as well
func (ts *testServerSetup) stopDB(t *testing.T) {
// TODO(wjang): delete the database files as well
// TODO(wjang): Close() is panic-ing in storage/shard.go
err := ts.db.Close()
if err != nil {
t.Fatalf("error closing database: %v", err)
}
}

client := newClient(cfg.HTTP.ListenAddress)
func (ts *testServerSetup) close(t *testing.T) {
ts.stopServer(t)
ts.stopDB(t)
}

func (ts *testServerSetup) newClient() client {
client := newClient(ts.cfg.HTTP.ListenAddress)
for {
if client.serverIsHealthy() {
break
}
time.Sleep(time.Millisecond * 10)
}
return client
}

data, err := ioutil.ReadFile(cfg.InputFile)
func (ts *testServerSetup) writeTestFixture(t *testing.T) {
client := ts.newClient()

data, err := ioutil.ReadFile(ts.cfg.InputFile)
if err != nil {
t.Fatalf("cannot read %s: %v", cfg.InputFile, err)
t.Fatalf("cannot read %s: %v", ts.cfg.InputFile, err)
}

if err := client.write(data); err != nil {
// closer() // TODO(wjang): closer() is panic-ing in storage/shard.go
t.Fatalf("failed write to server: %v", err)
}

return client, closer
}

0 comments on commit 95544a0

Please sign in to comment.