From 96d6a20ee47838f09b7dfe02c960a44e10df0db0 Mon Sep 17 00:00:00 2001
From: Boqin Qin <Bobbqqin@gmail.com>
Date: Tue, 18 Feb 2020 00:33:12 +0800
Subject: [PATCH] all: fix goroutine leaks in unit tests by adding 1-elem
 channel buffer (#20666)

This fixes a bunch of cases where a timeout in the test would leak
a goroutine.
---
 common/mclock/simclock_test.go | 2 +-
 eth/handler_test.go            | 2 +-
 p2p/server_test.go             | 2 +-
 rpc/client_test.go             | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/common/mclock/simclock_test.go b/common/mclock/simclock_test.go
index 94aa4f2b39..48f3fd56a0 100644
--- a/common/mclock/simclock_test.go
+++ b/common/mclock/simclock_test.go
@@ -96,7 +96,7 @@ func TestSimulatedSleep(t *testing.T) {
 	var (
 		c       Simulated
 		timeout = 1 * time.Hour
-		done    = make(chan AbsTime)
+		done    = make(chan AbsTime, 1)
 	)
 	go func() {
 		c.Sleep(timeout)
diff --git a/eth/handler_test.go b/eth/handler_test.go
index 9ae8a19050..42a8700a4d 100644
--- a/eth/handler_test.go
+++ b/eth/handler_test.go
@@ -1320,7 +1320,7 @@ func TestBroadcastMalformedBlock(t *testing.T) {
 	malformedEverything.TxHash[0]++
 
 	// Keep listening to broadcasts and notify if any arrives
-	notify := make(chan struct{})
+	notify := make(chan struct{}, 1)
 	go func() {
 		if _, err := sink.app.ReadMsg(); err == nil {
 			notify <- struct{}{}
diff --git a/p2p/server_test.go b/p2p/server_test.go
index 34371beb14..ccd6a89ee3 100644
--- a/p2p/server_test.go
+++ b/p2p/server_test.go
@@ -550,7 +550,7 @@ func TestServerInboundThrottle(t *testing.T) {
 	conn.Close()
 
 	// Dial again. This time the server should close the connection immediately.
-	connClosed := make(chan struct{})
+	connClosed := make(chan struct{}, 1)
 	conn, err = net.DialTimeout("tcp", srv.ListenAddr, timeout)
 	if err != nil {
 		t.Fatalf("could not dial: %v", err)
diff --git a/rpc/client_test.go b/rpc/client_test.go
index 97f9a95b23..a89f257043 100644
--- a/rpc/client_test.go
+++ b/rpc/client_test.go
@@ -297,7 +297,7 @@ func TestClientSubscribeClose(t *testing.T) {
 
 	var (
 		nc   = make(chan int)
-		errc = make(chan error)
+		errc = make(chan error, 1)
 		sub  *ClientSubscription
 		err  error
 	)
-- 
GitLab