github.com/ava-labs/avalanchego@v1.11.11/genesis/config_test.go (about)

     1  // Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
     2  // See the file LICENSE for licensing terms.
     3  
     4  package genesis
     5  
     6  import (
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/stretchr/testify/require"
    11  
    12  	"github.com/ava-labs/avalanchego/ids"
    13  )
    14  
    15  func TestAllocationCompare(t *testing.T) {
    16  	type test struct {
    17  		name     string
    18  		alloc1   Allocation
    19  		alloc2   Allocation
    20  		expected int
    21  	}
    22  	tests := []test{
    23  		{
    24  			name:     "equal",
    25  			alloc1:   Allocation{},
    26  			alloc2:   Allocation{},
    27  			expected: 0,
    28  		},
    29  		{
    30  			name:   "initial amount smaller",
    31  			alloc1: Allocation{},
    32  			alloc2: Allocation{
    33  				InitialAmount: 1,
    34  			},
    35  			expected: -1,
    36  		},
    37  		{
    38  			name:   "bytes smaller",
    39  			alloc1: Allocation{},
    40  			alloc2: Allocation{
    41  				AVAXAddr: ids.ShortID{1},
    42  			},
    43  			expected: -1,
    44  		},
    45  	}
    46  	for _, tt := range tests {
    47  		t.Run(tt.name, func(t *testing.T) {
    48  			require := require.New(t)
    49  
    50  			require.Equal(tt.expected, tt.alloc1.Compare(tt.alloc2))
    51  			require.Equal(-tt.expected, tt.alloc2.Compare(tt.alloc1))
    52  		})
    53  	}
    54  }
    55  
    56  func TestGetRecentStartTime(t *testing.T) {
    57  	type test struct {
    58  		name     string
    59  		defined  time.Time
    60  		now      time.Time
    61  		expected time.Time
    62  	}
    63  	tests := []test{
    64  		{
    65  			name:     "before 1 period and 1 second",
    66  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    67  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(-localNetworkUpdateStartTimePeriod - time.Second),
    68  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    69  		},
    70  		{
    71  			name:     "before 1 second",
    72  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    73  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(-time.Second),
    74  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    75  		},
    76  		{
    77  			name:     "equal",
    78  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    79  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    80  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    81  		},
    82  		{
    83  			name:     "after 1 second",
    84  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    85  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(time.Second),
    86  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    87  		},
    88  		{
    89  			name:     "after 1 period",
    90  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    91  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
    92  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
    93  		},
    94  		{
    95  			name:     "after 1 period and 1 second",
    96  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
    97  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod + time.Second),
    98  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(localNetworkUpdateStartTimePeriod),
    99  		},
   100  		{
   101  			name:     "after 2 periods",
   102  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
   103  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
   104  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
   105  		},
   106  		{
   107  			name:     "after 2 periods and 1 second",
   108  			defined:  time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC),
   109  			now:      time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2*localNetworkUpdateStartTimePeriod + time.Second),
   110  			expected: time.Date(2024, time.July, 15, 4, 0, 0, 0, time.UTC).Add(2 * localNetworkUpdateStartTimePeriod),
   111  		},
   112  	}
   113  	for _, tt := range tests {
   114  		t.Run(tt.name, func(t *testing.T) {
   115  			actual := getRecentStartTime(tt.defined, tt.now, localNetworkUpdateStartTimePeriod)
   116  			require.Equal(t, tt.expected, actual)
   117  		})
   118  	}
   119  }