github.com/ledgerwatch/erigon-lib@v1.0.0/kv/remotedbserver/server_test.go (about)

     1  /*
     2     Copyright 2021 Erigon contributors
     3  
     4     Licensed under the Apache License, Version 2.0 (the "License");
     5     you may not use this file except in compliance with the License.
     6     You may obtain a copy of the License at
     7  
     8         http://www.apache.org/licenses/LICENSE-2.0
     9  
    10     Unless required by applicable law or agreed to in writing, software
    11     distributed under the License is distributed on an "AS IS" BASIS,
    12     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13     See the License for the specific language governing permissions and
    14     limitations under the License.
    15  */
    16  
    17  package remotedbserver
    18  
    19  import (
    20  	"context"
    21  	"runtime"
    22  	"testing"
    23  
    24  	"github.com/ledgerwatch/erigon-lib/kv"
    25  	"github.com/ledgerwatch/erigon-lib/kv/memdb"
    26  	"github.com/ledgerwatch/log/v3"
    27  	"github.com/stretchr/testify/require"
    28  	"golang.org/x/sync/errgroup"
    29  )
    30  
    31  func TestKvServer_renew(t *testing.T) {
    32  	if runtime.GOOS == "windows" {
    33  		t.Skip("fix me on win please")
    34  	}
    35  
    36  	require, ctx, db := require.New(t), context.Background(), memdb.NewTestDB(t)
    37  	require.NoError(db.Update(ctx, func(tx kv.RwTx) error {
    38  		wc, err := tx.RwCursorDupSort(kv.PlainState)
    39  		require.NoError(err)
    40  		require.NoError(wc.Append([]byte{1}, []byte{1}))
    41  		require.NoError(wc.Append([]byte{1}, []byte{2}))
    42  		require.NoError(wc.Append([]byte{2}, []byte{1}))
    43  		require.NoError(wc.Append([]byte{3}, []byte{1}))
    44  		return nil
    45  	}))
    46  
    47  	s := NewKvServer(ctx, db, nil, nil, log.New())
    48  	g, ctx := errgroup.WithContext(ctx)
    49  	testCase := func() error {
    50  		id, err := s.begin(ctx)
    51  		if err != nil {
    52  			return err
    53  		}
    54  		var c, c2 kv.Cursor
    55  		if err = s.with(id, func(tx kv.Tx) error {
    56  			c, err = tx.Cursor(kv.PlainState)
    57  			return err
    58  		}); err != nil {
    59  			return err
    60  		}
    61  		k, v, err := c.First()
    62  		require.NoError(err)
    63  		require.Equal([]byte{1}, k)
    64  		require.Equal([]byte{1}, v)
    65  
    66  		if err = s.renew(ctx, id); err != nil {
    67  			return err
    68  		}
    69  
    70  		if err = s.with(id, func(tx kv.Tx) error {
    71  			c, err = tx.Cursor(kv.PlainState)
    72  			if err != nil {
    73  				return err
    74  			}
    75  			c2, err = tx.Cursor(kv.PlainState)
    76  			return err
    77  		}); err != nil {
    78  			return err
    79  		}
    80  
    81  		k, v, err = c.Next()
    82  		require.NoError(err)
    83  		require.Equal([]byte{1}, k)
    84  		require.Equal([]byte{1}, v)
    85  		k, v, err = c2.Next()
    86  		require.NoError(err)
    87  		require.Equal([]byte{1}, k)
    88  		require.Equal([]byte{1}, v)
    89  
    90  		s.rollback(id)
    91  		return nil
    92  	}
    93  	for i := 0; i < 10; i++ {
    94  		g.Go(testCase)
    95  	}
    96  	require.NoError(g.Wait())
    97  }