Exemple #1
0
func TestFileOp(t *testing.T) {
	dirname := utils.GetRoot("/test/bitmap.file")

	f, err := NewFile(dirname)
	if err != nil {
		t.Fatal(err)
	}
	f.Delete()
	if _, err := os.Stat(dirname); os.IsExist(err) {
		t.Fatal("expect not exist")
	}

	f, err = NewFileEx(dirname, 11)
	if err != nil {
		t.Fatal(err)
	}
	defer f.Close()
	buf2 := make([]byte, 1024)
	copy(buf2, []byte("hello"))
	buf := make([]byte, 1024)

	{
		n, err := f.WriteAt(buf2[:5], int64(f.chunkSize)-3)
		if err != nil {
			t.Fatal(err)
		}
		nRead, err := f.ReadAt(buf[:5], int64(f.chunkSize)-3)
		if err != nil && !logex.Equal(err, io.EOF) {
			t.Fatal(err)
		}
		if !bytes.Equal(buf[:nRead], buf2[:n]) {
			t.Fatal("result not expect")
		}
	}

	if f.Size() != int64(f.chunkSize)-3+5 {
		t.Fatal("size not expect")
	}

	for i := 0; i < 20; i++ {
		offset := int64(utils.RandInt(4096))
		n, err := f.WriteAt(buf2[:5], 1024+offset)
		if err != nil {
			t.Fatal(err)
		}
		nRead, err := f.ReadAt(buf[:5], 1024+offset)
		if err != nil && !logex.Equal(err, io.EOF) {
			logex.Error(err)
			t.Fatal(err)
		}
		if !bytes.Equal(buf[:nRead], buf2[:n]) {
			t.Fatal("result not expect")
		}
	}

	if _, err := f.ReadAt(buf, -1); err == nil {
		t.Fatal("expect error")
	}
}
Exemple #2
0
// Write data via N reqs and read them via M reqs
func TestNWriteMRead(t *testing.T) {
	var err error
	defer utils.TDefer(t, &err)

	ins, err := newIns()
	if err != nil {
		return
	}
	defer ins.Close()
	factor := 50

	w, err := ins.OpenWriter("test1")
	if err != nil {
		return
	}
	defer w.Close()

	lengths := make([]int, factor)
	datas := make([][]byte, factor)
	expect := make([]byte, 0)
	total := 0
	for i := range lengths {
		l := 1 + utils.RandInt(200*cfg.blkSize)
		total += l
		datas[i] = bytes.Repeat([]byte(utils.RandString(1)), l)
		expect = append(expect, datas[i]...)
	}

	for _, data := range datas {
		if err = safeWrite(w, data); err != nil {
			return
		}
	}

	r, err := ins.OpenReader("test1")
	if err != nil {
		return
	}
	defer r.Close()

	maxSize := 1 << 12
	pStart := 0
	pEnd := maxSize
	for pEnd > pStart {
		if pEnd > len(expect) {
			pEnd = len(expect)
		}

		if err = safeReadExpect(r, expect[pStart:pEnd]); err != nil {
			return
		}
		pStart = pEnd
		if pEnd < len(expect) {
			pEnd += maxSize
		}
	}

	// expect eof
	r.Offset -= 1
	n, err := r.Read([]byte("padding"))
	if !logex.Equal(err, io.EOF) || n != 1 {
		err = logex.NewError("expect EOF, got ", n, err)
		return
	}

	n, err = r.Read([]byte("pad"))
	if err != io.EOF || n != 0 {
		err = logex.NewError("expect EOF, got ", n)
		return
	}

	err = nil
}