diff --git a/README.md b/README.md index 680e8dc..1669d35 100644 --- a/README.md +++ b/README.md @@ -342,7 +342,7 @@ If your Redis instance isn't located at the `localhost`, you can change host by `STORAGE_REDIS_HOST`. After all of that `go run main.go serve` should successfully start the application. -To run tests execute `go test ./...`. If your Go version is older than 1.9, then run a `/script/test`. +To run tests execute `go test ./...`. [ico-lang]: https://img.shields.io/badge/lang-go%201.13-blue.svg?style=flat-square [ico-build]: https://img.shields.io/travis/elyby/chrly.svg?style=flat-square diff --git a/eventsubscribers/logger.go b/eventsubscribers/logger.go index ad8d330..357fa98 100644 --- a/eventsubscribers/logger.go +++ b/eventsubscribers/logger.go @@ -11,14 +11,13 @@ import ( "github.com/mono83/slf/wd" "github.com/elyby/chrly/api/mojang" - "github.com/elyby/chrly/dispatcher" ) type Logger struct { slf.Logger } -func (l *Logger) ConfigureWithDispatcher(d dispatcher.EventDispatcher) { +func (l *Logger) ConfigureWithDispatcher(d Subscriber) { d.Subscribe("skinsystem:after_request", l.handleAfterSkinsystemRequest) d.Subscribe("mojang_textures:usernames:after_call", l.createMojangTexturesErrorHandler("usernames")) diff --git a/eventsubscribers/logger_test.go b/eventsubscribers/logger_test.go index 40a3248..e892234 100644 --- a/eventsubscribers/logger_test.go +++ b/eventsubscribers/logger_test.go @@ -8,14 +8,55 @@ import ( "syscall" "testing" + "github.com/mono83/slf" "github.com/mono83/slf/params" "github.com/stretchr/testify/mock" "github.com/elyby/chrly/api/mojang" "github.com/elyby/chrly/dispatcher" - "github.com/elyby/chrly/tests" ) +type LoggerMock struct { + mock.Mock +} + +func prepareLoggerArgs(message string, params []slf.Param) []interface{} { + args := []interface{}{message} + for _, v := range params { + args = append(args, v.(interface{})) + } + + return args +} + +func (l *LoggerMock) Trace(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + +func (l *LoggerMock) Debug(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + +func (l *LoggerMock) Info(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + +func (l *LoggerMock) Warning(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + +func (l *LoggerMock) Error(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + +func (l *LoggerMock) Alert(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + +func (l *LoggerMock) Emergency(message string, params ...slf.Param) { + l.Called(prepareLoggerArgs(message, params)...) +} + type LoggerTestCase struct { Events [][]interface{} ExpectedCalls [][]interface{} @@ -189,16 +230,16 @@ func init() { func TestLogger(t *testing.T) { for name, c := range loggerTestCases { t.Run(name, func(t *testing.T) { - wdMock := &tests.WdMock{} + loggerMock := &LoggerMock{} if c.ExpectedCalls != nil { for _, c := range c.ExpectedCalls { topicName, _ := c[0].(string) - wdMock.On(topicName, c[1:]...) + loggerMock.On(topicName, c[1:]...) } } reporter := &Logger{ - Logger: wdMock, + Logger: loggerMock, } d := dispatcher.New() @@ -211,7 +252,7 @@ func TestLogger(t *testing.T) { if c.ExpectedCalls != nil { for _, c := range c.ExpectedCalls { topicName, _ := c[0].(string) - wdMock.AssertCalled(t, topicName, c[1:]...) + loggerMock.AssertCalled(t, topicName, c[1:]...) } } }) diff --git a/eventsubscribers/stats_reporter.go b/eventsubscribers/stats_reporter.go index 2a1ce2b..76f0994 100644 --- a/eventsubscribers/stats_reporter.go +++ b/eventsubscribers/stats_reporter.go @@ -9,7 +9,6 @@ import ( "github.com/mono83/slf" "github.com/elyby/chrly/api/mojang" - "github.com/elyby/chrly/dispatcher" ) type StatsReporter struct { @@ -19,7 +18,7 @@ type StatsReporter struct { timersMap sync.Map } -func (s *StatsReporter) ConfigureWithDispatcher(d dispatcher.EventDispatcher) { +func (s *StatsReporter) ConfigureWithDispatcher(d Subscriber) { // Per request events d.Subscribe("skinsystem:before_request", s.handleBeforeRequest) d.Subscribe("skinsystem:after_request", s.handleAfterRequest) diff --git a/eventsubscribers/stats_reporter_test.go b/eventsubscribers/stats_reporter_test.go index 51b5b93..fd15ce8 100644 --- a/eventsubscribers/stats_reporter_test.go +++ b/eventsubscribers/stats_reporter_test.go @@ -4,14 +4,45 @@ import ( "errors" "net/http/httptest" "testing" + "time" - "github.com/stretchr/testify/mock" + "github.com/mono83/slf" "github.com/elyby/chrly/api/mojang" "github.com/elyby/chrly/dispatcher" - "github.com/elyby/chrly/tests" + + "github.com/stretchr/testify/mock" ) +func prepareStatsReporterArgs(name string, value interface{}, params []slf.Param) []interface{} { + args := []interface{}{name, value} + for _, v := range params { + args = append(args, v.(interface{})) + } + + return args +} + +type StatsReporterMock struct { + mock.Mock +} + +func (r *StatsReporterMock) IncCounter(name string, value int64, params ...slf.Param) { + r.Called(prepareStatsReporterArgs(name, value, params)...) +} + +func (r *StatsReporterMock) UpdateGauge(name string, value int64, params ...slf.Param) { + r.Called(prepareStatsReporterArgs(name, value, params)...) +} + +func (r *StatsReporterMock) RecordTimer(name string, duration time.Duration, params ...slf.Param) { + r.Called(prepareStatsReporterArgs(name, duration, params)...) +} + +func (r *StatsReporterMock) Timer(name string, params ...slf.Param) slf.Timer { + return slf.NewTimer(name, params, r) +} + type StatsReporterTestCase struct { Events map[string][]interface{} ExpectedCalls [][]interface{} @@ -326,16 +357,16 @@ var statsReporterTestCases = []*StatsReporterTestCase{ func TestStatsReporter(t *testing.T) { for _, c := range statsReporterTestCases { t.Run("handle events", func(t *testing.T) { - wdMock := &tests.WdMock{} + statsReporterMock := &StatsReporterMock{} if c.ExpectedCalls != nil { for _, c := range c.ExpectedCalls { topicName, _ := c[0].(string) - wdMock.On(topicName, c[1:]...).Once() + statsReporterMock.On(topicName, c[1:]...).Once() } } reporter := &StatsReporter{ - Reporter: wdMock, + Reporter: statsReporterMock, Prefix: "mock_prefix", } @@ -345,7 +376,7 @@ func TestStatsReporter(t *testing.T) { d.Emit(event, args...) } - wdMock.AssertExpectations(t) + statsReporterMock.AssertExpectations(t) }) } } diff --git a/eventsubscribers/subscriber.go b/eventsubscribers/subscriber.go new file mode 100644 index 0000000..8b64092 --- /dev/null +++ b/eventsubscribers/subscriber.go @@ -0,0 +1,5 @@ +package eventsubscribers + +type Subscriber interface { + Subscribe(topic string, fn interface{}) +} diff --git a/script/coverage b/script/coverage deleted file mode 100755 index 680e4fb..0000000 --- a/script/coverage +++ /dev/null @@ -1,46 +0,0 @@ -#!/bin/sh -# Based on https://github.com/mlafeldt/chef-runner/blob/34269dbb726c243dff9764007e7bd7f0fe9ee331/script/coverage -# Generate test coverage statistics for Go packages. -# -# Works around the fact that `go test -coverprofile` currently does not work -# with multiple packages, see https://code.google.com/p/go/issues/detail?id=6909 -# -# Usage: script/coverage [--html] -# -# --html Additionally create HTML report and open it in browser -# - -set -e - -workdir=.cover -profile="$workdir/cover.out" -mode=count - -generate_cover_data() { - rm -rf "$workdir" - mkdir "$workdir" - - go test -i "$@" # compile dependencies first before serializing go test invocations - for pkg in "$@"; do - f="$workdir/$(echo $pkg | tr / -).cover" - go test -covermode="$mode" -coverprofile="$f" "$pkg" - done - - echo "mode: $mode" >"$profile" - grep -h -v "^mode:" "$workdir"/*.cover >>"$profile" -} - -show_cover_report() { - go tool cover -${1}="$profile" -} - -generate_cover_data $(go list ./... | grep -v /vendor/) -show_cover_report func -case "$1" in -"") - ;; ---html) - show_cover_report html ;; -*) - echo >&2 "error: invalid option: $1"; exit 1 ;; -esac diff --git a/script/test b/script/test deleted file mode 100755 index fd4a6b4..0000000 --- a/script/test +++ /dev/null @@ -1,27 +0,0 @@ -#!/bin/sh -# Based on https://github.com/mlafeldt/chef-runner/blob/34269dbb726c243dff9764007e7bd7f0fe9ee331/script/test -# Run package tests for a file/directory, or all tests if no argument is passed. -# Useful to e.g. execute package tests for the file currently open in Vim. -# Usage: script/test [path] - -set -e - -go_pkg_from_path() { - path=$1 - if test -d "$path"; then - dir="$path" - else - dir=$(dirname "$path") - fi - (cd "$dir" && go list) -} - -if test $# -gt 0; then - pkg=$(go_pkg_from_path "$1") - verbose=-v -else - pkg=$(go list ./... | grep -v /vendor/) - verbose= -fi - -exec go test ${GOTESTOPTS:-$verbose} $pkg diff --git a/tests/wd_mock.go b/tests/wd_mock.go deleted file mode 100644 index 62d7bd1..0000000 --- a/tests/wd_mock.go +++ /dev/null @@ -1,74 +0,0 @@ -package tests - -import ( - "time" - - "github.com/mono83/slf" - "github.com/mono83/slf/wd" - "github.com/stretchr/testify/mock" -) - -func paramsSliceToInterfaceSlice(params []slf.Param) []interface{} { - result := make([]interface{}, len(params)) - for i, v := range params { - result[i], _ = v.(interface{}) - } - - return result -} - -func prepareLoggerArgs(message string, params []slf.Param) []interface{} { - return append([]interface{}{message}, paramsSliceToInterfaceSlice(params)...) -} - -type WdMock struct { - mock.Mock -} - -func (m *WdMock) Trace(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) Debug(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) Info(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) Warning(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) Error(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) Alert(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) Emergency(message string, params ...slf.Param) { - m.Called(prepareLoggerArgs(message, params)...) -} - -func (m *WdMock) IncCounter(name string, value int64, p ...slf.Param) { - m.Called(name, value) -} - -func (m *WdMock) UpdateGauge(name string, value int64, p ...slf.Param) { - m.Called(name, value) -} - -func (m *WdMock) RecordTimer(name string, d time.Duration, p ...slf.Param) { - m.Called(name, d) -} - -func (m *WdMock) Timer(name string, p ...slf.Param) slf.Timer { - return slf.NewTimer(name, p, m) -} - -func (m *WdMock) WithParams(p ...slf.Param) wd.Watchdog { - panic("this method shouldn't be used") -}