Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Wait for fake influxd to have started
Browse files Browse the repository at this point in the history
Two fake influxd types have been implemented for use in tests. The
funcs which started them would sometimes return before the fake server
was listening, causing test failures. The start funcs now wait for the
listener to have started before returning, eliminating occasional
spurious test failures.
  • Loading branch information
mjs committed Apr 4, 2018
1 parent 4c5d1a8 commit 2d4ee5e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
10 changes: 10 additions & 0 deletions spouttest/influxd.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"log"
"net/http"
"sync"
"time"
)

// RunFakeInfluxd starts a fake influxd instance with the HTTP port
Expand All @@ -31,12 +32,19 @@ func RunFakeInfluxd(port int) *FakeInfluxDB {
server: &http.Server{
Addr: fmt.Sprintf(":%d", port),
},
ready: make(chan struct{}),
lines: make(map[string][]string),
}

f.wg.Add(1)
go f.run()

select {
case <-f.ready:
case <-time.After(LongWait):
panic("FakeInfluxDB failed to start")
}

return f
}

Expand All @@ -45,6 +53,7 @@ func RunFakeInfluxd(port int) *FakeInfluxDB {
type FakeInfluxDB struct {
server *http.Server
wg sync.WaitGroup
ready chan struct{}

mu sync.Mutex
lines map[string][]string
Expand Down Expand Up @@ -78,6 +87,7 @@ func (f *FakeInfluxDB) run() {
f.server.Handler = mux

log.Printf("fake influxd listening on %s", f.server.Addr)
close(f.ready)
f.server.ListenAndServe()
}

Expand Down
10 changes: 10 additions & 0 deletions writer/writer_medium_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ type testInfluxd struct {
server *http.Server
wg sync.WaitGroup
Writes chan string
ready chan struct{}
}

func runTestInfluxd() *testInfluxd {
Expand All @@ -319,9 +320,17 @@ func runTestInfluxd() *testInfluxd {
Addr: fmt.Sprintf(":%d", influxPort),
},
Writes: make(chan string, 99),
ready: make(chan struct{}),
}

s.wg.Add(1)
go s.run()
select {
case <-s.ready:
case <-time.After(spouttest.LongWait):
panic("testInfluxd failed to start")
}

return s
}

Expand Down Expand Up @@ -353,6 +362,7 @@ func (s *testInfluxd) run() {
mux := http.NewServeMux()
mux.HandleFunc("/write", s.handleWrite)
s.server.Handler = mux
close(s.ready)
s.server.ListenAndServe()
}

Expand Down

0 comments on commit 2d4ee5e

Please sign in to comment.