1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

TestTransfer*: don't call t.Fatal from a goroutine

staticcheck go linter warns:

> distribution/xfer/transfer_test.go:37:2: SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test (staticcheck)

What it doesn't say is why. The reason is, t.Fatalf() calls t.FailNow(),
which is expected to stop test execution right now. It does so by
calling runtime.Goexit(), which, unless called from a main goroutine,
does not stop test execution.

Anyway, long story short, if we don't care much about stopping the test
case immediately, we can just replace t.Fatalf() with t.Errorf() which
still marks the test case as failed, but won't stop it immediately.

This patch was tested to check that the test fails if any of the
goroutines call t.Errorf():

1. Failure in DoFunc ("transfer function not started ...") was tested by
decreading the NewTransferManager() argument:

-        tm := NewTransferManager(5)
+        tm := NewTransferManager(2)

2. Failure "got unexpected progress value" was tested by injecting a random:

-                       if present && p.Current <= val {
+                       if present && p.Current <= val || rand.Intn(100) > 80 {

3. Failure in DoFunc ("too many jobs running") was tested by increasing
the NewTransferManager() argument:

-       tm := NewTransferManager(concurrencyLimit)
+       tm := NewTransferManager(concurrencyLimit + 1)

While at it:
 * fix/amend some error messages
 * use _ for unused arguments of DoFunc

Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
Kir Kolyshkin 2019-08-06 15:37:07 -07:00 committed by Sebastiaan van Stijn
parent 739b7b44aa
commit 33c205be4f
No known key found for this signature in database
GPG key ID: 76698F39D527CE8C

View file

@ -10,11 +10,11 @@ import (
func TestTransfer(t *testing.T) {
makeXferFunc := func(id string) DoFunc {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, _ chan<- struct{}) Transfer {
select {
case <-start:
default:
t.Fatalf("transfer function not started even though concurrency limit not reached")
t.Errorf("%s: transfer function not started even though concurrency limit not reached", id)
}
xfer := NewTransfer()
@ -38,7 +38,7 @@ func TestTransfer(t *testing.T) {
for p := range progressChan {
val, present := receivedProgress[p.ID]
if present && p.Current <= val {
t.Fatalf("got unexpected progress value: %d (expected %d)", p.Current, val+1)
t.Errorf("%s: got unexpected progress value: %d (expected <= %d)", p.ID, p.Current, val)
}
receivedProgress[p.ID] = p.Current
}
@ -72,13 +72,13 @@ func TestConcurrencyLimit(t *testing.T) {
var runningJobs int32
makeXferFunc := func(id string) DoFunc {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, _ chan<- struct{}) Transfer {
xfer := NewTransfer()
go func() {
<-start
totalJobs := atomic.AddInt32(&runningJobs, 1)
if int(totalJobs) > concurrencyLimit {
t.Fatalf("too many jobs running")
t.Errorf("%s: too many jobs running (%d > %d)", id, totalJobs, concurrencyLimit)
}
for i := 0; i <= 10; i++ {
progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
@ -137,7 +137,7 @@ func TestInactiveJobs(t *testing.T) {
<-start
totalJobs := atomic.AddInt32(&runningJobs, 1)
if int(totalJobs) > concurrencyLimit {
t.Fatalf("too many jobs running")
t.Errorf("%s: too many jobs running (%d > %d)", id, totalJobs, concurrencyLimit)
}
for i := 0; i <= 10; i++ {
progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
@ -191,7 +191,7 @@ func TestWatchRelease(t *testing.T) {
ready := make(chan struct{})
makeXferFunc := func(id string) DoFunc {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, _ chan<- struct{}) Transfer {
xfer := NewTransfer()
go func() {
defer func() {
@ -280,7 +280,7 @@ func TestWatchRelease(t *testing.T) {
func TestWatchFinishedTransfer(t *testing.T) {
makeXferFunc := func(id string) DoFunc {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
return func(progressChan chan<- progress.Progress, _ <-chan struct{}, _ chan<- struct{}) Transfer {
xfer := NewTransfer()
go func() {
// Finish immediately
@ -322,7 +322,7 @@ func TestDuplicateTransfer(t *testing.T) {
var xferFuncCalls int32
makeXferFunc := func(id string) DoFunc {
return func(progressChan chan<- progress.Progress, start <-chan struct{}, inactive chan<- struct{}) Transfer {
return func(progressChan chan<- progress.Progress, _ <-chan struct{}, _ chan<- struct{}) Transfer {
atomic.AddInt32(&xferFuncCalls, 1)
xfer := NewTransfer()
go func() {