Open platform to collect and prioritize feedback
enhancement: add option to test handlers call count
goenning9248bff9be5a
diff --git a/app/cmd/routes.go b/app/cmd/routes.goindex 6fa427b4f..daf32d93d 100644--- a/app/cmd/routes.go+++ b/app/cmd/routes.go@@ -23,6 +23,7 @@ func routes(r *web.Engine) *web.Engine {mw := middlewares.Chain(middlewares.WebSetup(),middlewares.Tenant(),+ middlewares.User(),)next := mw(func(c *web.Context) error {return c.NotFound()diff --git a/app/handlers/admin_test.go b/app/handlers/admin_test.goindex 811a6a714..6617c9cb0 100644--- a/app/handlers/admin_test.go+++ b/app/handlers/admin_test.go@@ -22,9 +22,12 @@ import (func TestUpdateSettingsHandler(t *testing.T) {RegisterT(t)- var updateCmd *cmd.UpdateTenantSettingsbus.AddHandler(func(ctx context.Context, c *cmd.UpdateTenantSettings) error {- updateCmd = c+ Expect(c.Title).Equals("GoT")+ Expect(c.Invitation).Equals("Join us!")+ Expect(c.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")+ Expect(c.Locale).Equals("pt-BR")+ Expect(c.Logo.BlobKey).Equals("logos/hello-world.png")return nil})@@ -44,20 +47,20 @@ func TestUpdateSettingsHandler(t *testing.T) {)Expect(code).Equals(http.StatusOK)- Expect(updateCmd.Title).Equals("GoT")- Expect(updateCmd.Invitation).Equals("Join us!")- Expect(updateCmd.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")- Expect(updateCmd.Locale).Equals("pt-BR")- Expect(updateCmd.Logo.BlobKey).Equals("logos/hello-world.png")+ ExpectHandler(&cmd.UpdateTenantSettings{}).CalledOnce()+ ExpectHandler(&cmd.UploadImage{}).CalledOnce()}func TestUpdateSettingsHandler_NewLogo(t *testing.T) {RegisterT(t)bus.Init(fs.Service{})- var updateCmd *cmd.UpdateTenantSettingsbus.AddHandler(func(ctx context.Context, c *cmd.UpdateTenantSettings) error {- updateCmd = c+ Expect(c.Title).Equals("GoT")+ Expect(c.Invitation).Equals("Join us!")+ Expect(c.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")+ Expect(c.Locale).Equals("pt-BR")+ Expect(c.Logo.BlobKey).Equals("logos/picture.png")return nil})@@ -89,19 +92,19 @@ func TestUpdateSettingsHandler_NewLogo(t *testing.T) {}`)Expect(code).Equals(http.StatusOK)- Expect(updateCmd.Title).Equals("GoT")- Expect(updateCmd.Invitation).Equals("Join us!")- Expect(updateCmd.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")- Expect(updateCmd.Locale).Equals("pt-BR")- Expect(updateCmd.Logo.BlobKey).Equals("logos/picture.png")+ ExpectHandler(&cmd.UpdateTenantSettings{}).CalledOnce()+ ExpectHandler(&cmd.UploadImage{}).CalledOnce()}func TestUpdateSettingsHandler_RemoveLogo(t *testing.T) {RegisterT(t)- var updateCmd *cmd.UpdateTenantSettingsbus.AddHandler(func(ctx context.Context, c *cmd.UpdateTenantSettings) error {- updateCmd = c+ Expect(c.Title).Equals("GoT")+ Expect(c.Invitation).Equals("Join us!")+ Expect(c.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")+ Expect(c.Logo.Remove).IsTrue()+ Expect(c.Locale).Equals("en")return nil})@@ -127,11 +130,8 @@ func TestUpdateSettingsHandler_RemoveLogo(t *testing.T) {}`)Expect(code).Equals(http.StatusOK)- Expect(updateCmd.Title).Equals("GoT")- Expect(updateCmd.Invitation).Equals("Join us!")- Expect(updateCmd.WelcomeMessage).Equals("Welcome to GoT Feedback Forum")- Expect(updateCmd.Logo.Remove).IsTrue()- Expect(updateCmd.Locale).Equals("en")+ ExpectHandler(&cmd.UpdateTenantSettings{}).CalledOnce()+ ExpectHandler(&cmd.UploadImage{}).CalledOnce()}func TestUpdatePrivacyHandler(t *testing.T) {diff --git a/app/pkg/assert/bus_assert.go b/app/pkg/assert/bus_assert.gonew file mode 100644index 000000000..150e0bfcb--- /dev/null+++ b/app/pkg/assert/bus_assert.go@@ -0,0 +1,37 @@+package assert++import (+ "fmt"+ "reflect"++ "github.com/getfider/fider/app/pkg/bus"+ "github.com/getfider/fider/app/pkg/errors"+)++type BusHandlerAssertions struct {+ msg bus.Msg+}++// ExpectHandler is used to assert bus operations+func ExpectHandler(msg bus.Msg) *BusHandlerAssertions {+ return &BusHandlerAssertions{+ msg,+ }+}++// CalledOnce asserts that a particular handler was called once+func (a *BusHandlerAssertions) CalledOnce() bool {+ return a.CalledTimes(1)+}++// CalledTimes asserts that a particular handler was called X number of times+func (a *BusHandlerAssertions) CalledTimes(n int) bool {+ numOfCalls := bus.GetCallCount(a.msg)+ if reflect.DeepEqual(numOfCalls, n) {+ return true+ }++ err := fmt.Errorf("CalledOnce assertion failed. \n Expected: \n\t\t %s\n Actual: \n\t\t %s", describe(n), describe(numOfCalls))+ currentT.Error(errors.StackN(err, 1))+ return false+}diff --git a/app/pkg/bus/bus.go b/app/pkg/bus/bus.goindex 9b516372d..eae81b458 100644--- a/app/pkg/bus/bus.go+++ b/app/pkg/bus/bus.go@@ -7,6 +7,7 @@ import ("sync""github.com/getfider/fider/app/models/cmd"+ "github.com/getfider/fider/app/pkg/env""github.com/getfider/fider/app/pkg/errors")@@ -25,6 +26,11 @@ var listeners = make(map[string][]HandlerFunc)var services = make([]Service, 0)var busLock = &sync.RWMutex{}+// We only keep counters during unit tests to avoid unnecessary overhead+var shouldCount = env.IsTest()+var handlersCallCounter = make(map[string]int)+var counterLock = &sync.RWMutex{}+func Register(svc Service) {busLock.Lock()defer busLock.Unlock()@@ -39,6 +45,8 @@ func Reset() {services = make([]Service, 0)handlers = make(map[string]HandlerFunc)listeners = make(map[string][]HandlerFunc)++ handlersCallCounter = make(map[string]int)}// Initializes the bus services that have been registered via bus.Register@@ -99,13 +107,7 @@ func Dispatch(ctx context.Context, msgs ...Msg) error {defer busLock.RUnlock()for _, msg := range msgs {- typeof := reflect.TypeOf(msg)- if typeof.Kind() != reflect.Ptr {- panic(fmt.Errorf("'%s' is not a pointer", keyForElement(typeof)))- }-- elem := typeof.Elem()- key := keyForElement(elem)+ key := getKey(msg)handler := handlers[key]if handler == nil {panic(fmt.Errorf("could not find handler for '%s'.", key))@@ -116,6 +118,12 @@ func Dispatch(ctx context.Context, msgs ...Msg) error {reflect.ValueOf(msg),}+ if shouldCount {+ counterLock.Lock()+ handlersCallCounter[key]+++ counterLock.Unlock()+ }+ret := reflect.ValueOf(handler).Call(params)if err := ret[0].Interface(); err != nil {return err.(error)@@ -134,13 +142,7 @@ func Publish(ctx context.Context, msgs ...Msg) {defer busLock.RUnlock()for _, msg := range msgs {- typeof := reflect.TypeOf(msg)- if typeof.Kind() != reflect.Ptr {- panic(fmt.Errorf("'%s' is not a pointer", keyForElement(typeof)))- }-- elem := typeof.Elem()- key := keyForElement(elem)+ key := getKey(msg)msgListeners := listeners[key]var params = []reflect.Value{@@ -161,6 +163,23 @@ func Publish(ctx context.Context, msgs ...Msg) {}}+// GetCallCount returns the number of times a handler has been called+// Only available during unit tests+func GetCallCount(msg Msg) int {+ key := getKey(msg)+ return handlersCallCounter[key]+}++func getKey(msg Msg) string {+ typeof := reflect.TypeOf(msg)+ if typeof.Kind() != reflect.Ptr {+ panic(fmt.Errorf("'%s' is not a pointer", keyForElement(typeof)))+ }++ elem := typeof.Elem()+ return keyForElement(elem)+}+func keyForElement(t reflect.Type) string {msgTypeName := t.Name()pkgPath := t.PkgPath()