github.com/turingchain2020/turingchain@v1.1.21/executor/localdb_test.go (about)

     1  package executor_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	dbm "github.com/turingchain2020/turingchain/common/db"
     7  	"github.com/turingchain2020/turingchain/executor"
     8  	"github.com/turingchain2020/turingchain/types"
     9  	"github.com/turingchain2020/turingchain/util/testnode"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestLocalDBGet(t *testing.T) {
    14  	mock33 := testnode.New("", nil)
    15  	defer mock33.Close()
    16  	db := executor.NewLocalDB(mock33.GetClient(), false)
    17  	defer db.(*executor.LocalDB).Close()
    18  	testDBGet(t, db)
    19  }
    20  
    21  func TestLocalDBGetReadOnly(t *testing.T) {
    22  	mock33 := testnode.New("", nil)
    23  	defer mock33.Close()
    24  	db := executor.NewLocalDB(mock33.GetClient(), true)
    25  	defer db.(*executor.LocalDB).Close()
    26  	testDBGet(t, db)
    27  }
    28  
    29  func TestLocalDBEnable(t *testing.T) {
    30  	mock33 := testnode.New("", nil)
    31  	defer mock33.Close()
    32  	db := executor.NewLocalDB(mock33.GetClient(), false)
    33  	ldb := db.(*executor.LocalDB)
    34  	defer ldb.Close()
    35  	_, err := ldb.Get([]byte("hello"))
    36  	assert.Equal(t, err, types.ErrNotFound)
    37  	ldb.DisableRead()
    38  	_, err = ldb.Get([]byte("hello"))
    39  
    40  	assert.Equal(t, err, types.ErrDisableRead)
    41  	_, err = ldb.List(nil, nil, 0, 0)
    42  	assert.Equal(t, err, types.ErrDisableRead)
    43  	ldb.EnableRead()
    44  	_, err = ldb.Get([]byte("hello"))
    45  	assert.Equal(t, err, types.ErrNotFound)
    46  	_, err = ldb.List(nil, nil, 0, 0)
    47  	assert.Equal(t, err, nil)
    48  	ldb.DisableWrite()
    49  	err = ldb.Set([]byte("hello"), nil)
    50  	assert.Equal(t, err, types.ErrDisableWrite)
    51  	ldb.EnableWrite()
    52  	err = ldb.Set([]byte("hello"), nil)
    53  	assert.Equal(t, err, nil)
    54  
    55  }
    56  
    57  func BenchmarkLocalDBGet(b *testing.B) {
    58  	mock33 := testnode.New("", nil)
    59  	defer mock33.Close()
    60  	db := executor.NewLocalDB(mock33.GetClient(), false)
    61  	defer db.(*executor.LocalDB).Close()
    62  
    63  	err := db.Set([]byte("k1"), []byte("v1"))
    64  	assert.Nil(b, err)
    65  	b.StartTimer()
    66  	for i := 0; i < b.N; i++ {
    67  		v, err := db.Get([]byte("k1"))
    68  		assert.Nil(b, err)
    69  		assert.Equal(b, v, []byte("v1"))
    70  	}
    71  }
    72  
    73  func TestLocalDBTxGet(t *testing.T) {
    74  	mock33 := testnode.New("", nil)
    75  	defer mock33.Close()
    76  	db := executor.NewLocalDB(mock33.GetClient(), false)
    77  	testTxGet(t, db)
    78  }
    79  
    80  func testDBGet(t *testing.T, db dbm.KV) {
    81  	err := db.Set([]byte("k1"), []byte("v1"))
    82  	assert.Nil(t, err)
    83  	v, err := db.Get([]byte("k1"))
    84  	assert.Nil(t, err)
    85  	assert.Equal(t, v, []byte("v1"))
    86  
    87  	err = db.Set([]byte("k1"), []byte("v11"))
    88  	assert.Nil(t, err)
    89  	v, err = db.Get([]byte("k1"))
    90  	assert.Nil(t, err)
    91  	assert.Equal(t, v, []byte("v11"))
    92  }
    93  
    94  func testTxGet(t *testing.T, db dbm.KV) {
    95  	//新版本
    96  	db.Begin()
    97  	err := db.Set([]byte("k1"), []byte("v1"))
    98  	assert.Nil(t, err)
    99  	v, err := db.Get([]byte("k1"))
   100  	assert.Nil(t, err)
   101  	assert.Equal(t, v, []byte("v1"))
   102  
   103  	db.Commit()
   104  	v, err = db.Get([]byte("k1"))
   105  	assert.Nil(t, err)
   106  	assert.Equal(t, v, []byte("v1"))
   107  
   108  	//在非transaction中set,直接set成功,不能rollback
   109  	err = db.Set([]byte("k1"), []byte("v11"))
   110  	assert.Nil(t, err)
   111  
   112  	db.Begin()
   113  	v, err = db.Get([]byte("k1"))
   114  	assert.Nil(t, err)
   115  	assert.Equal(t, v, []byte("v11"))
   116  
   117  	err = db.Set([]byte("k1"), []byte("v12"))
   118  	assert.Nil(t, err)
   119  	v, err = db.Get([]byte("k1"))
   120  	assert.Nil(t, err)
   121  	assert.Equal(t, v, []byte("v12"))
   122  
   123  	db.Rollback()
   124  	v, err = db.Get([]byte("k1"))
   125  	assert.Nil(t, err)
   126  	assert.Equal(t, v, []byte("v11"))
   127  }
   128  
   129  func TestLocalDB(t *testing.T) {
   130  	mock33 := testnode.New("", nil)
   131  	defer mock33.Close()
   132  	db := executor.NewLocalDB(mock33.GetClient(), false)
   133  	defer db.(*executor.LocalDB).Close()
   134  	err := db.Set([]byte("k1"), []byte("v1"))
   135  	assert.Nil(t, err)
   136  	v, err := db.Get([]byte("k1"))
   137  	assert.Nil(t, err)
   138  	assert.Equal(t, v, []byte("v1"))
   139  
   140  	err = db.Set([]byte("k1"), []byte("v11"))
   141  	assert.Nil(t, err)
   142  	v, err = db.Get([]byte("k1"))
   143  	assert.Nil(t, err)
   144  	assert.Equal(t, v, []byte("v11"))
   145  
   146  	//beigin and rollback not imp
   147  	db.Begin()
   148  	err = db.Set([]byte("k2"), []byte("v2"))
   149  	assert.Nil(t, err)
   150  	db.Rollback()
   151  	_, err = db.Get([]byte("k2"))
   152  	assert.Equal(t, err, types.ErrNotFound)
   153  	err = db.Set([]byte("k2"), []byte("v2"))
   154  	assert.Nil(t, err)
   155  	//get
   156  	v, err = db.Get([]byte("k2"))
   157  	assert.Nil(t, err)
   158  	assert.Equal(t, v, []byte("v2"))
   159  	//list
   160  	values, err := db.List([]byte("k"), nil, 0, 0)
   161  	assert.Nil(t, err)
   162  	assert.Equal(t, len(values), 2)
   163  	assert.Equal(t, string(values[0]), "v2")
   164  	assert.Equal(t, string(values[1]), "v11")
   165  	err = db.Commit()
   166  	assert.Nil(t, err)
   167  	//get
   168  	v, err = db.Get([]byte("k2"))
   169  	assert.Nil(t, err)
   170  	assert.Equal(t, v, []byte("v2"))
   171  	//list
   172  	values, err = db.List([]byte("k"), nil, 0, 0)
   173  	assert.Nil(t, err)
   174  	assert.Equal(t, len(values), 2)
   175  	assert.Equal(t, string(values[0]), "v2")
   176  	assert.Equal(t, string(values[1]), "v11")
   177  }