GoPLS Viewer

Home|gopls/go/analysis/passes/testinggoroutine/testdata/src/a/a.go
1// Copyright 2020 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package a
6
7import (
8    "log"
9    "sync"
10    "testing"
11)
12
13func TestBadFatalf(t *testing.T) {
14    var wg sync.WaitGroup
15    defer wg.Wait()
16
17    for i := 0i < 2i++ {
18        wg.Add(1)
19        go func(id int) {
20            defer wg.Done()
21            t.Fatalf("TestFailed: id = %v\n"id// want "call to .+T.+Fatalf from a non-test goroutine"
22        }(i)
23    }
24}
25
26func TestOKErrorf(t *testing.T) {
27    var wg sync.WaitGroup
28    defer wg.Wait()
29
30    for i := 0i < 2i++ {
31        wg.Add(1)
32        go func(id int) {
33            defer wg.Done()
34            t.Errorf("TestFailed: id = %v\n"id)
35        }(i)
36    }
37}
38
39func TestBadFatal(t *testing.T) {
40    var wg sync.WaitGroup
41    defer wg.Wait()
42
43    for i := 0i < 2i++ {
44        wg.Add(1)
45        go func(id int) {
46            defer wg.Done()
47            t.Fatal("TestFailed"// want "call to .+T.+Fatal from a non-test goroutine"
48        }(i)
49    }
50}
51
52func f(t *testing.T_ string) {
53    t.Fatal("TestFailed")
54}
55
56func g() {}
57
58func TestBadFatalIssue47470(t *testing.T) {
59    go f(t"failed test 1"// want "call to .+T.+Fatal from a non-test goroutine"
60
61    g := func(t *testing.T_ string) {
62        t.Fatal("TestFailed")
63    }
64    go g(t"failed test 2"// want "call to .+T.+Fatal from a non-test goroutine"
65}
66
67func BenchmarkBadFatalf(b *testing.B) {
68    var wg sync.WaitGroup
69    defer wg.Wait()
70
71    for i := 0i < b.Ni++ {
72        wg.Add(1)
73        go func(id int) {
74            defer wg.Done()
75            b.Fatalf("TestFailed: id = %v\n"id// want "call to .+B.+Fatalf from a non-test goroutine"
76        }(i)
77    }
78}
79
80func BenchmarkBadFatal(b *testing.B) {
81    var wg sync.WaitGroup
82    defer wg.Wait()
83
84    for i := 0i < b.Ni++ {
85        wg.Add(1)
86        go func(id int) {
87            defer wg.Done()
88            b.Fatal("TestFailed"// want "call to .+B.+Fatal from a non-test goroutine"
89        }(i)
90    }
91}
92
93func BenchmarkOKErrorf(b *testing.B) {
94    var wg sync.WaitGroup
95    defer wg.Wait()
96
97    for i := 0i < b.Ni++ {
98        wg.Add(1)
99        go func(id int) {
100            defer wg.Done()
101            b.Errorf("TestFailed: %d"i)
102        }(i)
103    }
104}
105
106func BenchmarkBadFatalGoGo(b *testing.B) {
107    var wg sync.WaitGroup
108    defer wg.Wait()
109
110    for i := 0i < b.Ni++ {
111        wg.Add(1)
112        go func(id int) {
113            go func() {
114                defer wg.Done()
115                b.Fatal("TestFailed"// want "call to .+B.+Fatal from a non-test goroutine"
116            }()
117        }(i)
118    }
119
120    if false {
121        defer b.Fatal("here")
122    }
123
124    if true {
125        go func() {
126            b.Fatal("in here"// want "call to .+B.+Fatal from a non-test goroutine"
127        }()
128    }
129
130    func() {
131        func() {
132            func() {
133                func() {
134                    go func() {
135                        b.Fatal("Here"// want "call to .+B.+Fatal from a non-test goroutine"
136                    }()
137                }()
138            }()
139        }()
140    }()
141
142    _ = 10 * 10
143    _ = func() bool {
144        go b.Fatal("Failed"// want "call to .+B.+Fatal from a non-test goroutine"
145        return true
146    }
147
148    defer func() {
149        go b.Fatal("Here"// want "call to .+B.+Fatal from a non-test goroutine"
150    }()
151}
152
153func BenchmarkBadSkip(b *testing.B) {
154    for i := 0i < b.Ni++ {
155        if i == 100 {
156            go b.Skip("Skipping"// want "call to .+B.+Skip from a non-test goroutine"
157        }
158        if i == 22 {
159            go func() {
160                go func() {
161                    b.Skip("Skipping now"// want "call to .+B.+Skip from a non-test goroutine"
162                }()
163            }()
164        }
165    }
166}
167
168func TestBadSkip(t *testing.T) {
169    for i := 0i < 1000i++ {
170        if i == 100 {
171            go t.Skip("Skipping"// want "call to .+T.+Skip from a non-test goroutine"
172        }
173        if i == 22 {
174            go func() {
175                go func() {
176                    t.Skip("Skipping now"// want "call to .+T.+Skip from a non-test goroutine"
177                }()
178            }()
179        }
180    }
181}
182
183func BenchmarkBadFailNow(b *testing.B) {
184    for i := 0i < b.Ni++ {
185        if i == 100 {
186            go b.FailNow() // want "call to .+B.+FailNow from a non-test goroutine"
187        }
188        if i == 22 {
189            go func() {
190                go func() {
191                    b.FailNow() // want "call to .+B.+FailNow from a non-test goroutine"
192                }()
193            }()
194        }
195    }
196}
197
198func TestBadFailNow(t *testing.T) {
199    for i := 0i < 1000i++ {
200        if i == 100 {
201            go t.FailNow() // want "call to .+T.+FailNow from a non-test goroutine"
202        }
203        if i == 22 {
204            go func() {
205                go func() {
206                    t.FailNow() // want "call to .+T.+FailNow from a non-test goroutine"
207                }()
208            }()
209        }
210    }
211}
212
213func TestBadWithLoopCond(ty *testing.T) {
214    var wg sync.WaitGroup
215    defer wg.Wait()
216
217    for i := 0i < 10i++ {
218        wg.Add(1)
219        go func(id int) {
220            defer ty.Fatalf("Why"// want "call to .+T.+Fatalf from a non-test goroutine"
221            go func() {
222                for j := 0j < 2ty.FailNow() { // want "call to .+T.+FailNow from"
223                    j++
224                    ty.Errorf("Done here")
225                }
226            }()
227        }(i)
228    }
229}
230
231type customType int
232
233func (ct *customTypeFatalf(fmtSpec stringargs ...interface{}) {
234    if fmtSpec == "" {
235        panic("empty format specifier")
236    }
237}
238
239func (ct *customTypeFailNow() {}
240func (ct *customTypeSkip()    {}
241
242func TestWithLogFatalf(t *testing.T) {
243    var wg sync.WaitGroup
244    defer wg.Wait()
245
246    for i := 0i < 10i++ {
247        wg.Add(1)
248        go func(id int) {
249            go func() {
250                for j := 0j < 2j++ {
251                    log.Fatal("Done here")
252                }
253            }()
254        }(i)
255    }
256}
257
258func TestWithCustomType(t *testing.T) {
259    var wg sync.WaitGroup
260    defer wg.Wait()
261
262    ct := new(customType)
263    defer ct.FailNow()
264    defer ct.Skip()
265
266    for i := 0i < 10i++ {
267        wg.Add(1)
268        go func(id int) {
269            go func() {
270                for j := 0j < 2j++ {
271                    ct.Fatalf("Done here: %d"i)
272                }
273            }()
274        }(i)
275    }
276}
277
278func TestIssue48124(t *testing.T) {
279    go h()
280}
281
MembersX
BenchmarkBadFailNow.i
customType
customType.FailNow
TestWithLogFatalf.t
TestBadFatalf.t
TestBadFatalIssue47470.t
BenchmarkBadFatal.b
TestWithCustomType.ct
g
customType.Fatalf.fmtSpec
TestWithLogFatalf.i
TestBadSkip.i
TestBadFailNow.i
customType.Skip
TestWithCustomType
log
TestOKErrorf.i
f
TestBadFailNow
TestBadWithLoopCond
TestBadWithLoopCond.ty
TestOKErrorf.t
TestOKErrorf.wg
BenchmarkBadFatalf.wg
BenchmarkOKErrorf.i
TestBadFatalf
TestBadFatal
f.t
BenchmarkBadFatal.wg
TestBadFatalf.i
TestBadFatal.i
BenchmarkBadFatalf.b
TestWithCustomType.i
TestBadSkip.t
TestBadWithLoopCond.i
customType.Fatalf.args
customType.FailNow.ct
TestIssue48124
TestBadFatalIssue47470
BenchmarkBadSkip.b
TestBadSkip
BenchmarkBadFailNow
TestWithCustomType.wg
BenchmarkBadFatal
BenchmarkOKErrorf.b
BenchmarkBadSkip
customType.Skip.ct
TestWithCustomType.BlockStmt.BlockStmt.BlockStmt.j
sync
BenchmarkBadFatalGoGo
customType.Fatalf.ct
TestBadFatalf.wg
TestBadWithLoopCond.BlockStmt.BlockStmt.BlockStmt.j
TestWithLogFatalf
BenchmarkBadFailNow.b
TestBadFailNow.t
TestBadWithLoopCond.wg
customType.Fatalf
TestWithLogFatalf.BlockStmt.BlockStmt.BlockStmt.j
BenchmarkBadFatal.i
BenchmarkOKErrorf.wg
BenchmarkBadSkip.i
TestBadFatal.t
BenchmarkBadFatalGoGo.i
TestWithLogFatalf.wg
BenchmarkOKErrorf
BenchmarkBadFatalGoGo.b
testing
TestOKErrorf
BenchmarkBadFatalf.i
TestWithCustomType.t
TestIssue48124.t
TestBadFatal.wg
BenchmarkBadFatalf
BenchmarkBadFatalGoGo.wg
Members
X