Skip to content

Commit

Permalink
Merge unit test changes from issue ipfs#32
Browse files Browse the repository at this point in the history
Pull in commit from go-ipfs commit
ipfs/kubo@f4dc9a4
Dropped nloop to 500 (from 1000) in `TestConcurrentWriteAndFlush` to
reduce testing time.

All unittests pass.
  • Loading branch information
nmalhotra committed Dec 27, 2018
1 parent ab71ad6 commit e76c2b5
Showing 1 changed file with 53 additions and 8 deletions.
61 changes: 53 additions & 8 deletions mfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package mfs
import (
"bytes"
"context"
"encoding/binary"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -765,14 +766,14 @@ func TestConcurrentWriteAndFlush(t *testing.T) {
t.Fatal(err)
}

nloops := 5000
nloops := 500

wg := new(sync.WaitGroup)
wg.Add(1)
go func() {
defer wg.Done()
for i := 0; i < nloops; i++ {
err := writeFile(rt, "/foo/bar/baz/file", []byte("STUFF"))
err := writeFile(rt, "/foo/bar/baz/file", func(_ []byte) []byte { return []byte("STUFF") })
if err != nil {
t.Error("file write failed: ", err)
return
Expand Down Expand Up @@ -936,7 +937,7 @@ func TestConcurrentReads(t *testing.T) {
wg.Wait()
}

func writeFile(rt *Root, path string, data []byte) error {
func writeFile(rt *Root, path string, transform func([]byte) []byte) error {
n, err := Lookup(rt, path)
if err != nil {
return err
Expand All @@ -947,12 +948,27 @@ func writeFile(rt *Root, path string, data []byte) error {
return fmt.Errorf("expected to receive a file, but didnt get one")
}

fd, err := fi.Open(Flags{Write: true, Sync: true})
fd, err := fi.Open(Flags{Read: true, Write: true, Sync: true})
if err != nil {
return err
}
defer fd.Close()

data, err := ioutil.ReadAll(fd)
if err != nil {
return err
}
data = transform(data)

_, err = fd.Seek(0, io.SeekStart)
if err != nil {
return err
}
err = fd.Truncate(0)
if err != nil {
return err
}

nw, err := fd.Write(data)
if err != nil {
return err
Expand Down Expand Up @@ -986,19 +1002,48 @@ func TestConcurrentWrites(t *testing.T) {
nloops := 100
for i := 0; i < 10; i++ {
wg.Add(1)
go func(me int) {
go func() {
defer wg.Done()
mybuf := bytes.Repeat([]byte{byte(me)}, 10)
var lastSeen uint64
for j := 0; j < nloops; j++ {
err := writeFile(rt, "a/b/c/afile", mybuf)
err := writeFile(rt, "a/b/c/afile", func(buf []byte) []byte {
if len(buf) == 0 {
if lastSeen > 0 {
t.Fatalf("file corrupted, last seen: %d", lastSeen)
}
buf = make([]byte, 8)
} else if len(buf) != 8 {
t.Fatal("buf not the right size")
}

num := binary.LittleEndian.Uint64(buf)
if num < lastSeen {
t.Fatalf("count decreased: was %d, is %d", lastSeen, num)
} else {
t.Logf("count correct: was %d, is %d", lastSeen, num)
}
num++
binary.LittleEndian.PutUint64(buf, num)
lastSeen = num
return buf
})
if err != nil {
t.Error("writefile failed: ", err)
return
}
}
}(i)
}()
}
wg.Wait()
buf := make([]byte, 8)
if err := readFile(rt, "a/b/c/afile", 0, buf); err != nil {
t.Fatal(err)
}
actual := binary.LittleEndian.Uint64(buf)
expected := uint64(10 * nloops)
if actual != expected {
t.Fatalf("iteration mismatch: expect %d, got %d", expected, actual)
}
}

func TestFileDescriptors(t *testing.T) {
Expand Down

0 comments on commit e76c2b5

Please sign in to comment.