Skip to content

Commit

Permalink
Removed 'Interface' suffix (#28)
Browse files Browse the repository at this point in the history
* Removed 'Interface' suffix

* Synced
  • Loading branch information
krzysztof-gzocha authored May 10, 2019
1 parent d0d11aa commit 2d87cf0
Show file tree
Hide file tree
Showing 41 changed files with 222 additions and 229 deletions.
18 changes: 9 additions & 9 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,15 +62,15 @@ func main() {
}
}

func run(ctx context.Context, cfg config.Config, logger log.LoggerInterface) {
func run(ctx context.Context, cfg config.Config, logger log.Logger) {
// EventDispatcher with subscribers
eventDispatcher := event.NewDispatcher()
err := attachSubscribers(logger, eventDispatcher, cfg)
if err != nil {
logrus.Fatalf("Could not attach subscribers: %s", err.Error())
}

mainChecker := check.CheckerInterface(multiple.NewChecker(
mainChecker := check.Checker(multiple.NewChecker(
logger,
cfg.SingleCheckTimeout,
cfg.SuccessRateThreshold,
Expand All @@ -95,7 +95,7 @@ func run(ctx context.Context, cfg config.Config, logger log.LoggerInterface) {
checker.Check(ctx)
}

func attachSubscribers(logger log.LoggerInterface, dispatcher event.DispatcherInterface, cfg config.Config) error {
func attachSubscribers(logger log.Logger, dispatcher event.Dispatcher, cfg config.Config) error {
checkLogger := subscriber.NewChecksLogger(logger)
dispatcher.AttachSubscriber(periodic.ConnectionCheckEventName, checkLogger.LogConnectionCheckResult)

Expand Down Expand Up @@ -123,14 +123,14 @@ func attachSubscribers(logger log.LoggerInterface, dispatcher event.DispatcherIn
return nil
}

func getCheckers(logger log.LoggerInterface, cfg config.Config) []check.CheckerInterface {
checkers := make([]check.CheckerInterface, 0)
func getCheckers(logger log.Logger, cfg config.Config) []check.Checker {
checkers := make([]check.Checker, 0)

if len(cfg.Dns.Hosts) > 0 {
dnsClient := dns.Dns{}
dnsCheckers := make([]check.CheckerInterface, 0)
dnsCheckers := make([]check.Checker, 0)
for _, url := range cfg.Dns.Hosts {
var checker check.CheckerInterface = dns.NewChecker(logger, dnsClient, url)
var checker check.Checker = dns.NewChecker(logger, dnsClient, url)
if cfg.Metrics.Enabled {
checker = metric.NewInstrumentedDnsChecker(checker)
}
Expand All @@ -150,9 +150,9 @@ func getCheckers(logger log.LoggerInterface, cfg config.Config) []check.CheckerI
}

if len(cfg.Http.Urls) > 0 {
httpCheckers := make([]check.CheckerInterface, 0)
httpCheckers := make([]check.Checker, 0)
for _, url := range cfg.Http.Urls {
var checker check.CheckerInterface = httpCheck.NewChecker(logger, http.DefaultClient, url)
var checker check.Checker = httpCheck.NewChecker(logger, http.DefaultClient, url)
if cfg.Metrics.Enabled {
checker = metric.NewInstrumentedHttpChecker(checker)
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/check/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"github.com/krzysztof-gzocha/pingor/pkg/check/result"
)

// CheckerInterface should be implemented by anything that is able to check current internet connection and return a result
type CheckerInterface interface {
Check(ctx context.Context) result.ResultInterface
// Checker should be implemented by anything that is able to check current internet connection and return a result
type Checker interface {
Check(ctx context.Context) result.Result
}
10 changes: 5 additions & 5 deletions pkg/check/dns/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,20 @@ import (

// Checker will try to resolve provided hosts into IPs in order to check the connection to DNS
type Checker struct {
logger log.LoggerInterface
dns ResolverInterface
logger log.Logger
dns Resolver
host string
}

// NewChecker will return new instance of Checker
func NewChecker(logger log.LoggerInterface, dns ResolverInterface, host string) Checker {
func NewChecker(logger log.Logger, dns Resolver, host string) Checker {
return Checker{logger: logger, dns: dns, host: host}
}

// Check will try to resolve provided hosts into IPs in order to check the connection to DNS.
// Time result is average time required to resolve all the hosts.
func (c Checker) Check(ctx context.Context) result.ResultInterface {
res := result.Result{URL: c.host, Success: true}
func (c Checker) Check(ctx context.Context) result.Result {
res := result.DefaultResult{URL: c.host, Success: true}
c.logger.
WithField("host", c.host).
Debugf("%T: starting to check", c)
Expand Down
4 changes: 2 additions & 2 deletions pkg/check/dns/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"time"
)

// ResolverInterface should be implemented by any service capable of resolving host into IP
type ResolverInterface interface {
// Resolver should be implemented by any service capable of resolving host into IP
type Resolver interface {
ResolveHost(host string) (Result, error)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/check/dns/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

func TestDns_ResolveHost(t *testing.T) {
d := Dns{}
assert.Implements(t, (*ResolverInterface)(nil), d)
assert.Implements(t, (*Resolver)(nil), d)

result, err := d.ResolveHost("google.com")
assert.Nil(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/check/formatter/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ package formatter
import "github.com/krzysztof-gzocha/pingor/pkg/check/result"

// Func is function declaration that is capable of printing the result
type Func func(result result.ResultInterface) (string, error)
type Func func(result result.Result) (string, error)
2 changes: 1 addition & 1 deletion pkg/check/formatter/json/formatter.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

// Formatter will encode result into JSON and return it as a string
func Formatter(result result.ResultInterface) (string, error) {
func Formatter(result result.Result) (string, error) {
m, err := json.Marshal(result)
if err != nil {
return "", err
Expand Down
4 changes: 2 additions & 2 deletions pkg/check/formatter/json/formatter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ func (r invalidResult) GetMessage() string {
func (r invalidResult) GetURL() string {
return ""
}
func (r invalidResult) GetSubResults() []result.ResultInterface {
return make([]result.ResultInterface, 0)
func (r invalidResult) GetSubResults() []result.Result {
return make([]result.Result, 0)
}
10 changes: 5 additions & 5 deletions pkg/check/http/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@ import (

// Checker will make HTTP request to provided URLs and will return positive result if HTTP status will be 200 OK
type Checker struct {
logger log.LoggerInterface
httpClient pkgHttp.ClientInterface
logger log.Logger
httpClient pkgHttp.Client
url string
}

// NewChecker will return new instance of Checker
func NewChecker(logger log.LoggerInterface, httpClient pkgHttp.ClientInterface, url string) Checker {
func NewChecker(logger log.Logger, httpClient pkgHttp.Client, url string) Checker {
return Checker{logger: logger, httpClient: httpClient, url: url}
}

// Check will send HTTP request to all provided URLs and check HTTP statuses of the response.
// Status code have to be "200" to be recognized as success.
func (c Checker) Check(ctx context.Context) result.ResultInterface {
func (c Checker) Check(ctx context.Context) result.Result {
c.logger.WithField("url", c.url).Debugf("Starting to check for HTTP status")

res := result.Result{Success: true, URL: c.url}
res := result.DefaultResult{Success: true, URL: c.url}
start := time.Now()
resp, err := c.httpClient.Get(c.url)
diff := time.Now().Sub(start)
Expand Down
18 changes: 9 additions & 9 deletions pkg/check/multiple/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@ import (
)

// Checker is able to aggregate multiple checkers and treat them as single checker.
// Result will be combination of all the results from individual checkers
// DefaultResult will be combination of all the results from individual checkers
type Checker struct {
logger log.LoggerInterface
checkers []check.CheckerInterface
logger log.Logger
checkers []check.Checker
successRateThreshold float32
successTimeThreshold time.Duration
singleCheckTimeout time.Duration
}

// NewChecker will return instance of Checker
func NewChecker(
logger log.LoggerInterface,
logger log.Logger,
singleCheckTimeout time.Duration,
successRateThreshold float32,
successTimeThreshold time.Duration,
checkers ...check.CheckerInterface,
checkers ...check.Checker,
) Checker {
return Checker{
logger: logger,
Expand All @@ -40,8 +40,8 @@ func NewChecker(

// Check will run all the checkers, combine their's results into single result and return it.
// Each checker will run in separate go-routine
func (c Checker) Check(ctx context.Context) result.ResultInterface {
overallResult := result.Result{Success: true}
func (c Checker) Check(ctx context.Context) result.Result {
overallResult := result.DefaultResult{Success: true}
var wg sync.WaitGroup
for _, checker := range c.checkers {
wg.Add(1)
Expand Down Expand Up @@ -69,9 +69,9 @@ func (c Checker) Check(ctx context.Context) result.ResultInterface {

func (c Checker) singleCheck(
ctx context.Context,
checker check.CheckerInterface,
checker check.Checker,
wg *sync.WaitGroup,
overallResult *result.Result,
overallResult *result.DefaultResult,
) {
c.logger.Debugf("%T: Starting checker: %T", c, checker)
wrappedCtx, cancelFunc := context.WithTimeout(ctx, c.singleCheckTimeout)
Expand Down
10 changes: 5 additions & 5 deletions pkg/check/multiple/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ import (

func TestNewMultipleChecker(t *testing.T) {
checkerMock := pkgMock.CheckerMock{}
checkerMock.On("Check").Return(result.Result{})
checkerMock.On("Check").Return(result.DefaultResult{})
checker := NewChecker(&pkgMock.Logger{}, time.Second, 1, time.Second, checkerMock, checkerMock)

assert.Implements(t, (*check.CheckerInterface)(nil), checker)
assert.Implements(t, (*check.Checker)(nil), checker)
assert.Len(t, checker.checkers, 2)
assert.Equal(t, checker.singleCheckTimeout, time.Second)
}
Expand All @@ -30,8 +30,8 @@ func TestMultipleChecker_Check(t *testing.T) {
logger.On("Debugf", mock.Anything, mock.Anything)

ctx := context.TODO()
unsuccessfulResult := result.Result{Success: false, SuccessRate: 0.4, Time: time.Millisecond * 40}
successfulResult := result.Result{Success: true, SuccessRate: 0.6, Time: time.Millisecond * 60}
unsuccessfulResult := result.DefaultResult{Success: false, SuccessRate: 0.4, Time: time.Millisecond * 40}
successfulResult := result.DefaultResult{Success: true, SuccessRate: 0.6, Time: time.Millisecond * 60}
successChecker := new(pkgMock.CheckerMock)
unsuccessfulChecker := pkgMock.CheckerMock{Result: unsuccessfulResult}
successChecker.
Expand Down Expand Up @@ -64,7 +64,7 @@ func TestMultipleChecker_Check_ThresholdsConditions(t *testing.T) {
logger := &pkgMock.Logger{}
logger.On("Debugf", mock.Anything, mock.Anything)
ctx := context.TODO()
successfulResult := result.Result{
successfulResult := result.DefaultResult{
Success: true,
SuccessRate: 0.85,
Time: time.Second,
Expand Down
20 changes: 10 additions & 10 deletions pkg/check/periodic/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ const ConnectionCheckEventName = "connection.check"
// Period will be getting longer (up to maximalCheckingPeriod) if connection is stable. If any error will be detected, the period
// will go back to minimalCheckingPeriod. It will check success rate and result's time against provided threshold to check if connection is ok or not.
type Checker struct {
logger log.LoggerInterface
eventDispatcher event.DispatcherInterface
checker check.CheckerInterface
logger log.Logger
eventDispatcher event.Dispatcher
checker check.Checker
successRateThreshold float32
successTimeThreshold time.Duration
minimalCheckingPeriod time.Duration
Expand All @@ -28,9 +28,9 @@ type Checker struct {

// NewChecker will return new Checker
func NewChecker(
logger log.LoggerInterface,
eventDispatcher event.DispatcherInterface,
checker check.CheckerInterface,
logger log.Logger,
eventDispatcher event.Dispatcher,
checker check.Checker,
minimalCheckingPeriod,
maximalCheckingPeriod time.Duration,
) Checker {
Expand All @@ -44,10 +44,10 @@ func NewChecker(
}

// Check should be used to actually start checking process. In order to kill it, you have to kill it's context.
func (c Checker) Check(ctx context.Context) result.ResultInterface {
func (c Checker) Check(ctx context.Context) result.Result {
c.periodicCheck(ctx)

return result.Result{}
return result.DefaultResult{}
}

// periodicCheck will run periodic checks on provided checker.
Expand All @@ -64,13 +64,13 @@ func (c Checker) periodicCheck(ctx context.Context) {
case <-time.After(currentPeriod):
}

res := result.TimeResult{Result: c.checker.Check(ctx), MeasuredAt: time.Now()}
res := result.DefaultMeasuredAtResult{Result: c.checker.Check(ctx), MeasuredAt: time.Now()}
c.eventDispatcher.Dispatch(ConnectionCheckEventName, res)
currentPeriod = c.newPeriod(currentPeriod, res)
}
}

func (c Checker) newPeriod(currentPeriod time.Duration, result result.ResultInterface) time.Duration {
func (c Checker) newPeriod(currentPeriod time.Duration, result result.Result) time.Duration {
if !result.IsSuccess() {
return c.minimalCheckingPeriod
}
Expand Down
16 changes: 8 additions & 8 deletions pkg/check/periodic/checker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ func TestPeriodicCheckerWrapper_Check(t *testing.T) {
subChecker.
On("Check", ctx).
Times(3).
Return(result.Result{Success: true, SuccessRate: 1})
Return(result.DefaultResult{Success: true, SuccessRate: 1})
eventDispatcherMock.
On("Dispatch", "connection.check", mock.AnythingOfType("result.TimeResult")).
On("Dispatch", "connection.check", mock.AnythingOfType("result.DefaultMeasuredAtResult")).
Times(3)

checker := NewChecker(
Expand Down Expand Up @@ -85,13 +85,13 @@ func TestPeriodicCheckerWrapper_newPeriod(t *testing.T) {

scenarios := []struct {
time time.Duration
res result.Result
res result.DefaultResult
expected time.Duration
}{
{time: time.Second, res: result.Result{Success: false}, expected: minimalPeriod},
{time: minimalPeriod * 2, res: result.Result{Success: false}, expected: minimalPeriod},
{time: maximalPeriod * 2, res: result.Result{Success: true}, expected: maximalPeriod},
{time: maximalPeriod - time.Millisecond, res: result.Result{Success: true}, expected: maximalPeriod},
{time: time.Second, res: result.DefaultResult{Success: false}, expected: minimalPeriod},
{time: minimalPeriod * 2, res: result.DefaultResult{Success: false}, expected: minimalPeriod},
{time: maximalPeriod * 2, res: result.DefaultResult{Success: true}, expected: maximalPeriod},
{time: maximalPeriod - time.Millisecond, res: result.DefaultResult{Success: true}, expected: maximalPeriod},
}

for _, scenario := range scenarios {
Expand All @@ -105,7 +105,7 @@ type eventDispatcherMock struct {
mock.Mock
}

func (m eventDispatcherMock) AttachSubscriber(eventName string, subscriber event.Subscriber) event.DispatcherInterface {
func (m eventDispatcherMock) AttachSubscriber(eventName string, subscriber event.Subscriber) event.Dispatcher {
m.Called(eventName, subscriber)
return m
}
Expand Down
Loading

0 comments on commit 2d87cf0

Please sign in to comment.