Skip to content

Commit

Permalink
all: sprinkle t.Parallel on some slow tests
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
bradfitz committed Nov 4, 2016
1 parent 3f69909 commit 2341631
Show file tree
Hide file tree
Showing 18 changed files with 103 additions and 22 deletions.
7 changes: 7 additions & 0 deletions src/compress/flate/deflate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ func testToFromWithLimit(t *testing.T, input []byte, name string, limit [11]int)
}

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

func TestDeflateInflateString(t *testing.T) {
t.Parallel()
if testing.Short() && testenv.Builder() == "" {
t.Skip("skipping in short mode")
}
Expand Down Expand Up @@ -463,6 +465,7 @@ func TestRegression2508(t *testing.T) {
}

func TestWriterReset(t *testing.T) {
t.Parallel()
for level := 0; level <= 9; level++ {
if testing.Short() && level > 1 {
break
Expand Down Expand Up @@ -559,6 +562,7 @@ func testResetOutput(t *testing.T, newWriter func(w io.Writer) (*Writer, error))
// compressor.encSpeed method (0, 16, 128), as well as near maxStoreBlockSize
// (65535).
func TestBestSpeed(t *testing.T) {
t.Parallel()
abc := make([]byte, 128)
for i := range abc {
abc[i] = byte(i)
Expand Down Expand Up @@ -648,6 +652,7 @@ func (w *failWriter) Write(b []byte) (int, error) {
}

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

func TestBestSpeedMatch(t *testing.T) {
t.Parallel()
cases := []struct {
previous, current []byte
t, s, want int32
Expand Down Expand Up @@ -800,6 +806,7 @@ func TestBestSpeedMatch(t *testing.T) {
}

func TestBestSpeedMaxMatchOffset(t *testing.T) {
t.Parallel()
const abc, xyz = "abcdefgh", "stuvwxyz"
for _, matchBefore := range []bool{false, true} {
for _, extra := range []int{0, inputMargin - 1, inputMargin, inputMargin + 1, 2 * inputMargin} {
Expand Down
1 change: 1 addition & 0 deletions src/compress/flate/flate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ func TestTruncatedStreams(t *testing.T) {
//
// See https://github.com/google/go-github/pull/317 for background.
func TestReaderEarlyEOF(t *testing.T) {
t.Parallel()
testSizes := []int{
1, 2, 3, 4, 5, 6, 7, 8,
100, 1000, 10000, 100000,
Expand Down
3 changes: 3 additions & 0 deletions src/compress/flate/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ func (e *errorWriter) Write(b []byte) (int, error) {

// Test if errors from the underlying writer is passed upwards.
func TestWriteError(t *testing.T) {
t.Parallel()
buf := new(bytes.Buffer)
n := 65536
if !testing.Short() {
Expand Down Expand Up @@ -113,13 +114,15 @@ func TestWriteError(t *testing.T) {
// Test if two runs produce identical results
// even when writing different sizes to the Writer.
func TestDeterministic(t *testing.T) {
t.Parallel()
for i := 0; i <= 9; i++ {
t.Run(fmt.Sprint("L", i), func(t *testing.T) { testDeterministic(i, t) })
}
t.Run("LM2", func(t *testing.T) { testDeterministic(-2, t) })
}

func testDeterministic(i int, t *testing.T) {
t.Parallel()
// Test so much we cross a good number of block boundaries.
var length = maxStoreBlockSize*30 + 500
if testing.Short() {
Expand Down
21 changes: 21 additions & 0 deletions src/crypto/tls/handshake_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"path/filepath"
"strconv"
"strings"
"sync"
"testing"
"time"
)
Expand Down Expand Up @@ -420,7 +421,26 @@ func (test *clientTest) run(t *testing.T, write bool) {
}
}

var (
didParMu sync.Mutex
didPar = map[*testing.T]bool{}
)

// setParallel calls t.Parallel once. If you call it twice, it would
// panic.
func setParallel(t *testing.T) {
didParMu.Lock()
v := didPar[t]
didPar[t] = true
didParMu.Unlock()
if !v {
t.Parallel()
}
}

func runClientTestForVersion(t *testing.T, template *clientTest, prefix, option string) {
setParallel(t)

test := *template
test.name = prefix + test.name
if len(test.command) == 0 {
Expand Down Expand Up @@ -1356,6 +1376,7 @@ func TestAlertFlushing(t *testing.T) {
}

func TestHandshakeRace(t *testing.T) {
t.Parallel()
// This test races a Read and Write to try and complete a handshake in
// order to provide some evidence that there are no races or deadlocks
// in the handshake locking.
Expand Down
2 changes: 2 additions & 0 deletions src/crypto/tls/handshake_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,7 @@ func (test *serverTest) run(t *testing.T, write bool) {
}

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

func TestClientAuth(t *testing.T) {
setParallel(t)
var certPath, keyPath, ecdsaCertPath, ecdsaKeyPath string

if *update {
Expand Down
1 change: 1 addition & 0 deletions src/crypto/tls/tls_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ var keyPairTests = []struct {
}

func TestX509KeyPair(t *testing.T) {
t.Parallel()
var pem []byte
for _, test := range keyPairTests {
pem = []byte(test.cert + test.key)
Expand Down
2 changes: 2 additions & 0 deletions src/encoding/json/encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ func TestDuplicatedFieldDisappears(t *testing.T) {
}

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

func TestMarshalFloat(t *testing.T) {
t.Parallel()
nfail := 0
test := func(f float64, bits int) {
vf := interface{}(f)
Expand Down
1 change: 1 addition & 0 deletions src/encoding/json/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ func TestCompactBig(t *testing.T) {
}

func TestIndentBig(t *testing.T) {
t.Parallel()
initBig()
var buf bytes.Buffer
if err := Indent(&buf, jsonBig, "", "\t"); err != nil {
Expand Down
55 changes: 33 additions & 22 deletions src/go/printer/printer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,12 +197,17 @@ var data = []entry{
}

func TestFiles(t *testing.T) {
t.Parallel()
for _, e := range data {
source := filepath.Join(dataDir, e.source)
golden := filepath.Join(dataDir, e.golden)
check(t, source, golden, e.mode)
// TODO(gri) check that golden is idempotent
//check(t, golden, golden, e.mode)
mode := e.mode
t.Run(e.source, func(t *testing.T) {
t.Parallel()
check(t, source, golden, mode)
// TODO(gri) check that golden is idempotent
//check(t, golden, golden, e.mode)
})
}
}

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

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

var buf bytes.Buffer
for indent := 0; indent < 4; indent++ {
buf.Reset()
(&Config{Tabwidth: tabwidth, Indent: indent}).Fprint(&buf, fset, file)
// all code must be indented by at least 'indent' tabs
lines := bytes.Split(buf.Bytes(), []byte{'\n'})
for i, line := range lines {
if len(line) == 0 {
continue // empty lines don't have indentation
}
n := 0
for j, b := range line {
if b != '\t' {
// end of indentation
n = j
break
indent := indent
t.Run(fmt.Sprint(indent), func(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
(&Config{Tabwidth: tabwidth, Indent: indent}).Fprint(&buf, fset, file)
// all code must be indented by at least 'indent' tabs
lines := bytes.Split(buf.Bytes(), []byte{'\n'})
for i, line := range lines {
if len(line) == 0 {
continue // empty lines don't have indentation
}
n := 0
for j, b := range line {
if b != '\t' {
// end of indentation
n = j
break
}
}
if n < indent {
t.Errorf("line %d: got only %d tabs; want at least %d: %q", i, n, indent, line)
}
}
if n < indent {
t.Errorf("line %d: got only %d tabs; want at least %d: %q", i, n, indent, line)
}
}
})
}
}

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

// Test whether the printer stops writing after the first error
func TestWriteErrors(t *testing.T) {
t.Parallel()
const filename = "printer.go"
src, err := ioutil.ReadFile(filename)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions src/log/syslog/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func startServer(n, la string, done chan<- string) (addr string, sock io.Closer,
}

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

func TestWrite(t *testing.T) {
t.Parallel()
tests := []struct {
pri Priority
pre string
Expand Down
2 changes: 2 additions & 0 deletions src/mime/multipart/multipart_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ func TestMultipartSlowInput(t *testing.T) {
}

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

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

func TestParseAllSizes(t *testing.T) {
t.Parallel()
const maxSize = 5 << 10
var buf bytes.Buffer
body := strings.Repeat("a", maxSize)
Expand Down
Loading

0 comments on commit 2341631

Please sign in to comment.