wgengine/magicsock: minor tidies in Test_endpoint_maybeProbeUDPLifetimeLocked
* Remove a couple of single-letter `l` variables * Use named struct parameters in the test cases for readability * Delete `wantAfterInactivityForFn` parameter when it returns the default zero Updates #cleanup Signed-off-by: Alex Chan <alexc@tailscale.com>
This commit is contained in:
@@ -173,130 +173,110 @@ func Test_endpoint_maybeProbeUDPLifetimeLocked(t *testing.T) {
|
|||||||
wantMaybe bool
|
wantMaybe bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
"nil probeUDPLifetime",
|
name: "nil probeUDPLifetime",
|
||||||
higher,
|
localDisco: higher,
|
||||||
&lower,
|
remoteDisco: &lower,
|
||||||
func() *probeUDPLifetime {
|
probeUDPLifetimeFn: func() *probeUDPLifetime {
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
|
||||||
return 0
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"local higher disco key",
|
name: "local higher disco key",
|
||||||
higher,
|
localDisco: higher,
|
||||||
&lower,
|
remoteDisco: &lower,
|
||||||
newProbeUDPLifetime,
|
probeUDPLifetimeFn: newProbeUDPLifetime,
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
|
||||||
return 0
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"remote no disco key",
|
name: "remote no disco key",
|
||||||
higher,
|
localDisco: higher,
|
||||||
nil,
|
remoteDisco: nil,
|
||||||
newProbeUDPLifetime,
|
probeUDPLifetimeFn: newProbeUDPLifetime,
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
|
||||||
return 0
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"invalid bestAddr",
|
name: "invalid bestAddr",
|
||||||
lower,
|
localDisco: lower,
|
||||||
&higher,
|
remoteDisco: &higher,
|
||||||
newProbeUDPLifetime,
|
probeUDPLifetimeFn: newProbeUDPLifetime,
|
||||||
addrQuality{},
|
bestAddr: addrQuality{},
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
|
||||||
return 0
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cycle started too recently",
|
name: "cycle started too recently",
|
||||||
lower,
|
localDisco: lower,
|
||||||
&higher,
|
remoteDisco: &higher,
|
||||||
func() *probeUDPLifetime {
|
probeUDPLifetimeFn: func() *probeUDPLifetime {
|
||||||
l := newProbeUDPLifetime()
|
lt := newProbeUDPLifetime()
|
||||||
l.cycleActive = false
|
lt.cycleActive = false
|
||||||
l.cycleStartedAt = time.Now()
|
lt.cycleStartedAt = time.Now()
|
||||||
return l
|
return lt
|
||||||
},
|
},
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
|
||||||
return 0
|
|
||||||
},
|
|
||||||
false,
|
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"maybe cliff 0 cycle not active",
|
name: "maybe cliff 0 cycle not active",
|
||||||
lower,
|
localDisco: lower,
|
||||||
&higher,
|
remoteDisco: &higher,
|
||||||
func() *probeUDPLifetime {
|
probeUDPLifetimeFn: func() *probeUDPLifetime {
|
||||||
l := newProbeUDPLifetime()
|
lt := newProbeUDPLifetime()
|
||||||
l.cycleActive = false
|
lt.cycleActive = false
|
||||||
l.cycleStartedAt = time.Now().Add(-l.config.CycleCanStartEvery).Add(-time.Second)
|
lt.cycleStartedAt = time.Now().Add(-lt.config.CycleCanStartEvery).Add(-time.Second)
|
||||||
return l
|
return lt
|
||||||
},
|
},
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
wantAfterInactivityForFn: func(lifetime *probeUDPLifetime) time.Duration {
|
||||||
return lifetime.config.Cliffs[0] - udpLifetimeProbeCliffSlack
|
return lifetime.config.Cliffs[0] - udpLifetimeProbeCliffSlack
|
||||||
},
|
},
|
||||||
true,
|
wantMaybe: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"maybe cliff 0",
|
name: "maybe cliff 0",
|
||||||
lower,
|
localDisco: lower,
|
||||||
&higher,
|
remoteDisco: &higher,
|
||||||
func() *probeUDPLifetime {
|
probeUDPLifetimeFn: func() *probeUDPLifetime {
|
||||||
l := newProbeUDPLifetime()
|
lt := newProbeUDPLifetime()
|
||||||
l.cycleActive = true
|
lt.cycleActive = true
|
||||||
l.currentCliff = 0
|
lt.currentCliff = 0
|
||||||
return l
|
return lt
|
||||||
},
|
},
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
wantAfterInactivityForFn: func(lifetime *probeUDPLifetime) time.Duration {
|
||||||
return lifetime.config.Cliffs[0] - udpLifetimeProbeCliffSlack
|
return lifetime.config.Cliffs[0] - udpLifetimeProbeCliffSlack
|
||||||
},
|
},
|
||||||
true,
|
wantMaybe: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"maybe cliff 1",
|
name: "maybe cliff 1",
|
||||||
lower,
|
localDisco: lower,
|
||||||
&higher,
|
remoteDisco: &higher,
|
||||||
func() *probeUDPLifetime {
|
probeUDPLifetimeFn: func() *probeUDPLifetime {
|
||||||
l := newProbeUDPLifetime()
|
lt := newProbeUDPLifetime()
|
||||||
l.cycleActive = true
|
lt.cycleActive = true
|
||||||
l.currentCliff = 1
|
lt.currentCliff = 1
|
||||||
return l
|
return lt
|
||||||
},
|
},
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
wantAfterInactivityForFn: func(lifetime *probeUDPLifetime) time.Duration {
|
||||||
return lifetime.config.Cliffs[1] - udpLifetimeProbeCliffSlack
|
return lifetime.config.Cliffs[1] - udpLifetimeProbeCliffSlack
|
||||||
},
|
},
|
||||||
true,
|
wantMaybe: true,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"maybe cliff 2",
|
name: "maybe cliff 2",
|
||||||
lower,
|
localDisco: lower,
|
||||||
&higher,
|
remoteDisco: &higher,
|
||||||
func() *probeUDPLifetime {
|
probeUDPLifetimeFn: func() *probeUDPLifetime {
|
||||||
l := newProbeUDPLifetime()
|
lt := newProbeUDPLifetime()
|
||||||
l.cycleActive = true
|
lt.cycleActive = true
|
||||||
l.currentCliff = 2
|
lt.currentCliff = 2
|
||||||
return l
|
return lt
|
||||||
},
|
},
|
||||||
addr,
|
bestAddr: addr,
|
||||||
func(lifetime *probeUDPLifetime) time.Duration {
|
wantAfterInactivityForFn: func(lifetime *probeUDPLifetime) time.Duration {
|
||||||
return lifetime.config.Cliffs[2] - udpLifetimeProbeCliffSlack
|
return lifetime.config.Cliffs[2] - udpLifetimeProbeCliffSlack
|
||||||
},
|
},
|
||||||
true,
|
wantMaybe: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@@ -316,7 +296,10 @@ func Test_endpoint_maybeProbeUDPLifetimeLocked(t *testing.T) {
|
|||||||
p := tt.probeUDPLifetimeFn()
|
p := tt.probeUDPLifetimeFn()
|
||||||
de.probeUDPLifetime = p
|
de.probeUDPLifetime = p
|
||||||
gotAfterInactivityFor, gotMaybe := de.maybeProbeUDPLifetimeLocked()
|
gotAfterInactivityFor, gotMaybe := de.maybeProbeUDPLifetimeLocked()
|
||||||
wantAfterInactivityFor := tt.wantAfterInactivityForFn(p)
|
var wantAfterInactivityFor time.Duration
|
||||||
|
if tt.wantAfterInactivityForFn != nil {
|
||||||
|
wantAfterInactivityFor = tt.wantAfterInactivityForFn(p)
|
||||||
|
}
|
||||||
if gotAfterInactivityFor != wantAfterInactivityFor {
|
if gotAfterInactivityFor != wantAfterInactivityFor {
|
||||||
t.Errorf("maybeProbeUDPLifetimeLocked() gotAfterInactivityFor = %v, want %v", gotAfterInactivityFor, wantAfterInactivityFor)
|
t.Errorf("maybeProbeUDPLifetimeLocked() gotAfterInactivityFor = %v, want %v", gotAfterInactivityFor, wantAfterInactivityFor)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user