gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/renter/contractor/churnlimiter_test.go (about)

     1  package contractor
     2  
     3  import (
     4  	"testing"
     5  
     6  	"gitlab.com/SkynetLabs/skyd/skymodules"
     7  	"go.sia.tech/siad/types"
     8  )
     9  
    10  // contractWithSize is a helper function that creates a dummy file contract with a certain size.
    11  func contractWithSize(size uint64) skymodules.RenterContract {
    12  	txn := types.Transaction{
    13  		FileContractRevisions: []types.FileContractRevision{{NewFileSize: size}},
    14  	}
    15  	return skymodules.RenterContract{Transaction: txn}
    16  }
    17  
    18  // TestCanChurnContract tests the functionality of managedCanChurnContract
    19  func TestCanChurnContract(t *testing.T) {
    20  	// Use a dummy Contractor.
    21  	allowance := skymodules.DefaultAllowance
    22  	allowance.MaxPeriodChurn = 1000
    23  	cl := newChurnLimiter(&Contractor{
    24  		allowance: allowance,
    25  	})
    26  
    27  	// Test: Not enough remainingChurnBudget
    28  	cl.remainingChurnBudget = 499
    29  	cl.aggregateCurrentPeriodChurn = 0
    30  	ok := cl.managedCanChurnContract(contractWithSize(500))
    31  	if ok {
    32  		t.Fatal("Expected not to be able to churn contract")
    33  	}
    34  
    35  	// Test: just enough remainingChurnBudget.
    36  	cl.remainingChurnBudget = 500
    37  	cl.aggregateCurrentPeriodChurn = 0
    38  	ok = cl.managedCanChurnContract(contractWithSize(500))
    39  	if !ok {
    40  		t.Fatal("Expected to be able to churn contract")
    41  	}
    42  
    43  	// Test: not enough period budget.
    44  	cl.remainingChurnBudget = 500
    45  	cl.aggregateCurrentPeriodChurn = 501
    46  	ok = cl.managedCanChurnContract(contractWithSize(500))
    47  	if ok {
    48  		t.Fatal("Expected not to be able to churn contract")
    49  	}
    50  
    51  	// Test: just enough period budget.
    52  	cl.remainingChurnBudget = 500
    53  	cl.aggregateCurrentPeriodChurn = 500
    54  	ok = cl.managedCanChurnContract(contractWithSize(500))
    55  	if !ok {
    56  		t.Fatal("Expected to be able to churn contract")
    57  	}
    58  
    59  	// Test: can churn contract bigger than remainingChurnBudget as long as
    60  	// remainingChurnBudget is max and churn fits in period budget.
    61  	cl.remainingChurnBudget = 500
    62  	cl.aggregateCurrentPeriodChurn = 1
    63  	ok = cl.managedCanChurnContract(contractWithSize(999))
    64  	if !ok {
    65  		t.Fatal("Expected to be able to churn contract")
    66  	}
    67  
    68  	// Test: can churn any size contract if no aggregateChurn in period and max remainingChurnBudget.
    69  	cl.remainingChurnBudget = 500
    70  	cl.aggregateCurrentPeriodChurn = 0
    71  	ok = cl.managedCanChurnContract(contractWithSize(999999999999999999))
    72  	if !ok {
    73  		t.Fatal("Expected to be able to churn contract")
    74  	}
    75  
    76  	// Test: avoid churning big contracts if any aggregate churn in the period was found
    77  	cl.remainingChurnBudget = 1000
    78  	cl.aggregateCurrentPeriodChurn = 1
    79  	ok = cl.managedCanChurnContract(contractWithSize(999999999999999999))
    80  	if ok {
    81  		t.Fatal("Expected not to be able to churn contract")
    82  	}
    83  }