Prechádzať zdrojové kódy

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>
Kir Kolyshkin 6 rokov pred
rodič
commit
33c205be4f
1 zmenil súbory, kde vykonal 9 pridanie a 9 odobranie
  1. 9 9
      distribution/xfer/transfer_test.go

+ 9 - 9
distribution/xfer/transfer_test.go

@@ -10,11 +10,11 @@ import (
 
 
 func TestTransfer(t *testing.T) {
 func TestTransfer(t *testing.T) {
 	makeXferFunc := func(id string) DoFunc {
 	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 {
 			select {
 			case <-start:
 			case <-start:
 			default:
 			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()
 			xfer := NewTransfer()
@@ -38,7 +38,7 @@ func TestTransfer(t *testing.T) {
 		for p := range progressChan {
 		for p := range progressChan {
 			val, present := receivedProgress[p.ID]
 			val, present := receivedProgress[p.ID]
 			if present && p.Current <= val {
 			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
 			receivedProgress[p.ID] = p.Current
 		}
 		}
@@ -72,13 +72,13 @@ func TestConcurrencyLimit(t *testing.T) {
 	var runningJobs int32
 	var runningJobs int32
 
 
 	makeXferFunc := func(id string) DoFunc {
 	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()
 			xfer := NewTransfer()
 			go func() {
 			go func() {
 				<-start
 				<-start
 				totalJobs := atomic.AddInt32(&runningJobs, 1)
 				totalJobs := atomic.AddInt32(&runningJobs, 1)
 				if int(totalJobs) > concurrencyLimit {
 				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++ {
 				for i := 0; i <= 10; i++ {
 					progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
 					progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
@@ -137,7 +137,7 @@ func TestInactiveJobs(t *testing.T) {
 				<-start
 				<-start
 				totalJobs := atomic.AddInt32(&runningJobs, 1)
 				totalJobs := atomic.AddInt32(&runningJobs, 1)
 				if int(totalJobs) > concurrencyLimit {
 				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++ {
 				for i := 0; i <= 10; i++ {
 					progressChan <- progress.Progress{ID: id, Action: "testing", Current: int64(i), Total: 10}
 					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{})
 	ready := make(chan struct{})
 
 
 	makeXferFunc := func(id string) DoFunc {
 	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()
 			xfer := NewTransfer()
 			go func() {
 			go func() {
 				defer func() {
 				defer func() {
@@ -280,7 +280,7 @@ func TestWatchRelease(t *testing.T) {
 
 
 func TestWatchFinishedTransfer(t *testing.T) {
 func TestWatchFinishedTransfer(t *testing.T) {
 	makeXferFunc := func(id string) DoFunc {
 	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()
 			xfer := NewTransfer()
 			go func() {
 			go func() {
 				// Finish immediately
 				// Finish immediately
@@ -322,7 +322,7 @@ func TestDuplicateTransfer(t *testing.T) {
 	var xferFuncCalls int32
 	var xferFuncCalls int32
 
 
 	makeXferFunc := func(id string) DoFunc {
 	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)
 			atomic.AddInt32(&xferFuncCalls, 1)
 			xfer := NewTransfer()
 			xfer := NewTransfer()
 			go func() {
 			go func() {