Skip to content

Commit 2341631

Browse files
committed
all: sprinkle t.Parallel on some slow tests
I used the slowtests.go tool as described in https://golang.org/cl/32684 on packages that stood out. go test -short std drops from ~56 to ~52 seconds. This isn't a huge win, but it was mostly an exercise. Updates #17751 Change-Id: I9f3402e36a038d71e662d06ce2c1d52f6c4b674d Reviewed-on: https://go-review.googlesource.com/32751 Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Lance Taylor <[email protected]>
1 parent 3f69909 commit 2341631

18 files changed

+103
-22
lines changed

src/compress/flate/deflate_test.go

+7
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,7 @@ func testToFromWithLimit(t *testing.T, input []byte, name string, limit [11]int)
342342
}
343343

344344
func TestDeflateInflate(t *testing.T) {
345+
t.Parallel()
345346
for i, h := range deflateInflateTests {
346347
testToFromWithLimit(t, h.in, fmt.Sprintf("#%d", i), [11]int{})
347348
}
@@ -376,6 +377,7 @@ var deflateInflateStringTests = []deflateInflateStringTest{
376377
}
377378

378379
func TestDeflateInflateString(t *testing.T) {
380+
t.Parallel()
379381
if testing.Short() && testenv.Builder() == "" {
380382
t.Skip("skipping in short mode")
381383
}
@@ -463,6 +465,7 @@ func TestRegression2508(t *testing.T) {
463465
}
464466

465467
func TestWriterReset(t *testing.T) {
468+
t.Parallel()
466469
for level := 0; level <= 9; level++ {
467470
if testing.Short() && level > 1 {
468471
break
@@ -559,6 +562,7 @@ func testResetOutput(t *testing.T, newWriter func(w io.Writer) (*Writer, error))
559562
// compressor.encSpeed method (0, 16, 128), as well as near maxStoreBlockSize
560563
// (65535).
561564
func TestBestSpeed(t *testing.T) {
565+
t.Parallel()
562566
abc := make([]byte, 128)
563567
for i := range abc {
564568
abc[i] = byte(i)
@@ -648,6 +652,7 @@ func (w *failWriter) Write(b []byte) (int, error) {
648652
}
649653

650654
func TestWriterPersistentError(t *testing.T) {
655+
t.Parallel()
651656
d, err := ioutil.ReadFile("../testdata/Mark.Twain-Tom.Sawyer.txt")
652657
if err != nil {
653658
t.Fatalf("ReadFile: %v", err)
@@ -684,6 +689,7 @@ func TestWriterPersistentError(t *testing.T) {
684689
}
685690

686691
func TestBestSpeedMatch(t *testing.T) {
692+
t.Parallel()
687693
cases := []struct {
688694
previous, current []byte
689695
t, s, want int32
@@ -800,6 +806,7 @@ func TestBestSpeedMatch(t *testing.T) {
800806
}
801807

802808
func TestBestSpeedMaxMatchOffset(t *testing.T) {
809+
t.Parallel()
803810
const abc, xyz = "abcdefgh", "stuvwxyz"
804811
for _, matchBefore := range []bool{false, true} {
805812
for _, extra := range []int{0, inputMargin - 1, inputMargin, inputMargin + 1, 2 * inputMargin} {

src/compress/flate/flate_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ func TestTruncatedStreams(t *testing.T) {
281281
//
282282
// See https://github.com/google/go-github/pull/317 for background.
283283
func TestReaderEarlyEOF(t *testing.T) {
284+
t.Parallel()
284285
testSizes := []int{
285286
1, 2, 3, 4, 5, 6, 7, 8,
286287
100, 1000, 10000, 100000,

src/compress/flate/writer_test.go

+3
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ func (e *errorWriter) Write(b []byte) (int, error) {
5656

5757
// Test if errors from the underlying writer is passed upwards.
5858
func TestWriteError(t *testing.T) {
59+
t.Parallel()
5960
buf := new(bytes.Buffer)
6061
n := 65536
6162
if !testing.Short() {
@@ -113,13 +114,15 @@ func TestWriteError(t *testing.T) {
113114
// Test if two runs produce identical results
114115
// even when writing different sizes to the Writer.
115116
func TestDeterministic(t *testing.T) {
117+
t.Parallel()
116118
for i := 0; i <= 9; i++ {
117119
t.Run(fmt.Sprint("L", i), func(t *testing.T) { testDeterministic(i, t) })
118120
}
119121
t.Run("LM2", func(t *testing.T) { testDeterministic(-2, t) })
120122
}
121123

122124
func testDeterministic(i int, t *testing.T) {
125+
t.Parallel()
123126
// Test so much we cross a good number of block boundaries.
124127
var length = maxStoreBlockSize*30 + 500
125128
if testing.Short() {

src/crypto/tls/handshake_client_test.go

+21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"path/filepath"
2323
"strconv"
2424
"strings"
25+
"sync"
2526
"testing"
2627
"time"
2728
)
@@ -420,7 +421,26 @@ func (test *clientTest) run(t *testing.T, write bool) {
420421
}
421422
}
422423

424+
var (
425+
didParMu sync.Mutex
426+
didPar = map[*testing.T]bool{}
427+
)
428+
429+
// setParallel calls t.Parallel once. If you call it twice, it would
430+
// panic.
431+
func setParallel(t *testing.T) {
432+
didParMu.Lock()
433+
v := didPar[t]
434+
didPar[t] = true
435+
didParMu.Unlock()
436+
if !v {
437+
t.Parallel()
438+
}
439+
}
440+
423441
func runClientTestForVersion(t *testing.T, template *clientTest, prefix, option string) {
442+
setParallel(t)
443+
424444
test := *template
425445
test.name = prefix + test.name
426446
if len(test.command) == 0 {
@@ -1356,6 +1376,7 @@ func TestAlertFlushing(t *testing.T) {
13561376
}
13571377

13581378
func TestHandshakeRace(t *testing.T) {
1379+
t.Parallel()
13591380
// This test races a Read and Write to try and complete a handshake in
13601381
// order to provide some evidence that there are no races or deadlocks
13611382
// in the handshake locking.

src/crypto/tls/handshake_server_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -660,6 +660,7 @@ func (test *serverTest) run(t *testing.T, write bool) {
660660
}
661661

662662
func runServerTestForVersion(t *testing.T, template *serverTest, prefix, option string) {
663+
setParallel(t)
663664
test := *template
664665
test.name = prefix + test.name
665666
if len(test.command) == 0 {
@@ -1054,6 +1055,7 @@ FMBexFe01MNvja5oHt1vzobhfm6ySD6B5U7ixohLZNz1MLvT/2XMW/TdtWo+PtAd
10541055
-----END EC PRIVATE KEY-----`
10551056

10561057
func TestClientAuth(t *testing.T) {
1058+
setParallel(t)
10571059
var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath string
10581060

10591061
if *update {

src/crypto/tls/tls_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ var keyPairTests = []struct {
9999
}
100100

101101
func TestX509KeyPair(t *testing.T) {
102+
t.Parallel()
102103
var pem []byte
103104
for _, test := range keyPairTests {
104105
pem = []byte(test.cert + test.key)

src/encoding/json/encode_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ func TestDuplicatedFieldDisappears(t *testing.T) {
378378
}
379379

380380
func TestStringBytes(t *testing.T) {
381+
t.Parallel()
381382
// Test that encodeState.stringBytes and encodeState.string use the same encoding.
382383
var r []rune
383384
for i := '\u0000'; i <= unicode.MaxRune; i++ {
@@ -616,6 +617,7 @@ var badFloatREs = []*regexp.Regexp{
616617
}
617618

618619
func TestMarshalFloat(t *testing.T) {
620+
t.Parallel()
619621
nfail := 0
620622
test := func(f float64, bits int) {
621623
vf := interface{}(f)

src/encoding/json/scanner_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ func TestCompactBig(t *testing.T) {
119119
}
120120

121121
func TestIndentBig(t *testing.T) {
122+
t.Parallel()
122123
initBig()
123124
var buf bytes.Buffer
124125
if err := Indent(&buf, jsonBig, "", "\t"); err != nil {

src/go/printer/printer_test.go

+33-22
Original file line numberDiff line numberDiff line change
@@ -197,12 +197,17 @@ var data = []entry{
197197
}
198198

199199
func TestFiles(t *testing.T) {
200+
t.Parallel()
200201
for _, e := range data {
201202
source := filepath.Join(dataDir, e.source)
202203
golden := filepath.Join(dataDir, e.golden)
203-
check(t, source, golden, e.mode)
204-
// TODO(gri) check that golden is idempotent
205-
//check(t, golden, golden, e.mode)
204+
mode := e.mode
205+
t.Run(e.source, func(t *testing.T) {
206+
t.Parallel()
207+
check(t, source, golden, mode)
208+
// TODO(gri) check that golden is idempotent
209+
//check(t, golden, golden, e.mode)
210+
})
206211
}
207212
}
208213

@@ -295,6 +300,7 @@ func testComment(t *testing.T, f *ast.File, srclen int, comment *ast.Comment) {
295300
// even if the position information of comments introducing newlines
296301
// is incorrect.
297302
func TestBadComments(t *testing.T) {
303+
t.Parallel()
298304
const src = `
299305
// first comment - text and position changed by test
300306
package p
@@ -481,6 +487,7 @@ func TestStmtLists(t *testing.T) {
481487
}
482488

483489
func TestBaseIndent(t *testing.T) {
490+
t.Parallel()
484491
// The testfile must not contain multi-line raw strings since those
485492
// are not indented (because their values must not change) and make
486493
// this test fail.
@@ -495,28 +502,31 @@ func TestBaseIndent(t *testing.T) {
495502
panic(err) // error in test
496503
}
497504

498-
var buf bytes.Buffer
499505
for indent := 0; indent < 4; indent++ {
500-
buf.Reset()
501-
(&Config{Tabwidth: tabwidth, Indent: indent}).Fprint(&buf, fset, file)
502-
// all code must be indented by at least 'indent' tabs
503-
lines := bytes.Split(buf.Bytes(), []byte{'\n'})
504-
for i, line := range lines {
505-
if len(line) == 0 {
506-
continue // empty lines don't have indentation
507-
}
508-
n := 0
509-
for j, b := range line {
510-
if b != '\t' {
511-
// end of indentation
512-
n = j
513-
break
506+
indent := indent
507+
t.Run(fmt.Sprint(indent), func(t *testing.T) {
508+
t.Parallel()
509+
var buf bytes.Buffer
510+
(&Config{Tabwidth: tabwidth, Indent: indent}).Fprint(&buf, fset, file)
511+
// all code must be indented by at least 'indent' tabs
512+
lines := bytes.Split(buf.Bytes(), []byte{'\n'})
513+
for i, line := range lines {
514+
if len(line) == 0 {
515+
continue // empty lines don't have indentation
516+
}
517+
n := 0
518+
for j, b := range line {
519+
if b != '\t' {
520+
// end of indentation
521+
n = j
522+
break
523+
}
524+
}
525+
if n < indent {
526+
t.Errorf("line %d: got only %d tabs; want at least %d: %q", i, n, indent, line)
514527
}
515528
}
516-
if n < indent {
517-
t.Errorf("line %d: got only %d tabs; want at least %d: %q", i, n, indent, line)
518-
}
519-
}
529+
})
520530
}
521531
}
522532

@@ -567,6 +577,7 @@ func (l *limitWriter) Write(buf []byte) (n int, err error) {
567577

568578
// Test whether the printer stops writing after the first error
569579
func TestWriteErrors(t *testing.T) {
580+
t.Parallel()
570581
const filename = "printer.go"
571582
src, err := ioutil.ReadFile(filename)
572583
if err != nil {

src/log/syslog/syslog_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ func startServer(n, la string, done chan<- string) (addr string, sock io.Closer,
134134
}
135135

136136
func TestWithSimulated(t *testing.T) {
137+
t.Parallel()
137138
msg := "Test 123"
138139
var transport []string
139140
for _, n := range []string{"unix", "unixgram", "udp", "tcp"} {
@@ -262,6 +263,7 @@ func check(t *testing.T, in, out string) {
262263
}
263264

264265
func TestWrite(t *testing.T) {
266+
t.Parallel()
265267
tests := []struct {
266268
pri Priority
267269
pre string

src/mime/multipart/multipart_test.go

+2
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func TestMultipartSlowInput(t *testing.T) {
125125
}
126126

127127
func testMultipart(t *testing.T, r io.Reader, onlyNewlines bool) {
128+
t.Parallel()
128129
reader := NewReader(r, "MyBoundary")
129130
buf := new(bytes.Buffer)
130131

@@ -755,6 +756,7 @@ func partsFromReader(r *Reader) ([]headerBody, error) {
755756
}
756757

757758
func TestParseAllSizes(t *testing.T) {
759+
t.Parallel()
758760
const maxSize = 5 << 10
759761
var buf bytes.Buffer
760762
body := strings.Repeat("a", maxSize)

0 commit comments

Comments
 (0)