github.com/hyperledger/burrow@v0.34.5-0.20220512172541-77f09336001d/forensics/storage/channel_iterator_test.go (about) 1 package storage 2 3 import ( 4 "testing" 5 6 "github.com/stretchr/testify/assert" 7 "github.com/stretchr/testify/require" 8 9 dbm "github.com/tendermint/tm-db" 10 ) 11 12 func TestNewChannelIterator(t *testing.T) { 13 ch := make(chan KVPair) 14 go sendKVPair(ch, kvPairs("a", "hello", "b", "channel", "c", "this is nice")) 15 ci := NewChannelIterator(ch, []byte("a"), []byte("c")) 16 checkItem(t, ci, []byte("a"), []byte("hello")) 17 checkNext(t, ci, true) 18 checkItem(t, ci, []byte("b"), []byte("channel")) 19 checkNext(t, ci, true) 20 checkItem(t, ci, []byte("c"), []byte("this is nice")) 21 checkNext(t, ci, false) 22 checkInvalid(t, ci) 23 } 24 25 func checkInvalid(t *testing.T, itr dbm.Iterator) { 26 checkValid(t, itr, false) 27 checkKeyPanics(t, itr) 28 checkValuePanics(t, itr) 29 checkNextPanics(t, itr) 30 } 31 32 func checkValid(t *testing.T, itr dbm.Iterator, expected bool) { 33 valid := itr.Valid() 34 require.Equal(t, expected, valid) 35 } 36 37 func checkNext(t *testing.T, itr dbm.Iterator, expected bool) { 38 itr.Next() 39 valid := itr.Valid() 40 require.Equal(t, expected, valid) 41 } 42 43 func checkNextPanics(t *testing.T, itr dbm.Iterator) { 44 assert.Panics(t, func() { itr.Next() }, "checkNextPanics expected panic but didn't") 45 } 46 func checkKeyPanics(t *testing.T, itr dbm.Iterator) { 47 assert.Panics(t, func() { itr.Key() }, "checkKeyPanics expected panic but didn't") 48 } 49 50 func checkValuePanics(t *testing.T, itr dbm.Iterator) { 51 assert.Panics(t, func() { itr.Key() }, "checkValuePanics expected panic but didn't") 52 }