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

Commit

Permalink
Merge pull request #62 from mjs/writer-test-reliability
Browse files Browse the repository at this point in the history
Wait for fake influxd to have started
  • Loading branch information
oplehto committed Apr 5, 2018
2 parents 8aa8500 + 2d4ee5e commit 767b788
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
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
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 767b788

Please sign in to comment.