@cryptotaxi247 / kubo / commits / c0ab43318

Fixed and cleaned up TestIpfsStressRead

License: MIT Signed-off-by: Max Chechel <hexdigest@gmail.com>

Max Chechel committed Jan 11, 2019 at 00:39 UTC c0ab433186e60a54deeae59bd2e295dde524f559
1 file changed +25 -28
fuse/readonly/ipfs_test.go
+25 -28
@@ -4,13 +4,14 @@ package readonly
4
5 import (
6 "bytes"
7 - "errors"
7 + "context"
8 "fmt"
9 "io/ioutil"
10 "math/rand"
11 "os"
12 "path"
13 - "sync"
13 + "strconv"
14 + "strings"
15 "testing"
16
17 core "github.com/ipfs/go-ipfs/core"
@@ -162,49 +163,45 @@ func TestIpfsStressRead(t *testing.T) {
163 paths = append(paths, npaths...)
164 }
165
165 - // Now read a bunch, concurrently
166 - wg := sync.WaitGroup{}
167 - errs := make(chan error)
166 + t.Parallel()
167
168 for s := 0; s < 4; s++ {
170 - wg.Add(1)
171 - go func() {
172 - defer wg.Done()
173 -
169 + t.Run(strconv.Itoa(s), func(t *testing.T) {
170 for i := 0; i < 2000; i++ {
175 - item, _ := iface.ParsePath(paths[rand.Intn(len(paths))])
176 - fname := path.Join(mnt.Dir, item.String())
171 + item, err := iface.ParsePath(paths[rand.Intn(len(paths))])
172 + if err != nil {
173 + t.Fatal(err)
174 + }
175 +
176 + relpath := strings.Replace(item.String(), "/ipfs/", "/", 1)
177 + fname := path.Join(mnt.Dir, relpath)
178 +
179 rbuf, err := ioutil.ReadFile(fname)
180 if err != nil {
179 - errs <- err
181 + t.Fatal(err)
182 }
183
182 - read, err := api.Unixfs().Get(nd.Context(), item)
184 + //nd.Context() is never closed which leads to
185 + //hitting 8128 goroutine limit in go test -race mode
186 + ctx, cancelFunc := context.WithCancel(context.Background())
187 +
188 + read, err := api.Unixfs().Get(ctx, item)
189 if err != nil {
184 - errs <- err
190 + t.Fatal(err)
191 }
192
193 data, err := ioutil.ReadAll(read.(files.File))
194 if err != nil {
189 - errs <- err
195 + t.Fatal(err)
196 }
197
198 + cancelFunc()
199 +
200 if !bytes.Equal(rbuf, data) {
193 - errs <- errors.New("incorrect read")
201 + t.Fatal("incorrect read")
202 }
203 }
196 - }()
197 - }
198 -
199 - go func() {
200 - wg.Wait()
201 - close(errs)
202 - }()
203 -
204 - for err := range errs {
205 - if err != nil {
206 - t.Fatal(err)
207 - }
204 + })
205 }
206 }
207