Skip to content

Commit 26b50e3

Browse files
frncmxzelig
authored andcommitted
cmd/swarm: fix resource leaks in tests (#19443)
* swarm/api: fix file descriptor leak in NewTestSwarmServer Swarm storage (localstore) was not closed. That resulted a "too many open files" error if `TestClientUploadDownloadRawEncrypted` was run with `-count 1000`. * cmd/swarm: speed up StartNewNodes() by parallelization Reduce cluster startup time from 13s to 7s. * swarm/api: disable flaky TestClientUploadDownloadRawEncrypted with -race * swarm/storage: disable flaky TestLDBStoreCollectGarbage (-race) With race detection turned on the disabled cases often fail with: "ldbstore_test.go:535: expected surplus chunk 150 to be missing, but got no error" * cmd/swarm: fix process leak in TestACT and TestSwarmUp Each test run we start 3 nodes, but we did not terminate them. So those 3 nodes continued eating up 1.2GB (3.4GB with -race) after test completion. 6b6c4d1 changed how we start clusters to speed up tests. The changeset merged together test cases and introduced a global cluster. But "forgot" about termination. Let's get rid of "global cluster" so we have a clear owner of termination (some time sacrifice), while leaving subtests to use the same cluster.
1 parent 54dfce8 commit 26b50e3

6 files changed

Lines changed: 97 additions & 66 deletions

File tree

‎cmd/swarm/access_test.go‎

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,12 @@ func TestACT(t *testing.T) {
5252
t.Skip()
5353
}
5454

55-
initCluster(t)
55+
cluster := newTestCluster(t, clusterSize)
56+
defer cluster.Shutdown()
5657

5758
cases := []struct {
5859
name string
59-
f func(t *testing.T)
60+
f func(t *testing.T, cluster *testCluster)
6061
}{
6162
{"Password", testPassword},
6263
{"PK", testPK},
@@ -65,7 +66,9 @@ func TestACT(t *testing.T) {
6566
}
6667

6768
for _, tc := range cases {
68-
t.Run(tc.name, tc.f)
69+
t.Run(tc.name, func(t *testing.T) {
70+
tc.f(t, cluster)
71+
})
6972
}
7073
}
7174

@@ -74,7 +77,7 @@ func TestACT(t *testing.T) {
7477
// The parties participating - node (publisher), uploads to second node then disappears. Content which was uploaded
7578
// is then fetched through 2nd node. since the tested code is not key-aware - we can just
7679
// fetch from the 2nd node using HTTP BasicAuth
77-
func testPassword(t *testing.T) {
80+
func testPassword(t *testing.T, cluster *testCluster) {
7881
dataFilename := testutil.TempFileWithContent(t, data)
7982
defer os.RemoveAll(dataFilename)
8083

@@ -226,7 +229,7 @@ func testPassword(t *testing.T) {
226229
// The parties participating - node (publisher), uploads to second node (which is also the grantee) then disappears.
227230
// Content which was uploaded is then fetched through the grantee's http proxy. Since the tested code is private-key aware,
228231
// the test will fail if the proxy's given private key is not granted on the ACT.
229-
func testPK(t *testing.T) {
232+
func testPK(t *testing.T, cluster *testCluster) {
230233
dataFilename := testutil.TempFileWithContent(t, data)
231234
defer os.RemoveAll(dataFilename)
232235

@@ -359,21 +362,21 @@ func testPK(t *testing.T) {
359362
}
360363

361364
// testACTWithoutBogus tests the creation of the ACT manifest end-to-end, without any bogus entries (i.e. default scenario = 3 nodes 1 unauthorized)
362-
func testACTWithoutBogus(t *testing.T) {
363-
testACT(t, 0)
365+
func testACTWithoutBogus(t *testing.T, cluster *testCluster) {
366+
testACT(t, cluster, 0)
364367
}
365368

366369
// testACTWithBogus tests the creation of the ACT manifest end-to-end, with 100 bogus entries (i.e. 100 EC keys + default scenario = 3 nodes 1 unauthorized = 103 keys in the ACT manifest)
367-
func testACTWithBogus(t *testing.T) {
368-
testACT(t, 100)
370+
func testACTWithBogus(t *testing.T, cluster *testCluster) {
371+
testACT(t, cluster, 100)
369372
}
370373

371374
// testACT tests the e2e creation, uploading and downloading of an ACT access control with both EC keys AND password protection
372375
// the test fires up a 3 node cluster, then randomly picks 2 nodes which will be acting as grantees to the data
373376
// set and also protects the ACT with a password. the third node should fail decoding the reference as it will not be granted access.
374377
// the third node then then tries to download using a correct password (and succeeds) then uses a wrong password and fails.
375378
// the publisher uploads through one of the nodes then disappears.
376-
func testACT(t *testing.T, bogusEntries int) {
379+
func testACT(t *testing.T, cluster *testCluster, bogusEntries int) {
377380
var uploadThroughNode = cluster.Nodes[0]
378381
client := swarmapi.NewClient(uploadThroughNode.URL)
379382

‎cmd/swarm/run_test.go‎

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,6 @@ func init() {
5959

6060
const clusterSize = 3
6161

62-
var clusteronce sync.Once
63-
var cluster *testCluster
64-
65-
func initCluster(t *testing.T) {
66-
clusteronce.Do(func() {
67-
cluster = newTestCluster(t, clusterSize)
68-
})
69-
}
70-
7162
func serverFunc(api *api.API) swarmhttp.TestServer {
7263
return swarmhttp.NewServer(api, "")
7364
}
@@ -165,10 +156,8 @@ outer:
165156
}
166157

167158
func (c *testCluster) Shutdown() {
168-
for _, node := range c.Nodes {
169-
node.Shutdown()
170-
}
171-
os.RemoveAll(c.TmpDir)
159+
c.Stop()
160+
c.Cleanup()
172161
}
173162

174163
func (c *testCluster) Stop() {
@@ -179,16 +168,35 @@ func (c *testCluster) Stop() {
179168

180169
func (c *testCluster) StartNewNodes(t *testing.T, size int) {
181170
c.Nodes = make([]*testNode, 0, size)
171+
172+
errors := make(chan error, size)
173+
nodes := make(chan *testNode, size)
182174
for i := 0; i < size; i++ {
183-
dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", i))
184-
if err := os.Mkdir(dir, 0700); err != nil {
185-
t.Fatal(err)
186-
}
175+
go func(nodeIndex int) {
176+
dir := filepath.Join(c.TmpDir, fmt.Sprintf("swarm%02d", nodeIndex))
177+
if err := os.Mkdir(dir, 0700); err != nil {
178+
errors <- err
179+
return
180+
}
187181

188-
node := newTestNode(t, dir)
189-
node.Name = fmt.Sprintf("swarm%02d", i)
182+
node := newTestNode(t, dir)
183+
node.Name = fmt.Sprintf("swarm%02d", nodeIndex)
184+
nodes <- node
185+
}(i)
186+
}
190187

191-
c.Nodes = append(c.Nodes, node)
188+
for i := 0; i < size; i++ {
189+
select {
190+
case node := <-nodes:
191+
c.Nodes = append(c.Nodes, node)
192+
case err := <-errors:
193+
t.Error(err)
194+
}
195+
}
196+
197+
if t.Failed() {
198+
c.Shutdown()
199+
t.FailNow()
192200
}
193201
}
194202

‎cmd/swarm/upload_test.go‎

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,12 @@ func TestSwarmUp(t *testing.T) {
4646
t.Skip()
4747
}
4848

49-
initCluster(t)
49+
cluster := newTestCluster(t, clusterSize)
50+
defer cluster.Shutdown()
5051

5152
cases := []struct {
5253
name string
53-
f func(t *testing.T)
54+
f func(t *testing.T, cluster *testCluster)
5455
}{
5556
{"NoEncryption", testNoEncryption},
5657
{"Encrypted", testEncrypted},
@@ -60,31 +61,33 @@ func TestSwarmUp(t *testing.T) {
6061
}
6162

6263
for _, tc := range cases {
63-
t.Run(tc.name, tc.f)
64+
t.Run(tc.name, func(t *testing.T) {
65+
tc.f(t, cluster)
66+
})
6467
}
6568
}
6669

6770
// testNoEncryption tests that running 'swarm up' makes the resulting file
6871
// available from all nodes via the HTTP API
69-
func testNoEncryption(t *testing.T) {
70-
testDefault(false, t)
72+
func testNoEncryption(t *testing.T, cluster *testCluster) {
73+
testDefault(t, cluster, false)
7174
}
7275

7376
// testEncrypted tests that running 'swarm up --encrypted' makes the resulting file
7477
// available from all nodes via the HTTP API
75-
func testEncrypted(t *testing.T) {
76-
testDefault(true, t)
78+
func testEncrypted(t *testing.T, cluster *testCluster) {
79+
testDefault(t, cluster, true)
7780
}
7881

79-
func testRecursiveNoEncryption(t *testing.T) {
80-
testRecursive(false, t)
82+
func testRecursiveNoEncryption(t *testing.T, cluster *testCluster) {
83+
testRecursive(t, cluster, false)
8184
}
8285

83-
func testRecursiveEncrypted(t *testing.T) {
84-
testRecursive(true, t)
86+
func testRecursiveEncrypted(t *testing.T, cluster *testCluster) {
87+
testRecursive(t, cluster, true)
8588
}
8689

87-
func testDefault(toEncrypt bool, t *testing.T) {
90+
func testDefault(t *testing.T, cluster *testCluster, toEncrypt bool) {
8891
tmpFileName := testutil.TempFileWithContent(t, data)
8992
defer os.Remove(tmpFileName)
9093

@@ -189,7 +192,7 @@ func testDefault(toEncrypt bool, t *testing.T) {
189192
}
190193
}
191194

192-
func testRecursive(toEncrypt bool, t *testing.T) {
195+
func testRecursive(t *testing.T, cluster *testCluster, toEncrypt bool) {
193196
tmpUploadDir, err := ioutil.TempDir("", "swarm-test")
194197
if err != nil {
195198
t.Fatal(err)
@@ -279,14 +282,14 @@ func testRecursive(toEncrypt bool, t *testing.T) {
279282

280283
// testDefaultPathAll tests swarm recursive upload with relative and absolute
281284
// default paths and with encryption.
282-
func testDefaultPathAll(t *testing.T) {
283-
testDefaultPath(false, false, t)
284-
testDefaultPath(false, true, t)
285-
testDefaultPath(true, false, t)
286-
testDefaultPath(true, true, t)
285+
func testDefaultPathAll(t *testing.T, cluster *testCluster) {
286+
testDefaultPath(t, cluster, false, false)
287+
testDefaultPath(t, cluster, false, true)
288+
testDefaultPath(t, cluster, true, false)
289+
testDefaultPath(t, cluster, true, true)
287290
}
288291

289-
func testDefaultPath(toEncrypt bool, absDefaultPath bool, t *testing.T) {
292+
func testDefaultPath(t *testing.T, cluster *testCluster, toEncrypt bool, absDefaultPath bool) {
290293
tmp, err := ioutil.TempDir("", "swarm-defaultpath-test")
291294
if err != nil {
292295
t.Fatal(err)

‎swarm/api/client/client_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ import (
2525
"sort"
2626
"testing"
2727

28+
"github.com/ethereum/go-ethereum/swarm/testutil"
29+
2830
"github.com/ethereum/go-ethereum/swarm/storage"
2931
"github.com/ethereum/go-ethereum/swarm/storage/feed/lookup"
3032

@@ -43,7 +45,13 @@ func serverFunc(api *api.API) swarmhttp.TestServer {
4345
func TestClientUploadDownloadRaw(t *testing.T) {
4446
testClientUploadDownloadRaw(false, t)
4547
}
48+
4649
func TestClientUploadDownloadRawEncrypted(t *testing.T) {
50+
if testutil.RaceEnabled {
51+
t.Skip("flaky with -race on Travis")
52+
// See: https://github.com/ethersphere/go-ethereum/issues/1254
53+
}
54+
4755
testClientUploadDownloadRaw(true, t)
4856
}
4957

‎swarm/api/http/test_server.go‎

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,44 +33,45 @@ type TestServer interface {
3333
}
3434

3535
func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer, resolver api.Resolver) *TestSwarmServer {
36-
dir, err := ioutil.TempDir("", "swarm-storage-test")
36+
swarmDir, err := ioutil.TempDir("", "swarm-storage-test")
3737
if err != nil {
3838
t.Fatal(err)
3939
}
40-
storeparams := storage.NewDefaultLocalStoreParams()
41-
storeparams.DbCapacity = 5000000
42-
storeparams.CacheCapacity = 5000
43-
storeparams.Init(dir)
44-
localStore, err := storage.NewLocalStore(storeparams, nil)
40+
41+
storeParams := storage.NewDefaultLocalStoreParams()
42+
storeParams.DbCapacity = 5000000
43+
storeParams.CacheCapacity = 5000
44+
storeParams.Init(swarmDir)
45+
localStore, err := storage.NewLocalStore(storeParams, nil)
4546
if err != nil {
46-
os.RemoveAll(dir)
47+
os.RemoveAll(swarmDir)
4748
t.Fatal(err)
4849
}
4950
fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams())
50-
5151
// Swarm feeds test setup
5252
feedsDir, err := ioutil.TempDir("", "swarm-feeds-test")
5353
if err != nil {
5454
t.Fatal(err)
5555
}
5656

57-
rhparams := &feed.HandlerParams{}
58-
rh, err := feed.NewTestHandler(feedsDir, rhparams)
57+
feeds, err := feed.NewTestHandler(feedsDir, &feed.HandlerParams{})
5958
if err != nil {
6059
t.Fatal(err)
6160
}
6261

63-
a := api.NewAPI(fileStore, resolver, rh.Handler, nil)
64-
srv := httptest.NewServer(serverFunc(a))
62+
swarmApi := api.NewAPI(fileStore, resolver, feeds.Handler, nil)
63+
apiServer := httptest.NewServer(serverFunc(swarmApi))
64+
6565
tss := &TestSwarmServer{
66-
Server: srv,
66+
Server: apiServer,
6767
FileStore: fileStore,
68-
dir: dir,
68+
dir: swarmDir,
6969
Hasher: storage.MakeHashFunc(storage.DefaultHash)(),
7070
cleanup: func() {
71-
srv.Close()
72-
rh.Close()
73-
os.RemoveAll(dir)
71+
apiServer.Close()
72+
fileStore.Close()
73+
feeds.Close()
74+
os.RemoveAll(swarmDir)
7475
os.RemoveAll(feedsDir)
7576
},
7677
CurrentTime: 42,

‎swarm/storage/ldbstore_test.go‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ import (
2727
"strings"
2828
"testing"
2929

30+
"github.com/ethereum/go-ethereum/swarm/testutil"
31+
3032
"github.com/ethereum/go-ethereum/common"
3133
"github.com/ethereum/go-ethereum/swarm/chunk"
3234
"github.com/ethereum/go-ethereum/swarm/log"
@@ -322,6 +324,12 @@ func TestLDBStoreCollectGarbage(t *testing.T) {
322324
initialCap := defaultMaxGCRound / 100
323325
cap := initialCap / 2
324326
t.Run(fmt.Sprintf("A/%d/%d", cap, cap*4), testLDBStoreCollectGarbage)
327+
328+
if testutil.RaceEnabled {
329+
t.Skip("only the simplest case run as others are flaky with race")
330+
// Note: some tests fail consistently and even locally with `-race`
331+
}
332+
325333
t.Run(fmt.Sprintf("B/%d/%d", cap, cap*4), testLDBStoreRemoveThenCollectGarbage)
326334

327335
// at max round

0 commit comments

Comments
 (0)