gitlab.com/SkynetLabs/skyd@v1.6.9/skymodules/accounting/accounting_test.go (about)

     1  package accounting
     2  
     3  import (
     4  	"fmt"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"gitlab.com/SkynetLabs/skyd/siatest/dependencies"
     9  	"gitlab.com/SkynetLabs/skyd/skymodules"
    10  	"go.sia.tech/siad/modules"
    11  )
    12  
    13  // TestAccounting tests the basic functionality of the accounting package
    14  func TestAccounting(t *testing.T) {
    15  	t.Run("AccountingRange", testAccountingRange)
    16  
    17  	if testing.Short() {
    18  		t.SkipNow()
    19  	}
    20  	t.Parallel()
    21  
    22  	// Specific Methods
    23  	t.Run("Accounting", testAccounting)
    24  	t.Run("NewCustomAccounting", testNewCustomAccounting)
    25  }
    26  
    27  // testAccounting probes the Accounting method
    28  func testAccounting(t *testing.T) {
    29  	// Create new accounting
    30  	testDir := accountingTestDir(t.Name())
    31  	h, m, r, w, _ := testingParams()
    32  	a, err := NewCustomAccounting(h, m, r, w, testDir, &dependencies.AccountingDisablePersistLoop{})
    33  	if err != nil {
    34  		t.Fatal(err)
    35  	}
    36  	defer func() {
    37  		err = a.Close()
    38  		if err != nil {
    39  			t.Fatal(err)
    40  		}
    41  	}()
    42  
    43  	// Initial persistence should be empty
    44  	a.mu.Lock()
    45  	lenHistroy := len(a.history)
    46  	a.mu.Unlock()
    47  	if lenHistroy != 0 {
    48  		t.Error("history should be empty")
    49  	}
    50  
    51  	// Check accounting
    52  	ais, err := a.Accounting(0, DefaultEndRangeTime)
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  	ai := ais[0]
    57  
    58  	// Check renter explicitly
    59  	if reflect.DeepEqual(ai.Renter, skymodules.RenterAccounting{}) {
    60  		t.Error("renter accounting information is empty")
    61  	}
    62  	// Check wallet explicitly
    63  	if reflect.DeepEqual(ai.Wallet, skymodules.WalletAccounting{}) {
    64  		t.Error("wallet accounting information is empty")
    65  	}
    66  	// Check timestamp explicitly
    67  	if ai.Timestamp == 0 {
    68  		t.Error("timestamp not set")
    69  	}
    70  
    71  	// The history should still be empty as a call to Accounting does not persist
    72  	// the update to disk.
    73  	a.mu.Lock()
    74  	lenHistroy = len(a.history)
    75  	a.mu.Unlock()
    76  	if lenHistroy != 0 {
    77  		t.Error("history should be empty")
    78  	}
    79  }
    80  
    81  // testAccountingRange probes the accountingRange function
    82  func testAccountingRange(t *testing.T) {
    83  	// Create a history
    84  	var first, mid, last int64 = 2, 4, 10
    85  	history := []skymodules.AccountingInfo{
    86  		{Timestamp: first},
    87  		{Timestamp: 3},
    88  		{Timestamp: mid},
    89  		{Timestamp: mid},
    90  		{Timestamp: mid},
    91  		{Timestamp: last},
    92  	}
    93  	all := len(history)
    94  
    95  	// Create range tests
    96  	before := first - 1
    97  	after := last + 1
    98  	var rangeTests = []struct {
    99  		start      int64
   100  		end        int64
   101  		numEntries int
   102  	}{
   103  		// Conditions that should return all
   104  		{0, after, all},
   105  		{0, DefaultEndRangeTime, all},
   106  		{0, last, all},
   107  		{before, after, all},
   108  		{before, last, all},
   109  		{first, last, all},
   110  		{first, after, all},
   111  
   112  		// Conditions that should return none
   113  		{0, 0, 0},
   114  		{0, before, 0},
   115  		{before, before, 0},
   116  		{mid + 1, last - 1, 0},
   117  		{after, after, 0},
   118  
   119  		// Conditions that should return a set amount
   120  		{0, first, 1},
   121  		{first, first, 1},
   122  		{before, mid, 5},
   123  		{first, mid, 5},
   124  		{mid, mid, 3},
   125  		{mid, last, 4},
   126  		{mid, after, 4},
   127  		{last, last, 1},
   128  	}
   129  
   130  	// Run range tests
   131  	for _, rt := range rangeTests {
   132  		// Grab accounting information range
   133  		ais := accountingRange(history, rt.start, rt.end)
   134  		if len(ais) != rt.numEntries {
   135  			test := fmt.Sprintf("Testing start %v, end %v, expected %v", rt.start, rt.end, rt.numEntries)
   136  			t.Log(test)
   137  			t.Errorf("expected %v got %v", rt.numEntries, len(ais))
   138  		}
   139  	}
   140  
   141  	// Test build.Critical for end < start
   142  	defer func() {
   143  		if r := recover(); r == nil {
   144  			t.Fatal("expected build.critical")
   145  		}
   146  	}()
   147  	accountingRange(nil, 1, 0)
   148  }
   149  
   150  // testNewCustomAccounting probes the NewCustomAccounting function
   151  func testNewCustomAccounting(t *testing.T) {
   152  	// checkNew is a helper function to check NewCustomAccounting
   153  	checkNew := func(h modules.Host, m modules.Miner, r skymodules.Renter, w modules.Wallet, dir string, deps modules.Dependencies, expectedErr error) {
   154  		a, err := NewCustomAccounting(h, m, r, w, dir, deps)
   155  		if err != expectedErr {
   156  			t.Errorf("Expected %v, got %v", expectedErr, err)
   157  		}
   158  		if a == nil {
   159  			return
   160  		}
   161  		err = a.Close()
   162  		if err != nil {
   163  			t.Error(err)
   164  		}
   165  	}
   166  
   167  	// Create testing parameters
   168  	testDir := accountingTestDir(t.Name())
   169  	h, m, r, w, deps := testingParams()
   170  
   171  	// Base Case
   172  	checkNew(nil, nil, nil, w, testDir, deps, nil)
   173  
   174  	// Check for nil wallet
   175  	checkNew(nil, nil, nil, nil, testDir, deps, errNilWallet)
   176  
   177  	// Check for blank persistDir
   178  	checkNew(nil, nil, nil, w, "", deps, errNilPersistDir)
   179  
   180  	// Check for nil Dependencies
   181  	checkNew(nil, nil, nil, w, testDir, nil, errNilDeps)
   182  
   183  	// Test optional modules
   184  	//
   185  	// Host
   186  	checkNew(h, nil, nil, w, testDir, deps, nil)
   187  	// Miner
   188  	checkNew(nil, m, nil, w, testDir, deps, nil)
   189  	// Renter
   190  	checkNew(nil, nil, r, w, testDir, deps, nil)
   191  }