Skip to content

Commit

Permalink
Added test and minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
myleshorton committed Nov 20, 2024
1 parent f6be105 commit ff73854
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 10 deletions.
20 changes: 10 additions & 10 deletions fronted.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,6 @@ dialLoop:
break dialLoop
default:
// okay

}

m, err := f.masqueradeToTry(masqueradesToTry, triedMasquerades)
Expand All @@ -350,16 +349,17 @@ dialLoop:
break dialLoop
}
conn, masqueradeGood, err := f.dialMasquerade(m)
if err == nil {
return conn, m, masqueradeGood, nil
}

// As we're looping through the masquerades, each check takes time. As that's happening,
// other goroutines may be successfully vetting new masquerades, which will change the
// sorting. We want to make sure we're always trying the best masquerades first.
masqueradesToTry = f.masquerades.sortedCopy()
totalMasquerades = len(masqueradesToTry)
triedMasquerades[m] = true
if err != nil {
log.Debugf("Could not dial to %v: %v", m, err)
// As we're looping through the masquerades, each check takes time. As that's happening,
// other goroutines may be successfully vetting new masquerades, which will change the
// sorting. We want to make sure we're always trying the best masquerades first.
masqueradesToTry = f.masquerades.sortedCopy()
totalMasquerades = len(masqueradesToTry)
continue
}
return conn, m, masqueradeGood, nil
}

return nil, nil, nil, log.Errorf("could not dial any masquerade? tried %v", totalMasquerades)
Expand Down
68 changes: 68 additions & 0 deletions fronted_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package fronted

import (
"context"
"crypto/x509"
"encoding/json"
"errors"
Expand Down Expand Up @@ -962,6 +963,73 @@ func TestMasqueradeToTry(t *testing.T) {
}
}

func TestDialAll(t *testing.T) {
tests := []struct {
name string
masquerades []*mockMasquerade
expectedSuccessful bool
expectedMasquerades int
}{
{
name: "All successful",
masquerades: []*mockMasquerade{
newMockMasquerade("domain1.com", "1.1.1.1", 0, true),
newMockMasquerade("domain2.com", "2.2.2.2", 0, true),
newMockMasquerade("domain3.com", "3.3.3.3", 0, true),
newMockMasquerade("domain4.com", "4.4.4.4", 0, true),
},
expectedSuccessful: true,
},
{
name: "Some successful",
masquerades: []*mockMasquerade{
newMockMasquerade("domain1.com", "1.1.1.1", 0, true),
newMockMasquerade("domain2.com", "2.2.2.2", 1*time.Millisecond, false),
newMockMasquerade("domain3.com", "3.3.3.3", 0, true),
newMockMasquerade("domain4.com", "4.4.4.4", 1*time.Millisecond, false),
},
expectedSuccessful: true,
},
{
name: "None successful",
masquerades: []*mockMasquerade{
newMockMasquerade("domain1.com", "1.1.1.1", 1*time.Millisecond, false),
newMockMasquerade("domain2.com", "2.2.2.2", 1*time.Millisecond, false),
newMockMasquerade("domain3.com", "3.3.3.3", 1*time.Millisecond, false),
newMockMasquerade("domain4.com", "4.4.4.4", 1*time.Millisecond, false),
},
expectedSuccessful: false,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
d := &fronted{}
d.providers = make(map[string]*Provider)
d.providers["testProviderId"] = NewProvider(nil, "", nil, nil, nil, nil, nil)
d.masquerades = make(sortedMasquerades, len(tt.masquerades))
for i, m := range tt.masquerades {
d.masquerades[i] = m
}

ctx := context.Background()
conn, m, masqueradeGood, err := d.dialAll(ctx)

if tt.expectedSuccessful {
assert.NoError(t, err)
assert.NotNil(t, conn)
assert.NotNil(t, m)
assert.NotNil(t, masqueradeGood)
} else {
assert.Error(t, err)
assert.Nil(t, conn)
assert.Nil(t, m)
assert.Nil(t, masqueradeGood)
}
})
}
}

// Generate a mock of a MasqueradeInterface with a Dial method that can optionally
// return an error after a specified number of milliseconds.
func newMockMasquerade(domain string, ipAddress string, timeout time.Duration, passesCheck bool) *mockMasquerade {
Expand Down

0 comments on commit ff73854

Please sign in to comment.