github.com/iotexproject/iotex-core@v1.14.1-rc1/db/kvstorewithbuffer_test.go (about) 1 package db 2 3 import ( 4 "bytes" 5 "context" 6 "testing" 7 8 "github.com/golang/mock/gomock" 9 "github.com/pkg/errors" 10 "github.com/stretchr/testify/require" 11 12 "github.com/iotexproject/iotex-core/db/batch" 13 "github.com/iotexproject/iotex-core/test/mock/mock_batch" 14 ) 15 16 func TestFlusher(t *testing.T) { 17 ctrl := gomock.NewController(t) 18 t.Run("create failed with nil kvStore", func(t *testing.T) { 19 f, err := NewKVStoreFlusher(nil, nil) 20 require.Nil(t, f) 21 require.Error(t, err) 22 require.Contains(t, err.Error(), "store cannot be nil") 23 }) 24 t.Run("fail to create with nil buffer", func(t *testing.T) { 25 store := NewMockKVStore(ctrl) 26 f, err := NewKVStoreFlusher(store, nil) 27 require.Nil(t, f) 28 require.Error(t, err) 29 require.Contains(t, err.Error(), "buffer cannot be nil") 30 }) 31 t.Run("create flusher successfully", func(t *testing.T) { 32 store := NewMockKVStore(ctrl) 33 buffer := mock_batch.NewMockCachedBatch(ctrl) 34 f, err := NewKVStoreFlusher(store, buffer) 35 require.NoError(t, err) 36 kvb := f.KVStoreWithBuffer() 37 expectedError := errors.New("failed to start") 38 ns := "namespace" 39 key := []byte("key") 40 value := []byte("value") 41 t.Run("fail to start kvStore with buffer", func(t *testing.T) { 42 store.EXPECT().Start(gomock.Any()).Return(err).Times(1) 43 require.Equal(t, err, kvb.Start(context.Background())) 44 }) 45 t.Run("fail to stop kvStore with buffer", func(t *testing.T) { 46 err = errors.New("failed to stop") 47 store.EXPECT().Stop(gomock.Any()).Return(err).Times(1) 48 require.Equal(t, err, kvb.Stop(context.Background())) 49 }) 50 t.Run("start kv store successfully", func(t *testing.T) { 51 store.EXPECT().Start(gomock.Any()).Return(nil).Times(1) 52 store.EXPECT().Stop(gomock.Any()).Return(nil).Times(1) 53 require.NoError(t, kvb.Start(context.Background())) 54 require.NoError(t, kvb.Stop(context.Background())) 55 }) 56 t.Run("fail to flush", func(t *testing.T) { 57 buffer.EXPECT().Translate(gomock.Any()).Return(buffer).Times(1) 58 store.EXPECT().WriteBatch(gomock.Any()).Return(expectedError).Times(1) 59 require.Equal(t, expectedError, f.Flush()) 60 }) 61 t.Run("flush successfully", func(t *testing.T) { 62 buffer.EXPECT().Translate(gomock.Any()).Return(buffer).Times(1) 63 store.EXPECT().WriteBatch(gomock.Any()).Return(nil).Times(1) 64 buffer.EXPECT().Lock().Times(1) 65 buffer.EXPECT().ClearAndUnlock().Times(1) 66 require.NoError(t, f.Flush()) 67 }) 68 t.Run("Get", func(t *testing.T) { 69 buffer.EXPECT().Get(ns, key).Return(value, nil).Times(1) 70 v, err := kvb.Get(ns, key) 71 require.True(t, bytes.Equal(value, v)) 72 require.NoError(t, err) 73 buffer.EXPECT().Get(ns, key).Return(nil, batch.ErrNotExist).Times(1) 74 store.EXPECT().Get(ns, key).Return(value, nil) 75 v, err = kvb.Get(ns, key) 76 require.True(t, bytes.Equal(value, v)) 77 require.NoError(t, err) 78 buffer.EXPECT().Get(ns, key).Return(nil, batch.ErrAlreadyDeleted).Times(1) 79 v, err = kvb.Get(ns, key) 80 require.Nil(t, v) 81 require.Equal(t, errors.Cause(err), ErrNotExist) 82 }) 83 t.Run("Snapshot", func(t *testing.T) { 84 buffer.EXPECT().Snapshot().Return(1).Times(1) 85 require.Equal(t, 1, kvb.Snapshot()) 86 }) 87 t.Run("Revert", func(t *testing.T) { 88 buffer.EXPECT().RevertSnapshot(gomock.Any()).Return(expectedError).Times(1) 89 require.Equal(t, expectedError, kvb.RevertSnapshot(1)) 90 buffer.EXPECT().RevertSnapshot(gomock.Any()).Return(nil).Times(1) 91 require.NoError(t, kvb.RevertSnapshot(1)) 92 }) 93 t.Run("Size", func(t *testing.T) { 94 buffer.EXPECT().Size().Return(5).Times(1) 95 require.Equal(t, 5, kvb.Size()) 96 }) 97 t.Run("SerializeQueue", func(t *testing.T) { 98 buffer.EXPECT().SerializeQueue(gomock.Any(), gomock.Any()).Return(value).Times(1) 99 require.Equal(t, value, f.SerializeQueue()) 100 }) 101 t.Run("MustPut", func(t *testing.T) { 102 buffer.EXPECT().Put(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).Times(1) 103 kvb.MustPut(ns, key, value) 104 }) 105 t.Run("MustDelete", func(t *testing.T) { 106 buffer.EXPECT().Delete(gomock.Any(), gomock.Any(), gomock.Any()).Times(1) 107 kvb.MustDelete(ns, key) 108 }) 109 }) 110 }