github.com/lbryio/lbcd@v0.22.119/claimtrie/node/noderepo/noderepo_test.go (about)

     1  package noderepo
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/lbryio/lbcd/claimtrie/change"
     7  	"github.com/lbryio/lbcd/claimtrie/node"
     8  
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  var (
    13  	out1          = node.NewOutPointFromString("0000000000000000000000000000000000000000000000000000000000000000:1")
    14  	testNodeName1 = []byte("name1")
    15  )
    16  
    17  func TestPebble(t *testing.T) {
    18  
    19  	r := require.New(t)
    20  
    21  	repo, err := NewPebble(t.TempDir())
    22  	r.NoError(err)
    23  	defer func() {
    24  		err := repo.Close()
    25  		r.NoError(err)
    26  	}()
    27  
    28  	cleanup := func() {
    29  		lowerBound := testNodeName1
    30  		upperBound := append(testNodeName1, byte(0))
    31  		err := repo.db.DeleteRange(lowerBound, upperBound, nil)
    32  		r.NoError(err)
    33  	}
    34  
    35  	testNodeRepo(t, repo, func() {}, cleanup)
    36  }
    37  
    38  func testNodeRepo(t *testing.T, repo node.Repo, setup, cleanup func()) {
    39  
    40  	r := require.New(t)
    41  
    42  	chg := change.NewChange(change.AddClaim).SetName(testNodeName1).SetOutPoint(out1)
    43  
    44  	testcases := []struct {
    45  		name     string
    46  		height   int32
    47  		changes  []change.Change
    48  		expected []change.Change
    49  	}{
    50  		{
    51  			"test 1",
    52  			1,
    53  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    54  			[]change.Change{chg.SetHeight(1)},
    55  		},
    56  		{
    57  			"test 2",
    58  			2,
    59  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    60  			[]change.Change{chg.SetHeight(1)},
    61  		},
    62  		{
    63  			"test 3",
    64  			3,
    65  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    66  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3)},
    67  		},
    68  		{
    69  			"test 4",
    70  			4,
    71  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    72  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3)},
    73  		},
    74  		{
    75  			"test 5",
    76  			5,
    77  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    78  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    79  		},
    80  		{
    81  			"test 6",
    82  			6,
    83  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    84  			[]change.Change{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
    85  		},
    86  	}
    87  
    88  	for _, tt := range testcases {
    89  
    90  		setup()
    91  
    92  		err := repo.AppendChanges(tt.changes)
    93  		r.NoError(err)
    94  
    95  		changes, err := repo.LoadChanges(testNodeName1)
    96  		r.NoError(err)
    97  		r.Equalf(tt.expected, changes[:len(tt.expected)], tt.name)
    98  
    99  		cleanup()
   100  	}
   101  
   102  	testcases2 := []struct {
   103  		name     string
   104  		height   int32
   105  		changes  [][]change.Change
   106  		expected []change.Change
   107  	}{
   108  		{
   109  			"Save in 2 batches, and load up to 1",
   110  			1,
   111  			[][]change.Change{
   112  				{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
   113  				{chg.SetHeight(6), chg.SetHeight(8), chg.SetHeight(9)},
   114  			},
   115  			[]change.Change{chg.SetHeight(1)},
   116  		},
   117  		{
   118  			"Save in 2 batches, and load up to 9",
   119  			9,
   120  			[][]change.Change{
   121  				{chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5)},
   122  				{chg.SetHeight(6), chg.SetHeight(8), chg.SetHeight(9)},
   123  			},
   124  			[]change.Change{
   125  				chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5),
   126  				chg.SetHeight(6), chg.SetHeight(8), chg.SetHeight(9),
   127  			},
   128  		},
   129  		{
   130  			"Save in 3 batches, and load up to 8",
   131  			8,
   132  			[][]change.Change{
   133  				{chg.SetHeight(1), chg.SetHeight(3)},
   134  				{chg.SetHeight(5)},
   135  				{chg.SetHeight(6), chg.SetHeight(8), chg.SetHeight(9)},
   136  			},
   137  			[]change.Change{
   138  				chg.SetHeight(1), chg.SetHeight(3), chg.SetHeight(5),
   139  				chg.SetHeight(6), chg.SetHeight(8),
   140  			},
   141  		},
   142  	}
   143  
   144  	for _, tt := range testcases2 {
   145  
   146  		setup()
   147  
   148  		for _, changes := range tt.changes {
   149  			err := repo.AppendChanges(changes)
   150  			r.NoError(err)
   151  		}
   152  
   153  		changes, err := repo.LoadChanges(testNodeName1)
   154  		r.NoError(err)
   155  		r.Equalf(tt.expected, changes[:len(tt.expected)], tt.name)
   156  
   157  		cleanup()
   158  	}
   159  }
   160  
   161  func TestIterator(t *testing.T) {
   162  
   163  	r := require.New(t)
   164  
   165  	repo, err := NewPebble(t.TempDir())
   166  	r.NoError(err)
   167  	defer func() {
   168  		err := repo.Close()
   169  		r.NoError(err)
   170  	}()
   171  
   172  	creation := []change.Change{
   173  		{Name: []byte("test\x00"), Height: 5},
   174  		{Name: []byte("test\x00\x00"), Height: 5},
   175  		{Name: []byte("test\x00b"), Height: 5},
   176  		{Name: []byte("test\x00\xFF"), Height: 5},
   177  		{Name: []byte("testa"), Height: 5},
   178  	}
   179  	err = repo.AppendChanges(creation)
   180  	r.NoError(err)
   181  
   182  	i := 0
   183  	repo.IterateChildren([]byte{}, func(changes []change.Change) bool {
   184  		r.Equal(creation[i], changes[0])
   185  		i++
   186  		return true
   187  	})
   188  }