示例#1
0
func genRandItems(t *testing.T, numItems, itemSize int64) []kvpair {
	items := make([]kvpair, numItems)
	for i := range items {
		buf := &bytes.Buffer{}
		if err := testutil.WriteRandFile(buf, itemSize); err != nil {
			t.Fatal(err)
		}

		items[i] = kvpair{
			key:   strconv.Itoa(i),
			value: buf.Bytes(),
		}
	}

	return items
}
示例#2
-1
// TestWriteRead writes a file and then reads it back and compares the hash to
// make sure the same contents was read and written.
func TestWriteRead(t *testing.T) {
	const readTimes = 5

	te := testutil.NewTestEnv("TestWriteRead", t)
	defer te.Teardown()

	fname := "test"

	testBody := func(i int, ti testInfo) {
		fs, cleanup, err := ti.setup(t)
		defer cleanup()
		if err != nil {
			t.Errorf("test %v: test initialization: %v", ti.name, err)
			return
		}

		w, err := fs.Create(fname)
		if err != nil {
			t.Errorf("test %v: create: %v", ti.name, err)
			return
		}
		wcleanup := func() {
			if err := w.Close(); err != nil {
				t.Errorf("test %v: error closing file: %v", ti.name, err)
			}
		}

		hIn, hOut := sha1.New(), sha1.New()
		hw := io.MultiWriter(w, hIn)

		err = testutil.WriteRandFile(hw, testutil.KB)
		if err != nil {
			t.Errorf("test %v: write: %v", ti.name, err)
			wcleanup()
			return
		}

		wcleanup()

		// We do this a few times in case someone is using a single
		// bytes.Buffer or some other non-idempotent mechanism for file reads.
		for j := 0; j < readTimes; j++ {
			r, err := fs.Open(fname)
			if err != nil {
				t.Errorf("test %v: open: %v", ti.name, err)
				return
			}
			defer r.Close()

			hOut.Reset()

			n, err := io.Copy(hOut, r)
			if n == 0 {
				t.Errorf("test %v: copy wrote 0 bytes", ti.name)
				return
			}
			if err != nil {
				t.Errorf("test %v: copy: %v", ti.name, err)
				return
			}

			if !bytes.Equal(hIn.Sum(nil), hOut.Sum(nil)) {
				t.Errorf("test %v: hashes did not match", ti.name)
			}
		}
	}

	for i, ti := range tests {
		testBody(i, ti)
	}
}