Example #1
0
	"github.com/shaladdle/goaaw/filestore"
	"github.com/shaladdle/goaaw/filestore/remote"
	"github.com/shaladdle/goaaw/filestore/std"
	"github.com/shaladdle/goaaw/filestore/util"
	"github.com/shaladdle/goaaw/testutil"
)

type testInfo struct {
	name  string
	setup func(*testing.T) (fs.FileStore, func(), error)
}

var tests = []testInfo{
	{"std", func(t *testing.T) (fs.FileStore, func(), error) {
		te := testutil.NewTestEnv("testcase-stdfs", t)
		return std.New(te.Root()), func() { te.Teardown() }, nil
	}},
	{"remote", func(t *testing.T) (fs.FileStore, func(), error) {
		const hostport = "localhost:9000"

		te := testutil.NewTestEnv("testcase-net-remotefs", t)

		srv, err := remote.NewTCPServer(te.Root(), hostport)
		if err != nil {
			return nil, nil, err
		}

		cli, err := remote.NewTCPClient(hostport)
		if err != nil {
			return nil, nil, err
Example #2
0
// 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)
	}
}
Example #3
0
	"github.com/shaladdle/goaaw/fs/remote"
	anet "github.com/shaladdle/goaaw/net"
	"github.com/shaladdle/goaaw/testutil"
)

type testCase struct {
	name  string
	setup func(t *testing.T) (bs BlkStore, cleanup func())
}

var tests = []testCase{
	{"inmem", func(t *testing.T) (BlkStore, func()) {
		return NewMemStore(), func() {}
	}},
	{"disk", func(t *testing.T) (BlkStore, func()) {
		te := testutil.NewTestEnv("disk", t)
		return NewDiskStore(te.Root()), func() { te.Teardown() }
	}},
	{"remote", func(t *testing.T) (BlkStore, func()) {
		const hostport = "localhost:9000"
		te := testutil.NewTestEnv("remote", t)

		srv, err := remote.NewTCPServer(te.Root(), hostport)
		if err != nil {
			t.Errorf("setup error: %v", err)
		}

		bs, err := NewRemoteStore(anet.TCPDialer(hostport))
		if err != nil {
			t.Errorf("setup error: %v", err)
		}