Skip to content

Commit

Permalink
fix/test: t.Fatal should be called from running the test or benchmark…
Browse files Browse the repository at this point in the history
… func

The linter complains: the goroutine calls T.Fatal, which must be called in the
same goroutine as the testSA2002(default).

testing.T.Fail will calls testing.common.FailNow(), this function will marks
the test failing and call runtime.GoExit(). Sure, it could let the calling
goroutine exit, but this is a misuse.

t.Fail should be used for marks the test failing and let the goroutine which
runs this test or the benchmark func exit. So, just use t.Error+return instead.
  • Loading branch information
hitzhangjie committed Aug 22, 2023
1 parent b894f41 commit 9b6a266
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 12 deletions.
8 changes: 5 additions & 3 deletions ast/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,14 @@ func TestGC_Encode(t *testing.T) {
defer wg.Done()
root, err := NewSearcher(_TwitterJson).GetByPath()
if err != nil {
t.Fatal(err)
t.Error(err)
return
}
root.Load()
_, err = root.MarshalJSON()
if err != nil {
t.Fatal(err)
t.Error(err)
return
}
runtime.GC()
}(wg)
Expand Down Expand Up @@ -228,4 +230,4 @@ func TestEncodeNone(t *testing.T) {
out, err = n.MarshalJSON()
require.NoError(t, err)
require.Equal(t, `[null]`, string(out))
}
}
5 changes: 3 additions & 2 deletions ast/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,8 @@ func TestGC_Parse(t *testing.T) {
defer wg.Done()
_, _, err := Loads(_TwitterJson)
if err != nil {
t.Fatal(err)
t.Error(err)
return
}
runtime.GC()
}(wg)
Expand Down Expand Up @@ -351,4 +352,4 @@ func BenchmarkParseSeven_Parallel_Sonic(b *testing.B) {
}
}
})
}
}
5 changes: 3 additions & 2 deletions ast/search_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ func TestGC_Search(t *testing.T) {
defer wg.Done()
_, err := NewSearcher(_TwitterJson).GetByPath("statuses", 0, "id")
if err != nil {
t.Fatal(err)
t.Error(err)
return
}
runtime.GC()
}(wg)
Expand Down Expand Up @@ -408,4 +409,4 @@ func BenchmarkSetOne_Parallel_Sonic(b *testing.B) {
_, _ = node.Set("id", n)
}
})
}
}
6 changes: 4 additions & 2 deletions decoder/decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ func TestGC(t *testing.T) {
var w interface{}
out, err := decode(TwitterJson, &w, true)
if err != nil {
t.Fatal(err)
t.Error(err)
return
}
if out != len(TwitterJson) {
t.Fatal(out)
t.Error(out)
return
}
runtime.GC()
}(wg)
Expand Down
8 changes: 5 additions & 3 deletions encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,12 @@ func TestGC(t *testing.T) {
defer wg.Done()
out, err := Encode(_GenericValue, 0)
if err != nil {
t.Fatal(err)
t.Error(err)
return
}
if len(out) != size {
t.Fatal(len(out), size)
t.Error(len(out), size)
return
}
runtime.GC()
}(wg, n)
Expand Down Expand Up @@ -543,4 +545,4 @@ func BenchmarkEncode_Float32(b *testing.B) {
b.Run(name, lib.test)
}
}
}
}

0 comments on commit 9b6a266

Please sign in to comment.