Beispiel #1
0
func TestFileStore(t *testing.T) {
	var store chainstore.Store
	var err error

	Convey("Fsdb Open", t, func() {
		store = filestore.New(chainstore.TempDir(), 0755)
		err = nil
		So(err, ShouldEqual, nil)

		Convey("Put/Get/Del basic data", func() {
			err = store.Put("test.txt", []byte{1, 2, 3, 4})
			So(err, ShouldEqual, nil)

			data, err := store.Get("test.txt")
			So(err, ShouldEqual, nil)
			So(data, ShouldResemble, []byte{1, 2, 3, 4})
		})

		Convey("Auto-creating directories on put", func() {
			err = store.Put("hello/there/everyone.txt", []byte{1, 2, 3, 4})
			So(err, ShouldEqual, nil)
		})

	})
}
Beispiel #2
0
func TestLRUManager(t *testing.T) {
	var err error
	var store chainstore.Store
	var lru *lrumgr.LruManager
	var capacity int64 = 20

	Convey("LRUManager", t, func() {
		storeDir := chainstore.TempDir()

		store = filestore.New(storeDir, 0755)
		lru = lrumgr.New(capacity, store)

		// based on 10% cushion
		lru.Put("peter", []byte{1, 2, 3})
		lru.Put("jeff", []byte{4})
		lru.Put("julia", []byte{5, 6, 7, 8, 9, 10})
		lru.Put("janet", []byte{11, 12, 13})
		lru.Put("ted", []byte{14, 15, 16, 17, 18})

		remaining := capacity - 18
		So(lru.Capacity(), ShouldEqual, remaining)

		remaining = remaining + 4
		err = lru.Put("agnes", []byte{20, 21, 22, 23, 24, 25})
		So(lru.Capacity(), ShouldEqual, remaining)
		So(err, ShouldEqual, nil)

		var b []byte
		var err error

		// has been evicted..
		b, err = lru.Get("peter")
		if err != nil {
			t.Error(err)
			t.Fail()
		}
		if len(b) != 0 {
			t.Error("byte arrays do not match")
			t.Fail()
		}

		// exists
		b, err = lru.Get("janet")
		if err != nil {
			t.Error(err)
			t.Fail()
		}
		if !reflect.DeepEqual(b, []byte{11, 12, 13}) {
			t.Error("byte arrays do not match")
			t.Fail()
		}

	})
}
Beispiel #3
0
func TestAsyncChain(t *testing.T) {
	var ms, fs, bs, chain chainstore.Store
	var err error

	logger := log.New(os.Stdout, "", log.LstdFlags)

	Convey("Async chain", t, func() {
		storeDir := chainstore.TempDir()
		err = nil

		ms = memstore.New(100)
		fs = filestore.New(storeDir+"/filestore", 0755)
		bs = boltstore.New(storeDir+"/boltstore/bolt.db", "test")

		chain = chainstore.New(
			logmgr.New(logger, ""),
			ms,
			chainstore.Async(
				logmgr.New(logger, "async"),
				metricsmgr.New("chaintest", nil,
					fs,
					lrumgr.New(100, bs),
				),
			),
		)
		err = chain.Open()
		So(err, ShouldEqual, nil)

		Convey("Put/Get/Del", func() {
			v := []byte("value")
			err = chain.Put("k", v)
			So(err, ShouldEqual, nil)

			val, err := chain.Get("k")
			So(err, ShouldEqual, nil)
			So(v, ShouldResemble, v)

			val, err = ms.Get("k")
			So(err, ShouldEqual, nil)
			So(val, ShouldResemble, v)

			time.Sleep(10e6) // wait for async operation..

			val, err = fs.Get("k")
			So(err, ShouldEqual, nil)
			So(val, ShouldResemble, v)

			val, err = bs.Get("k")
			So(err, ShouldEqual, nil)
			So(val, ShouldResemble, v)
		})
	})

}
Beispiel #4
0
func TestBasicChain(t *testing.T) {
	var ms, fs, chain chainstore.Store
	var err error

	ctx := context.Background()

	logger := log.New(os.Stdout, "", log.LstdFlags)

	storeDir := tempDir()
	err = nil

	ms = memstore.New(100)
	fs = filestore.New(storeDir+"/filestore", 0755)

	chain = chainstore.New(
		logmgr.New(logger, ""),
		ms,
		fs,
	)

	assert := assert.New(t)

	err = chain.Open()
	assert.Nil(err)

	v := []byte("value")
	err = chain.Put(ctx, "k", v)
	assert.Nil(err)

	val, err := chain.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	val, err = ms.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	val, err = fs.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	err = chain.Del(ctx, "k")
	assert.Nil(err)

	val, err = fs.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(len(val), 0)

	val, err = chain.Get(ctx, "woo!@#")
	assert.NotNil(err)
}
Beispiel #5
0
func TestBasicChain(t *testing.T) {
	var ms, fs, chain chainstore.Store
	var err error

	logger := log.New(os.Stdout, "", log.LstdFlags)

	Convey("Basic chain", t, func() {
		storeDir := chainstore.TempDir()
		err = nil

		ms = memstore.New(100)
		fs = filestore.New(storeDir+"/filestore", 0755)

		chain = chainstore.New(
			logmgr.New(logger, ""),
			ms,
			fs,
		)
		err = chain.Open()
		So(err, ShouldEqual, nil)

		Convey("Put/Get/Del", func() {
			v := []byte("value")
			err = chain.Put("k", v)
			So(err, ShouldEqual, nil)

			val, err := chain.Get("k")
			So(err, ShouldEqual, nil)
			So(v, ShouldResemble, v)

			val, err = ms.Get("k")
			So(err, ShouldEqual, nil)
			So(val, ShouldResemble, v)

			val, err = fs.Get("k")
			So(err, ShouldEqual, nil)
			So(val, ShouldResemble, v)

			err = chain.Del("k")
			So(err, ShouldEqual, nil)

			val, err = fs.Get("k")
			So(err, ShouldEqual, nil)
			So(len(val), ShouldEqual, 0)

			val, err = chain.Get("woo!@#")
			So(err, ShouldNotBeNil)
		})
	})
}
Beispiel #6
0
func TestLRUManager(t *testing.T) {
	var err error
	var store chainstore.Store
	var lru *lruManager
	var capacity int64 = 20

	ctx := context.Background()

	store = filestore.New(tempDir(), 0755)

	lru = newLruManager(capacity, store)

	assert := assert.New(t)

	// based on 10% cushion
	lru.Put(ctx, "peter", []byte{1, 2, 3})
	lru.Put(ctx, "jeff", []byte{4})
	lru.Put(ctx, "julia", []byte{5, 6, 7, 8, 9, 10})
	lru.Put(ctx, "janet", []byte{11, 12, 13})
	lru.Put(ctx, "ted", []byte{14, 15, 16, 17, 18})

	remaining := capacity - 18
	assert.Equal(lru.Capacity(), remaining)

	remaining = remaining + 4
	err = lru.Put(ctx, "agnes", []byte{20, 21, 22, 23, 24, 25})
	assert.Equal(lru.Capacity(), remaining)
	assert.Nil(err)

	var b []byte

	// has been evicted..
	b, err = lru.Get(ctx, "peter")
	assert.Nil(err)
	assert.Equal(len(b), 0)

	// exists
	b, err = lru.Get(ctx, "janet")
	assert.Nil(err)
	assert.Equal(b, []byte{11, 12, 13})
}
Beispiel #7
0
func TestAsyncChain(t *testing.T) {
	var ms, fs, bs, chain chainstore.Store
	var err error

	logger := log.New(os.Stdout, "", log.LstdFlags)
	storeDir := tempDir()

	var errored atomic.Value

	ms = memstore.New(100)
	fs = filestore.New(storeDir+"/filestore", 0755)
	bs = boltstore.New(storeDir+"/boltstore/bolt.db", "test")

	chain = chainstore.New(
		logmgr.New(logger, ""),
		ms,
		chainstore.Async(
			func(err error) {
				log.Println("async error:", err)
				errored.Store(true)
			},
			logmgr.New(logger, "async"),
			&testStore{},
			metricsmgr.New("chaintest",
				fs,
				lrumgr.New(100, bs),
			),
		),
	)

	ctx := context.Background()
	assert := assert.New(t)

	err = chain.Open()
	assert.Nil(err)

	v := []byte("value")
	err = chain.Put(ctx, "k", v)
	assert.Nil(err)

	val, err := chain.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	val, err = ms.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	time.Sleep(time.Second * 1) // wait for async operation..

	val, err = fs.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	val, err = bs.Get(ctx, "k")
	assert.Nil(err)
	assert.Equal(val, v)

	//--

	// Lets make an error in async store
	assert.Nil(errored.Load())

	err = chain.Put(ctx, "bad", []byte("v"))
	assert.Nil(err) // no error because sync store took it fine

	time.Sleep(time.Second * 1) // wait for async operation..
	assert.NotEmpty(errored.Load())
}