test: call t.Fatal() from main go routine

pull/3037/head
Frank Schroeder 2017-05-23 12:17:06 +02:00
parent 3e39f04527
commit fe777852cd
No known key found for this signature in database
GPG Key ID: 4D65C6EAEC87DECD
1 changed files with 44 additions and 29 deletions

View File

@ -289,41 +289,56 @@ func TestCatalogNodes_Blocking(t *testing.T) {
t.Fatalf("err: %v", err) t.Fatalf("err: %v", err)
} }
// Do an update in a little while // t.Fatal must be called from the main go routine
start := time.Now() // of the test. Because of this we cannot call
// t.Fatal from within the go routines and use
// an error channel instead.
errch := make(chan error, 2)
go func() { go func() {
time.Sleep(50 * time.Millisecond) start := time.Now()
// register a service after the blocking call
// in order to unblock it.
time.AfterFunc(100*time.Millisecond, func() {
args := &structs.RegisterRequest{ args := &structs.RegisterRequest{
Datacenter: "dc1", Datacenter: "dc1",
Node: "foo", Node: "foo",
Address: "127.0.0.1", Address: "127.0.0.1",
} }
var out struct{} var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil { errch <- a.RPC("Catalog.Register", args, &out)
t.Fatalf("err: %v", err) })
}
}()
// Do a blocking read // now block
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/catalog/nodes?wait=60s&index=%d", out.Index), nil) req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/catalog/nodes?wait=3s&index=%d", out.Index+1), nil)
resp := httptest.NewRecorder() resp := httptest.NewRecorder()
obj, err := a.srv.CatalogNodes(resp, req) obj, err := a.srv.CatalogNodes(resp, req)
if err != nil { if err != nil {
t.Fatalf("err: %v", err) errch <- err
} }
// Should block for a while // Should block for a while
if time.Now().Sub(start) < 50*time.Millisecond { if d := time.Now().Sub(start); d < 50*time.Millisecond {
t.Fatalf("too fast") errch <- fmt.Errorf("too fast: %v", d)
} }
if idx := getIndex(t, resp); idx <= out.Index { if idx := getIndex(t, resp); idx <= out.Index {
t.Fatalf("bad: %v", idx) errch <- fmt.Errorf("bad: %v", idx)
} }
nodes := obj.(structs.Nodes) nodes := obj.(structs.Nodes)
if len(nodes) != 2 { if len(nodes) != 2 {
t.Fatalf("bad: %v", obj) errch <- fmt.Errorf("bad: %v", obj)
}
errch <- nil
}()
// wait for both go routines to return
if err := <-errch; err != nil {
t.Fatal(err)
}
if err := <-errch; err != nil {
t.Fatal(err)
} }
} }