connchecker: initialCheck() now also respects CheckInterval (#719)

This commit is contained in:
Yordan Miladinov
2021-07-26 05:31:47 +03:00
committed by GitHub
parent cb21caccd0
commit 44ae89542f
2 changed files with 11 additions and 5 deletions

View File

@@ -1,6 +1,7 @@
package connchecker
import (
"context"
"net"
"strings"
"sync"
@@ -169,13 +170,17 @@ func (c *Checker) connectionTest() {
// CheckDNS checks current dns for connectivity
func (c *Checker) CheckDNS(dns string) error {
_, err := net.LookupAddr(dns)
ctx, cancel := context.WithTimeout(context.Background(), c.CheckInterval)
defer cancel()
_, err := net.DefaultResolver.LookupAddr(ctx, dns)
return err
}
// CheckHost checks current host name for connectivity
func (c *Checker) CheckHost(host string) error {
_, err := net.LookupHost(host)
ctx, cancel := context.WithTimeout(context.Background(), c.CheckInterval)
defer cancel()
_, err := net.DefaultResolver.LookupHost(ctx, host)
return err
}

View File

@@ -2,22 +2,23 @@ package connchecker
import (
"testing"
"time"
)
func TestConnection(t *testing.T) {
faultyDomain := []string{"faultyIP"}
faultyHost := []string{"faultyHost"}
_, err := New(faultyDomain, nil, 100000)
_, err := New(faultyDomain, nil, 1*time.Second)
if err == nil {
t.Fatal("New error cannot be nil")
}
_, err = New(DefaultDNSList, nil, 100000)
_, err = New(DefaultDNSList, nil, 1*time.Second)
if err != nil {
t.Fatal("New error", err)
}
_, err = New(nil, faultyHost, 100000)
_, err = New(nil, faultyHost, 1*time.Second)
if err != nil {
t.Fatal("New error cannot be nil", err)
}