Skip to content

Commit

Permalink
refactor: Use t.Fatal instead of t.Error
Browse files Browse the repository at this point in the history
This commit changes instances of t.Error followed by an immediate return
with t.Fatal, where valid.

Up until today I thought that t.Fatal didn't run defers.

I was [wrong](#386 (comment))
as @dominikh pointed out.
  • Loading branch information
Tomás Senart committed Apr 10, 2019
1 parent 8c061dc commit 7bf09f4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
12 changes: 4 additions & 8 deletions internal/resolver/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ func TestResolver(t *testing.T) {

res, err := NewResolver([]string{ds.PacketConn.LocalAddr().String()})
if err != nil {
t.Errorf("error from NewResolver: %s", err)
return
t.Fatalf("error from NewResolver: %s", err)
}
net.DefaultResolver = res

Expand All @@ -96,19 +95,16 @@ func TestResolver(t *testing.T) {

_, hport, err := net.SplitHostPort(tsurl.Host)
if err != nil {
t.Errorf("could not parse port from httptest url %s: %s", ts.URL, err)
return
t.Fatalf("could not parse port from httptest url %s: %s", ts.URL, err)
}
tsurl.Host = net.JoinHostPort(fakeDomain, hport)
resp, err := http.Get(tsurl.String())
if err != nil {
t.Errorf("failed resolver round trip: %s", err)
return
t.Fatalf("failed resolver round trip: %s", err)
}
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Errorf("failed to read respose body")
return
t.Fatalf("failed to read respose body")
}
if strings.TrimSpace(string(body)) != payload {
t.Errorf("body mismatch, got: '%s', expected: '%s'", body, payload)
Expand Down
15 changes: 5 additions & 10 deletions lib/attack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,17 +276,15 @@ func TestUnixSocket(t *testing.T) {

socketDir, err := ioutil.TempDir("", "vegata")
if err != nil {
t.Error("Failed to create socket dir", err)
return
t.Fatal("Failed to create socket dir", err)
}
defer os.RemoveAll(socketDir)
socketFile := filepath.Join(socketDir, "test.sock")

unixListener, err := net.Listen("unix", socketFile)

if err != nil {
t.Error("Failed to listen on unix socket", err)
return
t.Fatal("Failed to listen on unix socket", err)
}

server := http.Server{
Expand All @@ -301,17 +299,15 @@ func TestUnixSocket(t *testing.T) {
start := time.Now()
for {
if time.Since(start) > 1*time.Second {
t.Error("Server didn't listen on unix socket in time")
return
t.Fatal("Server didn't listen on unix socket in time")
}
_, err := os.Stat(socketFile)
if err == nil {
break
} else if os.IsNotExist(err) {
time.Sleep(10 * time.Millisecond)
} else {
t.Error("unexpected error from unix socket", err)
return
t.Fatal("unexpected error from unix socket", err)
}
}

Expand All @@ -320,8 +316,7 @@ func TestUnixSocket(t *testing.T) {
tr := NewStaticTargeter(Target{Method: "GET", URL: "http://anyserver/"})
res := atk.hit(tr, "")
if !bytes.Equal(res.Body, body) {
t.Errorf("got: %s, want: %s", string(res.Body), string(body))
return
t.Fatalf("got: %s, want: %s", string(res.Body), string(body))
}
}

Expand Down

0 comments on commit 7bf09f4

Please sign in to comment.