func TestCallbacksWork(t *testing.T) { fake := new(FakeStream) var sent int64 var recv int64 sentCB := func(n int64, proto protocol.ID, p peer.ID) { sent += n } recvCB := func(n int64, proto protocol.ID, p peer.ID) { recv += n } ms := newMeteredStream(fake, protocol.ID("TEST"), peer.ID("PEER"), recvCB, sentCB) toWrite := int64(100000) toRead := int64(100000) fake.ReadBuf = io.LimitReader(randbo.New(), toRead) writeData := io.LimitReader(randbo.New(), toWrite) n, err := io.Copy(ms, writeData) if err != nil { t.Fatal(err) } if n != toWrite { t.Fatal("incorrect write amount") } if toWrite != sent { t.Fatal("incorrectly reported writes", toWrite, sent) } n, err = io.Copy(ioutil.Discard, ms) if err != nil { t.Fatal(err) } if n != toRead { t.Fatal("incorrect read amount") } if toRead != recv { t.Fatal("incorrectly reported reads") } }
func actorWriteFile(d *Directory) error { fi, err := randomFile(d) if err != nil { return err } if fi == nil { return nil } size := rand.Intn(1024) + 1 buf := make([]byte, size) randbo.New().Read(buf) s, err := fi.Size() if err != nil { return err } wfd, err := fi.Open(OpenWriteOnly, true) if err != nil { return err } offset := rand.Int63n(s) n, err := wfd.WriteAt(buf, offset) if err != nil { return err } if n != size { return fmt.Errorf("didnt write enough") } return wfd.Close() }
func actorMakeFile(d *Directory) error { d, err := randomWalk(d, rand.Intn(7)) if err != nil { return err } name := randomName() f, err := NewFile(name, &dag.Node{Data: ft.FilePBData(nil, 0)}, d, d.dserv) if err != nil { return err } wfd, err := f.Open(OpenWriteOnly, true) if err != nil { return err } r := io.LimitReader(randbo.New(), int64(77*rand.Intn(123))) _, err = io.Copy(wfd, r) if err != nil { return err } err = wfd.Close() if err != nil { return err } return nil }
func TestConcurrentReads(t *testing.T) { ctx, cancel := context.WithCancel(context.Background()) defer cancel() ds, rt := setupRoot(ctx, t) rootdir := rt.GetValue().(*Directory) path := "a/b/c" d := mkdirP(t, rootdir, path) buf := make([]byte, 2048) randbo.New().Read(buf) fi := fileNodeFromReader(t, ds, bytes.NewReader(buf)) err := d.AddChild("afile", fi) if err != nil { t.Fatal(err) } var wg sync.WaitGroup nloops := 100 for i := 0; i < 10; i++ { wg.Add(1) go func(me int) { defer wg.Done() mybuf := make([]byte, len(buf)) for j := 0; j < nloops; j++ { offset := rand.Intn(len(buf)) length := rand.Intn(len(buf) - offset) err := readFile(rt, "/a/b/c/afile", int64(offset), mybuf[:length]) if err != nil { t.Error("readfile failed: ", err) return } if !bytes.Equal(mybuf[:length], buf[offset:offset+length]) { t.Error("incorrect read!") } } }(i) } wg.Wait() }