github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/stellar/loader_test.go (about)

     1  package stellar
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/keybase/client/go/libkb"
     7  	"github.com/keybase/client/go/protocol/stellar1"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  func TestLoaderClean(t *testing.T) {
    12  	tc := libkb.SetupTest(t, "loaderclean", 1)
    13  	defer tc.Cleanup()
    14  
    15  	loader := NewLoader(tc.G)
    16  	require.Equal(t, 0, loader.PaymentsLen())
    17  	require.Equal(t, 0, loader.RequestsLen())
    18  
    19  	// clean of empty should work
    20  	n := loader.cleanPayments(50)
    21  	require.Equal(t, 0, n)
    22  	require.Equal(t, 0, loader.PaymentsLen())
    23  	n = loader.cleanRequests(40)
    24  	require.Equal(t, 0, n)
    25  	require.Equal(t, 0, loader.RequestsLen())
    26  
    27  	for i := 0; i < 10; i++ {
    28  		addRandomPayment(t, loader)
    29  		addRandomRequest(t, loader)
    30  	}
    31  
    32  	// clean below limit shouldn't do anything
    33  	n = loader.cleanPayments(50)
    34  	require.Equal(t, 0, n)
    35  	require.Equal(t, 10, loader.PaymentsLen())
    36  	n = loader.cleanRequests(40)
    37  	require.Equal(t, 0, n)
    38  	require.Equal(t, 10, loader.RequestsLen())
    39  
    40  	for i := 0; i < 100; i++ {
    41  		addRandomPayment(t, loader)
    42  		addRandomRequest(t, loader)
    43  	}
    44  
    45  	require.Equal(t, 110, loader.PaymentsLen())
    46  	require.Equal(t, 110, loader.RequestsLen())
    47  
    48  	// clean above limit should clean
    49  	n = loader.cleanPayments(50)
    50  	require.Equal(t, 60, n)
    51  	require.Equal(t, 50, loader.PaymentsLen())
    52  	n = loader.cleanRequests(40)
    53  	require.Equal(t, 70, n)
    54  	require.Equal(t, 40, loader.RequestsLen())
    55  
    56  	// make sure clean works more than one time:
    57  	for i := 0; i < 10; i++ {
    58  		addRandomPayment(t, loader)
    59  		addRandomRequest(t, loader)
    60  	}
    61  
    62  	require.Equal(t, 60, loader.PaymentsLen())
    63  	require.Equal(t, 50, loader.RequestsLen())
    64  
    65  	n = loader.cleanPayments(50)
    66  	require.Equal(t, 10, n)
    67  	require.Equal(t, 50, loader.PaymentsLen())
    68  
    69  	n = loader.cleanRequests(40)
    70  	require.Equal(t, 10, n)
    71  	require.Equal(t, 40, loader.RequestsLen())
    72  }
    73  
    74  func addRandomPayment(t *testing.T, loader *Loader) {
    75  	id, err := libkb.RandString("", 16)
    76  	if err != nil {
    77  		t.Fatal(err)
    78  	}
    79  	pid := stellar1.PaymentID(id)
    80  	loader.storePayment(pid, &stellar1.PaymentLocal{Id: pid})
    81  }
    82  
    83  func addRandomRequest(t *testing.T, loader *Loader) {
    84  	id, err := libkb.RandString("", 16)
    85  	if err != nil {
    86  		t.Fatal(err)
    87  	}
    88  	rid := stellar1.KeybaseRequestID(id)
    89  	loader.storeRequest(rid, &stellar1.RequestDetailsLocal{Id: rid})
    90  }