github.com/ethersphere/bee/v2@v2.2.0/pkg/transaction/backend_test.go (about)

     1  // Copyright 2021 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package transaction_test
     6  
     7  import (
     8  	"context"
     9  	"errors"
    10  	"math/big"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/ethereum/go-ethereum/core/types"
    15  	"github.com/ethersphere/bee/v2/pkg/transaction"
    16  	"github.com/ethersphere/bee/v2/pkg/transaction/backendmock"
    17  )
    18  
    19  func TestIsSynced(t *testing.T) {
    20  	t.Parallel()
    21  
    22  	maxDelay := 10 * time.Second
    23  	now := time.Now().UTC()
    24  	ctx := context.Background()
    25  	blockNumber := uint64(100)
    26  
    27  	t.Run("synced", func(t *testing.T) {
    28  		t.Parallel()
    29  
    30  		synced, _, err := transaction.IsSynced(
    31  			ctx,
    32  			backendmock.New(
    33  				backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
    34  					return blockNumber, nil
    35  				}),
    36  				backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
    37  					if number.Uint64() != blockNumber {
    38  						return nil, errors.New("called with wrong block number")
    39  					}
    40  					return &types.Header{
    41  						Time: uint64(now.Unix()),
    42  					}, nil
    43  				}),
    44  			),
    45  			maxDelay,
    46  		)
    47  		if err != nil {
    48  			t.Fatal(err)
    49  		}
    50  		if !synced {
    51  			t.Fatal("expected synced")
    52  		}
    53  	})
    54  
    55  	t.Run("not synced", func(t *testing.T) {
    56  		t.Parallel()
    57  
    58  		synced, _, err := transaction.IsSynced(
    59  			ctx,
    60  			backendmock.New(
    61  				backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
    62  					return blockNumber, nil
    63  				}),
    64  				backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
    65  					if number.Uint64() != blockNumber {
    66  						return nil, errors.New("called with wrong block number")
    67  					}
    68  					return &types.Header{
    69  						Time: uint64(now.Add(-maxDelay).Unix()),
    70  					}, nil
    71  				}),
    72  			),
    73  			maxDelay,
    74  		)
    75  		if err != nil {
    76  			t.Fatal(err)
    77  		}
    78  		if synced {
    79  			t.Fatal("expected not synced")
    80  		}
    81  	})
    82  
    83  	t.Run("error", func(t *testing.T) {
    84  		t.Parallel()
    85  
    86  		expectedErr := errors.New("err")
    87  		_, _, err := transaction.IsSynced(
    88  			ctx,
    89  			backendmock.New(
    90  				backendmock.WithBlockNumberFunc(func(c context.Context) (uint64, error) {
    91  					return blockNumber, nil
    92  				}),
    93  				backendmock.WithHeaderbyNumberFunc(func(ctx context.Context, number *big.Int) (*types.Header, error) {
    94  					if number.Uint64() != blockNumber {
    95  						return nil, errors.New("called with wrong block number")
    96  					}
    97  					return nil, expectedErr
    98  				}),
    99  			),
   100  			maxDelay,
   101  		)
   102  		if !errors.Is(err, expectedErr) {
   103  			t.Fatalf("expected error. wanted %v, got %v", expectedErr, err)
   104  		}
   105  	})
   106  }