github.com/decred/dcrlnd@v0.7.6/kvdb/etcd_test.go (about) 1 //go:build kvdb_etcd 2 // +build kvdb_etcd 3 4 package kvdb 5 6 import ( 7 "fmt" 8 "testing" 9 10 "github.com/btcsuite/btcwallet/walletdb" 11 "github.com/decred/dcrlnd/kvdb/etcd" 12 "github.com/stretchr/testify/require" 13 ) 14 15 var ( 16 bkey = etcd.BucketKey 17 bval = etcd.BucketVal 18 vkey = etcd.ValueKey 19 ) 20 21 func TestEtcd(t *testing.T) { 22 tests := []struct { 23 name string 24 debugOnly bool 25 test func(*testing.T, walletdb.DB) 26 expectedDb map[string]string 27 }{ 28 { 29 name: "read cursor empty interval", 30 test: testReadCursorEmptyInterval, 31 }, 32 { 33 name: "read cursor non empty interval", 34 test: testReadCursorNonEmptyInterval, 35 }, 36 { 37 name: "read write cursor", 38 test: testReadWriteCursor, 39 expectedDb: map[string]string{ 40 bkey("apple"): bval("apple"), 41 vkey("a", "apple"): "0", 42 vkey("c", "apple"): "3", 43 vkey("cx", "apple"): "x", 44 vkey("cy", "apple"): "y", 45 vkey("da", "apple"): "3", 46 vkey("f", "apple"): "5", 47 }, 48 }, 49 { 50 name: "read write cursor with bucket and value", 51 test: testReadWriteCursorWithBucketAndValue, 52 expectedDb: map[string]string{ 53 bkey("apple"): bval("apple"), 54 bkey("apple", "banana"): bval("apple", "banana"), 55 bkey("apple", "pear"): bval("apple", "pear"), 56 vkey("key", "apple"): "val", 57 }, 58 }, 59 { 60 name: "bucket creation", 61 test: testBucketCreation, 62 expectedDb: map[string]string{ 63 bkey("apple"): bval("apple"), 64 bkey("apple", "banana"): bval("apple", "banana"), 65 bkey("apple", "mango"): bval("apple", "mango"), 66 bkey("apple", "banana", "pear"): bval("apple", "banana", "pear"), 67 }, 68 }, 69 { 70 name: "bucket deletion", 71 test: testBucketDeletion, 72 expectedDb: map[string]string{ 73 bkey("apple"): bval("apple"), 74 bkey("apple", "banana"): bval("apple", "banana"), 75 vkey("key1", "apple", "banana"): "val1", 76 vkey("key3", "apple", "banana"): "val3", 77 }, 78 }, 79 { 80 name: "bucket for each", 81 test: testBucketForEach, 82 expectedDb: map[string]string{ 83 bkey("apple"): bval("apple"), 84 bkey("apple", "banana"): bval("apple", "banana"), 85 vkey("key1", "apple"): "val1", 86 vkey("key2", "apple"): "val2", 87 vkey("key3", "apple"): "val3", 88 vkey("key1", "apple", "banana"): "val1", 89 vkey("key2", "apple", "banana"): "val2", 90 vkey("key3", "apple", "banana"): "val3", 91 }, 92 }, 93 { 94 name: "bucket for each with error", 95 test: testBucketForEachWithError, 96 expectedDb: map[string]string{ 97 bkey("apple"): bval("apple"), 98 bkey("apple", "banana"): bval("apple", "banana"), 99 bkey("apple", "pear"): bval("apple", "pear"), 100 vkey("key1", "apple"): "val1", 101 vkey("key2", "apple"): "val2", 102 }, 103 }, 104 { 105 name: "bucket sequence", 106 test: testBucketSequence, 107 }, 108 { 109 name: "key clash", 110 debugOnly: true, 111 test: testKeyClash, 112 expectedDb: map[string]string{ 113 bkey("apple"): bval("apple"), 114 bkey("apple", "banana"): bval("apple", "banana"), 115 vkey("key", "apple"): "val", 116 }, 117 }, 118 { 119 name: "bucket create delete", 120 test: testBucketCreateDelete, 121 expectedDb: map[string]string{ 122 vkey("banana", "apple"): "value", 123 bkey("apple"): bval("apple"), 124 }, 125 }, 126 { 127 name: "tx manual commit", 128 test: testTxManualCommit, 129 expectedDb: map[string]string{ 130 bkey("apple"): bval("apple"), 131 vkey("testKey", "apple"): "testVal", 132 }, 133 }, 134 { 135 name: "tx rollback", 136 test: testTxRollback, 137 expectedDb: map[string]string{}, 138 }, 139 { 140 name: "prefetch", 141 test: testPrefetch, 142 expectedDb: map[string]string{}, 143 }, 144 } 145 146 for _, test := range tests { 147 test := test 148 149 if test.debugOnly && !etcdDebug { 150 continue 151 } 152 153 rwLock := []bool{false, true} 154 for _, doRwLock := range rwLock { 155 name := fmt.Sprintf("%v/RWLock=%v", test.name, doRwLock) 156 157 t.Run(name, func(t *testing.T) { 158 t.Parallel() 159 160 f := etcd.NewEtcdTestFixture(t) 161 defer f.Cleanup() 162 163 test.test(t, f.NewBackend(doRwLock)) 164 165 if test.expectedDb != nil { 166 dump := f.Dump() 167 require.Equal(t, test.expectedDb, dump) 168 } 169 }) 170 } 171 } 172 }