func TestLFSCheckPointRepair(t *testing.T) { var err error defer utils.TDefer(t, &err) lfs, err := newIns() if err != nil { return } w, err := lfs.OpenWriter("/fff") if err != nil { return } w.Write([]byte("hello")) w.Close() lfs, err = New(cfg) if err != nil { return } r, err := lfs.OpenReader("/fff") if err != nil { return } buf := make([]byte, 5) _, err = r.Read(buf) if err != nil { return } if !bytes.Equal(buf, []byte("hello")) { err = logex.NewError("result not expected") return } }
// simplest test for write and read a file func TestSingleReadWrite(t *testing.T) { var err error defer utils.TDefer(t, &err) testData := genBlock(9 << 10) ins, err := newIns() if err != nil { return } defer ins.Pruge() f, err := ins.Open("lfsTestTopic") if err != nil { return } defer f.Close() w := &utils.Writer{f, 0} if err = safeWrite(w, testData); err != nil { return } testDataBuf := make([]byte, len(testData)) r := utils.NewBufio(&utils.Reader{f, 0}) if err = safeRead(r, testDataBuf); err != nil { return } return }
func TestLFSPersist(t *testing.T) { var err error defer utils.TDefer(t, &err) lfs, err := newIns() if err != nil { return } w, err := lfs.OpenWriter("/cao") if err != nil { return } w.Write([]byte("hello")) w.Close() w, err = lfs.OpenWriter("/kk") w.Write([]byte("jj")) w.Close() lfs.Close() lfs, err = New(cfg) if err != nil { return } buf := make([]byte, 5) r, err := lfs.OpenReader("/kk") if err != nil { return } n, err := r.Read(buf[:2]) if n != 2 || err != nil { return } if !bytes.Equal(buf[:2], []byte("jj")) { err = logex.NewError("result not expect") return } r, err = lfs.OpenReader("/cao") if err != nil { return } n, err = r.Read(buf) if n != 5 || err != nil { return } if !bytes.Equal(buf, []byte("hello")) { err = logex.NewError("result not expect") } }
func TestInode(t *testing.T) { var err error defer utils.TDefer(t, &err) InoName := rpc.NewString("inode") bit := uint(4) ino, err := NewInode(InoName, bit) if err != nil { return } if ino.blkSize != 1<<4 { err = logex.NewError("blkSize not expect") return } ino.ExtBlks(0, 12*(1<<4)) if ino.blkOff != 12 { err = logex.Define("block size not expect") return } buf := bytes.NewBuffer(nil) if err = ino.PWrite(buf); err != nil { return } ino2, err := ReadInode(buf, bit) if err != nil { return } if !reflect.DeepEqual(ino, ino2) { err = logex.Define("ino not expect") return } err = nil return }
func TestFileSize(t *testing.T) { var err error defer utils.TDefer(t, &err) lfs, err := newIns() if err != nil { return } f, err := lfs.Open("/hello") if err != nil { return } w := utils.NewWriter(f, f.Size()) _, err = w.Write([]byte("hello")) if err != nil { return } if f.Size() != 5 { err = logex.NewError("fileSize not expected: ", f.Size()) return } w.Close() // test restore lfs, err = New(cfg) if err != nil { return } f, err = lfs.Open("/hello") if err != nil { return } if f.Size() != 5 { err = logex.NewError("fileSize not expected: ", f.Size()) } }
// 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 }
// Testing read and write multiple files one time func TestMultiReadWrite(t *testing.T) { var err error defer utils.TDefer(t, &err) ins, err := newIns() if err != nil { return } defer ins.Close() label := []string{ "test1", "test2", "test3", "test4", "test5", "test6", "test7", } data := [][]byte{ genBlock(2), genBlock(20), genBlock(9), genBlock(15), genBlock(ins.cfg.blkSize + 2), genBlock(2*ins.cfg.blkSize + 21), genBlock(ins.cfg.blkSize + 1), } testTime := 2 for jj := 0; jj < testTime; jj++ { // write two file once ws := make([]*utils.Writer, len(label)) for i, l := range label { var w *utils.Writer w, err = ins.OpenWriter(l) if err != nil { return } ws[i] = w defer w.Close() } for i := range label { if err = safeWrite(ws[i], data[i]); err != nil { return } } } for jj := 0; jj < testTime; jj++ { // read two file once rs := make([]*utils.Reader, len(label)) for i, l := range label { var r *utils.Reader r, err = ins.OpenReader(l) if err != nil { return } defer r.Close() rs[i] = r } for i := range label { if err = safeReadExpect(rs[i], data[i]); err != nil { return } } } err = nil return }