mirror of
https://github.com/elyby/chrly.git
synced 2024-11-23 05:33:18 +05:30
Cleanup: remove tests and scripts folders.
Introduce Subscriber interface to make eventsubscriber package independent of package dispatcher.
This commit is contained in:
parent
bd13480175
commit
49a1aaada0
@ -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
|
||||
|
@ -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"))
|
||||
|
@ -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:]...)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
@ -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)
|
||||
|
@ -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)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
5
eventsubscribers/subscriber.go
Normal file
5
eventsubscribers/subscriber.go
Normal file
@ -0,0 +1,5 @@
|
||||
package eventsubscribers
|
||||
|
||||
type Subscriber interface {
|
||||
Subscribe(topic string, fn interface{})
|
||||
}
|
@ -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
|
27
script/test
27
script/test
@ -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
|
@ -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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user