grafana/pkg/bus/bus_test.go

46 lines
773 B
Go
Raw Normal View History

2014-11-28 15:16:49 -06:00
package bus
import (
"errors"
"testing"
)
type TestQuery struct {
Id int64
Resp string
}
2014-12-16 09:45:07 -06:00
func TestQueryHandlerReturnsError(t *testing.T) {
2014-12-01 10:56:03 -06:00
bus := New()
2014-11-28 15:16:49 -06:00
2014-12-16 09:45:07 -06:00
bus.AddHandler(func(query *TestQuery) error {
2014-11-28 15:16:49 -06:00
return errors.New("handler error")
})
2014-12-16 09:45:07 -06:00
err := bus.Dispatch(&TestQuery{})
2014-11-28 15:16:49 -06:00
if err == nil {
2014-12-16 05:04:08 -06:00
t.Fatal("Send query failed " + err.Error())
2014-11-28 15:16:49 -06:00
} else {
t.Log("Handler error received ok")
}
}
2014-12-16 09:45:07 -06:00
func TestQueryHandlerReturn(t *testing.T) {
2014-12-01 10:56:03 -06:00
bus := New()
2014-11-28 15:16:49 -06:00
2014-12-16 09:45:07 -06:00
bus.AddHandler(func(q *TestQuery) error {
2014-11-28 15:16:49 -06:00
q.Resp = "hello from handler"
return nil
})
query := &TestQuery{}
2014-12-16 09:45:07 -06:00
err := bus.Dispatch(query)
2014-11-28 15:16:49 -06:00
if err != nil {
2014-12-16 05:04:08 -06:00
t.Fatal("Send query failed " + err.Error())
2014-11-28 15:16:49 -06:00
} else if query.Resp != "hello from handler" {
t.Fatal("Failed to get response from handler")
}
}