提交 c0ab4331 作者: Max Chechel

Fixed and cleaned up TestIpfsStressRead

License: MIT
Signed-off-by: 's avatarMax Chechel <hexdigest@gmail.com>
上级 1e9a638a
...@@ -4,13 +4,14 @@ package readonly ...@@ -4,13 +4,14 @@ package readonly
import ( import (
"bytes" "bytes"
"errors" "context"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"math/rand" "math/rand"
"os" "os"
"path" "path"
"sync" "strconv"
"strings"
"testing" "testing"
core "github.com/ipfs/go-ipfs/core" core "github.com/ipfs/go-ipfs/core"
...@@ -162,49 +163,45 @@ func TestIpfsStressRead(t *testing.T) { ...@@ -162,49 +163,45 @@ func TestIpfsStressRead(t *testing.T) {
paths = append(paths, npaths...) paths = append(paths, npaths...)
} }
// Now read a bunch, concurrently t.Parallel()
wg := sync.WaitGroup{}
errs := make(chan error)
for s := 0; s < 4; s++ { for s := 0; s < 4; s++ {
wg.Add(1) t.Run(strconv.Itoa(s), func(t *testing.T) {
go func() {
defer wg.Done()
for i := 0; i < 2000; i++ { for i := 0; i < 2000; i++ {
item, _ := iface.ParsePath(paths[rand.Intn(len(paths))]) item, err := iface.ParsePath(paths[rand.Intn(len(paths))])
fname := path.Join(mnt.Dir, item.String()) if err != nil {
t.Fatal(err)
}
relpath := strings.Replace(item.String(), "/ipfs/", "/", 1)
fname := path.Join(mnt.Dir, relpath)
rbuf, err := ioutil.ReadFile(fname) rbuf, err := ioutil.ReadFile(fname)
if err != nil { if err != nil {
errs <- err t.Fatal(err)
} }
read, err := api.Unixfs().Get(nd.Context(), item) //nd.Context() is never closed which leads to
//hitting 8128 goroutine limit in go test -race mode
ctx, cancelFunc := context.WithCancel(context.Background())
read, err := api.Unixfs().Get(ctx, item)
if err != nil { if err != nil {
errs <- err t.Fatal(err)
} }
data, err := ioutil.ReadAll(read.(files.File)) data, err := ioutil.ReadAll(read.(files.File))
if err != nil { if err != nil {
errs <- err t.Fatal(err)
} }
cancelFunc()
if !bytes.Equal(rbuf, data) { if !bytes.Equal(rbuf, data) {
errs <- errors.New("incorrect read") t.Fatal("incorrect read")
} }
} }
}() })
}
go func() {
wg.Wait()
close(errs)
}()
for err := range errs {
if err != nil {
t.Fatal(err)
}
} }
} }
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论