github.com/decred/dcrlnd@v0.7.6/routing/missioncontrol_state_test.go (about)

     1  package routing
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/decred/dcrlnd/routing/route"
     8  	"github.com/stretchr/testify/require"
     9  )
    10  
    11  // TestMissionControlStateSetLastPairResult tests setting mission control state
    12  // pair results.
    13  func TestMissionControlStateSetLastPairResult(t *testing.T) {
    14  	const minFailureRelaxInterval = time.Minute
    15  	state := newMissionControlState(minFailureRelaxInterval)
    16  
    17  	var (
    18  		from      = route.Vertex{1}
    19  		to        = route.Vertex{2}
    20  		timestamp = testTime
    21  	)
    22  
    23  	// Report a 1000 sat failure.
    24  	state.setLastPairResult(
    25  		from, to, timestamp, &pairResult{amt: 1000}, false,
    26  	)
    27  	result, _ := state.getLastPairResult(from)
    28  	if result[to].FailAmt != 1000 {
    29  		t.Fatalf("unexpected fail amount %v", result[to].FailAmt)
    30  	}
    31  
    32  	// Report an 1100 sat failure one hour later. It is expected to
    33  	// overwrite the previous failure.
    34  	timestamp = timestamp.Add(time.Hour)
    35  	state.setLastPairResult(
    36  		from, to, timestamp, &pairResult{amt: 1100}, false,
    37  	)
    38  	result, _ = state.getLastPairResult(from)
    39  	if result[to].FailAmt != 1100 {
    40  		t.Fatalf("unexpected fail amount %v", result[to].FailAmt)
    41  	}
    42  
    43  	// Report a 1200 sat failure one second later. Because this increase of
    44  	// the failure amount is too soon after the previous failure, the result
    45  	// is not applied.
    46  	timestamp = timestamp.Add(time.Second)
    47  	state.setLastPairResult(
    48  		from, to, timestamp, &pairResult{amt: 1200}, false,
    49  	)
    50  	result, _ = state.getLastPairResult(from)
    51  	if result[to].FailAmt != 1100 {
    52  		t.Fatalf("unexpected fail amount %v", result[to].FailAmt)
    53  	}
    54  
    55  	// Roll back time 1 second to test forced import.
    56  	timestamp = testTime
    57  	state.setLastPairResult(
    58  		from, to, timestamp, &pairResult{amt: 999}, true,
    59  	)
    60  	result, _ = state.getLastPairResult(from)
    61  	require.Equal(t, 999, int(result[to].FailAmt))
    62  
    63  	// Report an 1500 sat success.
    64  	timestamp = timestamp.Add(time.Second)
    65  	state.setLastPairResult(
    66  		from, to, timestamp, &pairResult{amt: 1500, success: true}, false,
    67  	)
    68  	result, _ = state.getLastPairResult(from)
    69  	// We don't expect the failtime to change, only the fail amount, we
    70  	// expect however change of both the success time and the success amount.
    71  	expected := TimedPairResult{
    72  		FailTime:    timestamp.Add(-time.Second),
    73  		FailAmt:     1501,
    74  		SuccessTime: timestamp,
    75  		SuccessAmt:  1500,
    76  	}
    77  	require.Equal(t, expected, result[to])
    78  
    79  	// Again roll back time to test forced import.
    80  	state.setLastPairResult(
    81  		from, to, testTime, &pairResult{amt: 50, success: true}, true,
    82  	)
    83  	result, _ = state.getLastPairResult(from)
    84  	// We don't expect the failtime to change, only the fail amount, we
    85  	// expect however change of both the success time and the success amount.
    86  	expected = TimedPairResult{
    87  		FailTime:    timestamp.Add(-time.Second),
    88  		FailAmt:     51,
    89  		SuccessTime: testTime,
    90  		SuccessAmt:  50,
    91  	}
    92  	require.Equal(t, expected, result[to])
    93  
    94  }