websvc: add tests; imp names

This commit is contained in:
Ainar Garipov
2022-08-31 19:11:00 +03:00
parent dbdae5b4fc
commit b018e150e7
8 changed files with 58 additions and 36 deletions

View File

@@ -17,23 +17,26 @@ import (
// Main is the entry point of application. // Main is the entry point of application.
func Main(clientBuildFS fs.FS) { func Main(clientBuildFS fs.FS) {
// # Initial Configuration // Initial Configuration
start := time.Now() start := time.Now()
rand.Seed(start.UnixNano()) rand.Seed(start.UnixNano())
// TODO(a.garipov): Set up logging. // TODO(a.garipov): Set up logging.
// # Web Service // Web Service
// TODO(a.garipov): Use in the Web service. // TODO(a.garipov): Use in the Web service.
_ = clientBuildFS _ = clientBuildFS
// TODO(a.garipov): Make configurable. // TODO(a.garipov): Make configurable.
web := websvc.New(&websvc.Config{ web := websvc.New(&websvc.Config{
Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")}, // TODO(a.garipov): Use an actual implementation.
Start: start, ConfigManager: nil,
Timeout: 60 * time.Second, Addresses: []netip.AddrPort{netip.MustParseAddrPort("127.0.0.1:3001")},
Start: start,
Timeout: 60 * time.Second,
ForceHTTPS: false,
}) })
err := web.Start() err := web.Start()

View File

@@ -25,8 +25,18 @@ type ReqPatchSettingsDNS struct {
UpstreamTimeout timeutil.Duration `json:"upstream_timeout"` UpstreamTimeout timeutil.Duration `json:"upstream_timeout"`
} }
// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/http // httpAPIDNSSettings are the DNS settings as used by the HTTP API.
// HTTP API. type httpAPIDNSSettings struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
BootstrapServers []string `json:"bootstrap_servers"`
UpstreamServers []string `json:"upstream_servers"`
UpstreamTimeout timeutil.Duration `json:"upstream_timeout"`
}
// handlePatchSettingsDNS is the handler for the PATCH /api/v1/settings/dns HTTP
// API.
func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Request) { func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Request) {
req := &ReqPatchSettingsDNS{ req := &ReqPatchSettingsDNS{
Addresses: []netip.AddrPort{}, Addresses: []netip.AddrPort{},
@@ -66,7 +76,7 @@ func (svc *Service) handlePatchSettingsDNS(w http.ResponseWriter, r *http.Reques
return return
} }
writeJSONResponse(w, r, &respGetV1SettingsAllDNS{ writeJSONResponse(w, r, &httpAPIDNSSettings{
Addresses: newConf.Addresses, Addresses: newConf.Addresses,
BootstrapServers: newConf.BootstrapServers, BootstrapServers: newConf.BootstrapServers,
UpstreamServers: newConf.UpstreamServers, UpstreamServers: newConf.UpstreamServers,

View File

@@ -26,6 +26,14 @@ type ReqPatchSettingsHTTP struct {
SecureAddresses []netip.AddrPort `json:"secure_addresses"` SecureAddresses []netip.AddrPort `json:"secure_addresses"`
} }
// httpAPIDNSSettings are the HTTP settings as used by the HTTP API.
type httpAPIHTTPSettings struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
}
// handlePatchSettingsHTTP is the handler for the PATCH /api/v1/settings/http // handlePatchSettingsHTTP is the handler for the PATCH /api/v1/settings/http
// HTTP API. // HTTP API.
func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Request) { func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Request) {
@@ -52,7 +60,7 @@ func (svc *Service) handlePatchSettingsHTTP(w http.ResponseWriter, r *http.Reque
ForceHTTPS: svc.forceHTTPS, ForceHTTPS: svc.forceHTTPS,
} }
writeJSONResponse(w, r, &respGetV1SettingsAllHTTP{ writeJSONResponse(w, r, &httpAPIHTTPSettings{
Addresses: newConf.Addresses, Addresses: newConf.Addresses,
SecureAddresses: newConf.SecureAddresses, SecureAddresses: newConf.SecureAddresses,
}) })

View File

@@ -2,7 +2,6 @@ package websvc
import ( import (
"net/http" "net/http"
"net/netip"
"github.com/AdguardTeam/golibs/timeutil" "github.com/AdguardTeam/golibs/timeutil"
) )
@@ -16,28 +15,8 @@ import (
type RespGetV1SettingsAll struct { type RespGetV1SettingsAll struct {
// TODO(a.garipov): Add more as we go. // TODO(a.garipov): Add more as we go.
DNS *respGetV1SettingsAllDNS `json:"dns"` DNS *httpAPIDNSSettings `json:"dns"`
HTTP *respGetV1SettingsAllHTTP `json:"http"` HTTP *httpAPIHTTPSettings `json:"http"`
}
// respGetV1SettingsAllDNS describes the DNS part of the response of the GET
// /api/v1/settings/all HTTP API.
type respGetV1SettingsAllDNS struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
BootstrapServers []string `json:"bootstrap_servers"`
UpstreamServers []string `json:"upstream_servers"`
UpstreamTimeout timeutil.Duration `json:"upstream_timeout"`
}
// respGetV1SettingsAllHTTP describes the HTTP part of the response of the GET
// /api/v1/settings/all HTTP API.
type respGetV1SettingsAllHTTP struct {
// TODO(a.garipov): Add more as we go.
Addresses []netip.AddrPort `json:"addresses"`
SecureAddresses []netip.AddrPort `json:"secure_addresses"`
} }
// handleGetSettingsAll is the handler for the GET /api/v1/settings/all HTTP // handleGetSettingsAll is the handler for the GET /api/v1/settings/all HTTP
@@ -49,13 +28,13 @@ func (svc *Service) handleGetSettingsAll(w http.ResponseWriter, r *http.Request)
httpConf := svc.Config() httpConf := svc.Config()
writeJSONResponse(w, r, &RespGetV1SettingsAll{ writeJSONResponse(w, r, &RespGetV1SettingsAll{
DNS: &respGetV1SettingsAllDNS{ DNS: &httpAPIDNSSettings{
Addresses: dnsConf.Addresses, Addresses: dnsConf.Addresses,
BootstrapServers: dnsConf.BootstrapServers, BootstrapServers: dnsConf.BootstrapServers,
UpstreamServers: dnsConf.UpstreamServers, UpstreamServers: dnsConf.UpstreamServers,
UpstreamTimeout: timeutil.Duration{Duration: dnsConf.UpstreamTimeout}, UpstreamTimeout: timeutil.Duration{Duration: dnsConf.UpstreamTimeout},
}, },
HTTP: &respGetV1SettingsAllHTTP{ HTTP: &httpAPIHTTPSettings{
Addresses: httpConf.Addresses, Addresses: httpConf.Addresses,
SecureAddresses: httpConf.SecureAddresses, SecureAddresses: httpConf.SecureAddresses,
}, },

View File

@@ -1,12 +1,15 @@
package websvc package websvc
import ( import (
"fmt"
"net" "net"
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
"time"
"github.com/AdguardTeam/AdGuardHome/internal/aghtest" "github.com/AdguardTeam/AdGuardHome/internal/aghtest"
"github.com/stretchr/testify/assert"
) )
func TestWaitListener_Accept(t *testing.T) { func TestWaitListener_Accept(t *testing.T) {
@@ -29,6 +32,17 @@ func TestWaitListener_Accept(t *testing.T) {
wg := &sync.WaitGroup{} wg := &sync.WaitGroup{}
wg.Add(1) wg.Add(1)
done := make(chan struct{})
a := time.After(testTimeout)
go func() {
select {
case <-a:
panic(fmt.Errorf("did not finish after %s", testTimeout))
case <-done:
// Success.
}
}()
go func() { go func() {
var wrapper net.Listener = &waitListener{ var wrapper net.Listener = &waitListener{
Listener: l, Listener: l,
@@ -39,4 +53,7 @@ func TestWaitListener_Accept(t *testing.T) {
}() }()
wg.Wait() wg.Wait()
close(done)
assert.Equal(t, uint32(1), atomic.LoadUint32(&numAcceptCalls))
} }

View File

@@ -22,8 +22,6 @@ import (
) )
// ConfigManager is the configuration manager interface. // ConfigManager is the configuration manager interface.
//
// See internal/v1/svc/ for the main implementation.
type ConfigManager interface { type ConfigManager interface {
DNS() (svc *dnssvc.Service) DNS() (svc *dnssvc.Service)
Web() (svc *Service) Web() (svc *Service)

View File

@@ -0,0 +1,6 @@
package websvc
import "time"
// testTimeout is the common timeout for tests.
const testTimeout = 1 * time.Second

View File

@@ -15,6 +15,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
// testTimeout is the common timeout for tests.
const testTimeout = 1 * time.Second const testTimeout = 1 * time.Second
// testStart is the server start value for tests. // testStart is the server start value for tests.