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

This commit is contained in:
Frank Schroeder 2017-05-23 12:17:06 +02:00
parent 9d4529972e
commit a433d13b20
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()
args := &structs.RegisterRequest{
Datacenter: "dc1", // register a service after the blocking call
Node: "foo", // in order to unblock it.
Address: "127.0.0.1", time.AfterFunc(100*time.Millisecond, func() {
args := &structs.RegisterRequest{
Datacenter: "dc1",
Node: "foo",
Address: "127.0.0.1",
}
var out struct{}
errch <- a.RPC("Catalog.Register", args, &out)
})
// now block
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/catalog/nodes?wait=3s&index=%d", out.Index+1), nil)
resp := httptest.NewRecorder()
obj, err := a.srv.CatalogNodes(resp, req)
if err != nil {
errch <- err
} }
var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil { // Should block for a while
t.Fatalf("err: %v", err) if d := time.Now().Sub(start); d < 50*time.Millisecond {
errch <- fmt.Errorf("too fast: %v", d)
} }
if idx := getIndex(t, resp); idx <= out.Index {
errch <- fmt.Errorf("bad: %v", idx)
}
nodes := obj.(structs.Nodes)
if len(nodes) != 2 {
errch <- fmt.Errorf("bad: %v", obj)
}
errch <- nil
}() }()
// Do a blocking read // wait for both go routines to return
req, _ := http.NewRequest("GET", fmt.Sprintf("/v1/catalog/nodes?wait=60s&index=%d", out.Index), nil) if err := <-errch; err != nil {
resp := httptest.NewRecorder() t.Fatal(err)
obj, err := a.srv.CatalogNodes(resp, req)
if err != nil {
t.Fatalf("err: %v", err)
} }
if err := <-errch; err != nil {
// Should block for a while t.Fatal(err)
if time.Now().Sub(start) < 50*time.Millisecond {
t.Fatalf("too fast")
}
if idx := getIndex(t, resp); idx <= out.Index {
t.Fatalf("bad: %v", idx)
}
nodes := obj.(structs.Nodes)
if len(nodes) != 2 {
t.Fatalf("bad: %v", obj)
} }
} }