Ejemplo n.º 1
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)
		})
	})

}
Ejemplo n.º 2
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)
}
Ejemplo n.º 3
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)
		})
	})
}
Ejemplo n.º 4
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())
}