github.com/ari-anchor/sei-tendermint@v0.0.0-20230519144642-dc826b7b56bb/internal/dbsync/syncer_test.go (about)

     1  package dbsync
     2  
     3  import (
     4  	"context"
     5  	"crypto/md5"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/ari-anchor/sei-tendermint/config"
    10  	"github.com/ari-anchor/sei-tendermint/internal/state"
    11  	"github.com/ari-anchor/sei-tendermint/libs/log"
    12  	"github.com/ari-anchor/sei-tendermint/proto/tendermint/dbsync"
    13  	"github.com/ari-anchor/sei-tendermint/types"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  func getTestSyncer(t *testing.T) *Syncer {
    18  	baseConfig := config.DefaultBaseConfig()
    19  	dbsyncConfig := config.DefaultDBSyncConfig()
    20  	dbsyncConfig.TimeoutInSeconds = 99999
    21  	dbsyncConfig.NoFileSleepInSeconds = 5
    22  	dbsyncConfig.FileWorkerTimeout = 10
    23  	syncer := NewSyncer(
    24  		log.NewNopLogger(),
    25  		*dbsyncConfig,
    26  		baseConfig,
    27  		true,
    28  		func(ctx context.Context) error { return nil },
    29  		func(ctx context.Context, ni types.NodeID, u uint64, s string) error { return nil },
    30  		func(ctx context.Context, u uint64) (state.State, *types.Commit, error) {
    31  			return state.State{}, nil, nil
    32  		},
    33  		func(ctx context.Context, s state.State, c *types.Commit) error { return nil },
    34  		func(s *Syncer) {
    35  			s.applicationDBDirectory = t.TempDir()
    36  			s.wasmStateDirectory = t.TempDir()
    37  		},
    38  	)
    39  	syncer.active = true
    40  	syncer.applicationDBDirectory = t.TempDir()
    41  	syncer.wasmStateDirectory = t.TempDir()
    42  	return syncer
    43  }
    44  
    45  func TestSetMetadata(t *testing.T) {
    46  	syncer := getTestSyncer(t)
    47  	// initial
    48  	syncer.SetMetadata(context.Background(), types.NodeID("someone"), &dbsync.MetadataResponse{
    49  		Height:      1,
    50  		Hash:        []byte("hash"),
    51  		Filenames:   []string{"f1"},
    52  		Md5Checksum: [][]byte{[]byte("sum")},
    53  	})
    54  	syncer.fileWorkerCancelFn()
    55  	require.Equal(t, uint64(1), syncer.heightToSync)
    56  	require.NotNil(t, syncer.metadataSetAt)
    57  	require.Equal(t, 1, len(syncer.expectedChecksums))
    58  	require.Equal(t, 1, len(syncer.peersToSync))
    59  
    60  	// second time
    61  	syncer.SetMetadata(context.Background(), types.NodeID("someone else"), &dbsync.MetadataResponse{
    62  		Height:      1,
    63  		Hash:        []byte("hash"),
    64  		Filenames:   []string{"f1"},
    65  		Md5Checksum: [][]byte{[]byte("sum")},
    66  	})
    67  	require.Equal(t, uint64(1), syncer.heightToSync)
    68  	require.NotNil(t, syncer.metadataSetAt)
    69  	require.Equal(t, 1, len(syncer.expectedChecksums))
    70  	require.Equal(t, 2, len(syncer.peersToSync))
    71  }
    72  
    73  func TestFileProcessHappyPath(t *testing.T) {
    74  	// successful process
    75  	syncer := getTestSyncer(t)
    76  	data := []byte("data")
    77  	sum := md5.Sum(data)
    78  	syncer.SetMetadata(context.Background(), types.NodeID("someone"), &dbsync.MetadataResponse{
    79  		Height:      1,
    80  		Hash:        []byte("hash"),
    81  		Filenames:   []string{"f1"},
    82  		Md5Checksum: [][]byte{sum[:]},
    83  	})
    84  	for {
    85  		syncer.mtx.RLock()
    86  		_, ok := syncer.pendingFiles["f1"]
    87  		syncer.mtx.RUnlock()
    88  		if ok {
    89  			break
    90  		}
    91  	}
    92  	syncer.PushFile(&dbsync.FileResponse{
    93  		Height:   1,
    94  		Filename: "f1",
    95  		Data:     data,
    96  	})
    97  	syncer.Process(context.Background())
    98  }
    99  
   100  func TestFileProcessTimeoutReprocess(t *testing.T) {
   101  	// successful process
   102  	syncer := getTestSyncer(t)
   103  	data := []byte("data")
   104  	sum := md5.Sum(data)
   105  	syncer.SetMetadata(context.Background(), types.NodeID("someone"), &dbsync.MetadataResponse{
   106  		Height:      1,
   107  		Hash:        []byte("hash"),
   108  		Filenames:   []string{"f1"},
   109  		Md5Checksum: [][]byte{sum[:]},
   110  	})
   111  	for {
   112  		syncer.mtx.RLock()
   113  		_, ok := syncer.pendingFiles["f1"]
   114  		syncer.mtx.RUnlock()
   115  		if ok {
   116  			break
   117  		}
   118  	}
   119  	time.Sleep(syncer.fileWorkerTimeout + time.Second) // add some padding
   120  	syncer.PushFile(&dbsync.FileResponse{
   121  		Height:   1,
   122  		Filename: "f1",
   123  		Data:     data,
   124  	})
   125  	syncer.Process(context.Background())
   126  }