Skip to content

Commit

Permalink
Parallel tests where possible
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaMahany committed May 19, 2023
1 parent 8ad5aff commit 1b31b11
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 8 deletions.
16 changes: 8 additions & 8 deletions env/env_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"
)

func TestDuration(t *testing.T) {
func TestDuration(t *testing.T) { //nolint:paralleltest
var tests = []struct {
value time.Duration
}{
Expand All @@ -16,7 +16,7 @@ func TestDuration(t *testing.T) {
{value: 1 * time.Hour},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.value.String(), func(t *testing.T) {
key := strings.ToUpper(tt.value.String())
if err := os.Setenv(key, tt.value.String()); err != nil {
Expand All @@ -37,7 +37,7 @@ func TestDuration(t *testing.T) {
}
}

func TestString(t *testing.T) {
func TestString(t *testing.T) { //nolint:paralleltest
var tests = []struct {
value string
}{
Expand All @@ -46,7 +46,7 @@ func TestString(t *testing.T) {
{value: "baz"},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.value, func(t *testing.T) {
key := strings.ToUpper(tt.value)
if err := os.Setenv(key, tt.value); err != nil {
Expand All @@ -67,7 +67,7 @@ func TestString(t *testing.T) {
}
}

func TestBool(t *testing.T) {
func TestBool(t *testing.T) { //nolint:paralleltest
var tests = []struct {
env string
value bool
Expand All @@ -81,7 +81,7 @@ func TestBool(t *testing.T) {
{env: "0", value: false},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.env, func(t *testing.T) {
key := "TEST_BOOL"
if err := os.Setenv(key, tt.env); err != nil {
Expand All @@ -106,7 +106,7 @@ func TestBool(t *testing.T) {
}
}

func TestInt(t *testing.T) {
func TestInt(t *testing.T) { //nolint:paralleltest
var tests = []struct {
env string
value int
Expand All @@ -117,7 +117,7 @@ func TestInt(t *testing.T) {
{env: "0", value: 0},
}

for _, tt := range tests {
for _, tt := range tests { //nolint:paralleltest
t.Run(tt.env, func(t *testing.T) {
key := "TEST_INT"
if err := os.Setenv(key, tt.env); err != nil {
Expand Down
2 changes: 2 additions & 0 deletions fsutil/filesystem_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestSanitizeExtractPath(t *testing.T) {
t.Parallel()

var tests = []struct {
filepath string
destination string
Expand Down
7 changes: 7 additions & 0 deletions health/health_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import (
)

func TestCheckHealth(t *testing.T) {
t.Parallel()

checkers := map[string]Checker{
"fail": fail{},
"pass": Nop(),
Expand All @@ -34,6 +36,8 @@ func (c fail) HealthCheck() error {
}

func TestHealthzHandler(t *testing.T) {
t.Parallel()

logger := log.NewNopLogger()
failing := Handler(logger, map[string]Checker{
"mock": healthcheckFunc(func() error {
Expand All @@ -53,7 +57,10 @@ func TestHealthzHandler(t *testing.T) {
{500, failing},
}
for _, tt := range httpTests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()

rr := httptest.NewRecorder()
req := httptest.NewRequest("GET", "/healthz", nil)
tt.handler.ServeHTTP(rr, req)
Expand Down
14 changes: 14 additions & 0 deletions munemo/munemo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,23 +67,30 @@ func TestMunemoOriginal(t *testing.T) {

func testMunemo(t *testing.T, mg *munemoGenerator, tests []testCase) {
for _, tt := range tests {
tt := tt
if tt.e == "" {
// If we lack an error, this is a legit conversion. Try both ways
if !tt.skipInt {
t.Run(fmt.Sprintf("string/%d", tt.i), func(t *testing.T) {
t.Parallel()

ret := mg.String(tt.i)
require.Equal(t, tt.s, ret)
})
}

t.Run(fmt.Sprintf("int/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := mg.Int(tt.s)
assert.Equal(t, tt.i, ret)
assert.NoError(t, err)
})
} else {
// Having an error, means we're looking for an error
t.Run(fmt.Sprintf("interr/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := mg.Int(tt.s)
require.Equal(t, tt.i, ret)
require.EqualError(t, err, tt.e)
Expand All @@ -98,23 +105,30 @@ func TestLegacyInterfaces(t *testing.T) {
t.Parallel()

for _, tt := range originalTests {
tt := tt
if tt.e == "" {
// If we lack an error, this is a legit conversion. Try both ways
if !tt.skipInt {
t.Run(fmt.Sprintf("Munemo/%d", tt.i), func(t *testing.T) {
t.Parallel()

ret := Munemo(tt.i)
require.Equal(t, tt.s, ret)
})
}

t.Run(fmt.Sprintf("UnMunemo/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := UnMunemo(tt.s)
assert.Equal(t, tt.i, ret)
assert.NoError(t, err)
})
} else {
// Having an error, means we're looking for an error
t.Run(fmt.Sprintf("UnMunemo/%s", tt.s), func(t *testing.T) {
t.Parallel()

ret, err := UnMunemo(tt.s)
require.Equal(t, tt.i, ret)
require.EqualError(t, err, tt.e)
Expand Down
3 changes: 3 additions & 0 deletions pgutil/pgutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func TestConversion(t *testing.T) {
}

for _, tt := range tests {
tt := tt
t.Run("", func(t *testing.T) {
t.Parallel()

c, err := NewFromURL(tt.in, tt.opts...)
if tt.err {
require.Error(t, err)
Expand Down
16 changes: 16 additions & 0 deletions testutil/testutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
)

func TestErrorAfterSuccess(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -18,6 +20,8 @@ func TestErrorAfterSuccess(t *testing.T) {
}

func TestErrorAfterError(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -30,6 +34,8 @@ func TestErrorAfterError(t *testing.T) {
}

func TestErrorAfterFuncSuccess(t *testing.T) {
t.Parallel()

err := ErrorAfterFunc(100*time.Millisecond, func() {
time.Sleep(1 * time.Millisecond)
})
Expand All @@ -39,6 +45,8 @@ func TestErrorAfterFuncSuccess(t *testing.T) {
}

func TestErrorAfterFuncError(t *testing.T) {
t.Parallel()

err := ErrorAfterFunc(1*time.Millisecond, func() {
time.Sleep(100 * time.Millisecond)
})
Expand Down Expand Up @@ -69,6 +77,8 @@ func (m *mockFatal) Fatalf(string, ...interface{}) {
}

func TestFatalAfterFuncSuccess(t *testing.T) {
t.Parallel()

var m mockFatal

// Should not fatal
Expand All @@ -84,6 +94,8 @@ func TestFatalAfterFuncSuccess(t *testing.T) {
}

func TestFatalAfterFuncFatal(t *testing.T) {
t.Parallel()

var m mockFatal

// Should fatal
Expand Down Expand Up @@ -115,6 +127,8 @@ func ExampleFatalAfterFunc_fatal() {
}

func TestFatalAfterSuccess(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand All @@ -132,6 +146,8 @@ func TestFatalAfterSuccess(t *testing.T) {
}

func TestFatalAfterFatal(t *testing.T) {
t.Parallel()

var wg sync.WaitGroup
wg.Add(1)
go func() {
Expand Down
2 changes: 2 additions & 0 deletions tlsutil/tlsutil_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func TestNewConfig(t *testing.T) {
t.Parallel()

// default, should have Modern compatibility.
cfg := NewConfig()
if have, want := cfg.MinVersion, uint16(tls.VersionTLS12); have != want {
Expand Down
2 changes: 2 additions & 0 deletions version/version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

func TestVersion(t *testing.T) {
t.Parallel()

now := time.Now().String()
version = "test"
buildDate = now
Expand Down

0 comments on commit 1b31b11

Please sign in to comment.