Skip to content

Commit

Permalink
refactor options to be structs + interface
Browse files Browse the repository at this point in the history
  • Loading branch information
MKrupauskas authored and jeffbean committed Jul 24, 2024
1 parent e8570cf commit d4d09a5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
21 changes: 17 additions & 4 deletions dnshostprovider.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,29 @@ import (

const _defaultLookupTimeout = 3 * time.Second

type lookupHostFn func(context.Context, string) ([]string, error)

// DNSHostProviderOption is an option for the DNSHostProvider.
type DNSHostProviderOption func (*DNSHostProvider)
type DNSHostProviderOption interface {
apply(*DNSHostProvider)
}

type lookupTimeoutOption struct {
timeout time.Duration
}

// WithLookupTimeout returns a DNSHostProviderOption that sets the lookup timeout.
func WithLookupTimeout(timeout time.Duration) DNSHostProviderOption {
return func(provider *DNSHostProvider) {
provider.lookupTimeout = timeout
return lookupTimeoutOption{
timeout: timeout,
}
}

func (o lookupTimeoutOption) apply(provider *DNSHostProvider) {
provider.lookupTimeout = o.timeout
}


// DNSHostProvider is the default HostProvider. It currently matches
// the Java StaticHostProvider, resolving hosts from DNS once during
// the call to Init. It could be easily extended to re-query DNS
Expand All @@ -30,7 +43,7 @@ type DNSHostProvider struct {
curr int
last int
lookupTimeout time.Duration
lookupHost func(context.Context, string) ([]string, error) // Override of net.LookupHost, for testing.
lookupHost lookupHostFn // Override of net.LookupHost, for testing.
}

// NewDNSHostProvider creates a new DNSHostProvider with the given options.
Expand Down
14 changes: 11 additions & 3 deletions dnshostprovider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@ import (
"time"
)

func withLookupHost(lookupFn func(context.Context, string) ([]string, error)) DNSHostProviderOption {
return func(provider *DNSHostProvider) {
provider.lookupHost = lookupFn
type lookupHostOption struct {
lookupFn lookupHostFn
}

func withLookupHost(lookupFn lookupHostFn) DNSHostProviderOption {
return lookupHostOption{
lookupFn: lookupFn,
}
}

func (o lookupHostOption) apply(provider *DNSHostProvider) {
provider.lookupHost = o.lookupFn
}

// TestDNSHostProviderCreate is just like TestCreate, but with an
// overridden HostProvider that ignores the provided hostname.
func TestIntegration_DNSHostProviderCreate(t *testing.T) {
Expand Down

0 comments on commit d4d09a5

Please sign in to comment.