-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
96 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package dns_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/9seconds/httransform/v2/cache" | ||
"github.com/9seconds/httransform/v2/dns" | ||
"github.com/stretchr/testify/suite" | ||
) | ||
|
||
type DNSTestSuite struct { | ||
suite.Suite | ||
|
||
cache dns.Interface | ||
} | ||
|
||
func (suite *DNSTestSuite) SetupTest() { | ||
suite.cache = dns.New(dns.CacheSize, dns.CacheTTL, cache.NoopEvictCallback) | ||
} | ||
|
||
func (suite *DNSTestSuite) TestResolveNames() { | ||
ctx := context.Background() | ||
names, err := suite.cache.Lookup(ctx, "google.com") | ||
|
||
suite.NoError(err) | ||
|
||
names2, err := suite.cache.Lookup(ctx, "google.com") | ||
|
||
suite.NoError(err) | ||
|
||
suite.NotEqual(names, names2) | ||
suite.ElementsMatch(names, names2) | ||
} | ||
|
||
func (suite *DNSTestSuite) TestResolveIP() { | ||
ctx := context.Background() | ||
names, err := suite.cache.Lookup(ctx, "64.233.165.102") | ||
|
||
suite.NoError(err) | ||
suite.NotEmpty(names) | ||
} | ||
|
||
func TestDNS(t *testing.T) { | ||
suite.Run(t, &DNSTestSuite{}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// Cachning DNS resolver. | ||
// | ||
// This package has a global instance of caching DNS service. | ||
package dns |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package dns | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/9seconds/httransform/v2/cache" | ||
) | ||
|
||
const ( | ||
// CacheSize is a size of DNS cache. | ||
CacheSize = 512 | ||
|
||
// CacheTTL is a TTL of a DNS record in a cache. | ||
CacheTTL = 5 * time.Minute | ||
) | ||
|
||
// Default is a default DNS resolver you usually want to use everywhere. | ||
var Default Interface = New(CacheSize, CacheTTL, cache.NoopEvictCallback) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package dns | ||
|
||
import "context" | ||
|
||
// Interface is an interface for cachind DNS resolver. | ||
type Interface interface { | ||
// Lookup returns resolved IPs for given hostname/ips. Important | ||
// property is that this list is shuffled on each function | ||
// execution. | ||
Lookup(context.Context, string) ([]string, error) | ||
} |