Skip to content

Commit

Permalink
Fixed and cleaned up TestIpfsStressRead
Browse files Browse the repository at this point in the history
License: MIT
Signed-off-by: Max Chechel <hexdigest@gmail.com>
  • Loading branch information
Max Chechel committed Jan 10, 2019
1 parent 1e9a638 commit c0ab433
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions fuse/readonly/ipfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ package readonly

import (
"bytes"
"errors"
"context"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"sync"
"strconv"
"strings"
"testing"

core "github.com/ipfs/go-ipfs/core"
Expand Down Expand Up @@ -162,49 +163,45 @@ func TestIpfsStressRead(t *testing.T) {
paths = append(paths, npaths...)
}

// Now read a bunch, concurrently
wg := sync.WaitGroup{}
errs := make(chan error)
t.Parallel()

for s := 0; s < 4; s++ {
wg.Add(1)
go func() {
defer wg.Done()

t.Run(strconv.Itoa(s), func(t *testing.T) {
for i := 0; i < 2000; i++ {
item, _ := iface.ParsePath(paths[rand.Intn(len(paths))])
fname := path.Join(mnt.Dir, item.String())
item, err := iface.ParsePath(paths[rand.Intn(len(paths))])
if err != nil {
t.Fatal(err)
}

relpath := strings.Replace(item.String(), "/ipfs/", "/", 1)
fname := path.Join(mnt.Dir, relpath)

rbuf, err := ioutil.ReadFile(fname)
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 {
errs <- err
t.Fatal(err)
}

data, err := ioutil.ReadAll(read.(files.File))
if err != nil {
errs <- err
t.Fatal(err)
}

cancelFunc()

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)
}
})
}
}

Expand Down

0 comments on commit c0ab433

Please sign in to comment.