Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
N
nexpie-grafana-theme
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Registry
Registry
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Kornkitt Poolsup
nexpie-grafana-theme
Commits
08f01a6c
Unverified
Commit
08f01a6c
authored
Oct 14, 2019
by
Arve Knudsen
Committed by
GitHub
Oct 14, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
pkg/bus: Check errors (#19748)
* pkg/bus: Check errors * pkg/bus: Convert tests to testify
parent
24b475e4
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
15 additions
and
33 deletions
+15
-33
pkg/bus/bus_test.go
+15
-33
No files found.
pkg/bus/bus_test.go
View file @
08f01a6c
...
...
@@ -3,8 +3,9 @@ package bus
import
(
"context"
"errors"
"fmt"
"testing"
"github.com/stretchr/testify/require"
)
type
testQuery
struct
{
...
...
@@ -29,26 +30,25 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
}
err
:=
bus
.
DispatchCtx
(
context
.
Background
(),
&
testQuery
{})
if
err
!=
ErrHandlerNotFound
{
t
.
Errorf
(
"expected bus to return HandlerNotFound is no handler is registered"
)
}
require
.
Equal
(
t
,
err
,
ErrHandlerNotFound
,
"expected bus to return HandlerNotFound since no handler is registered"
)
bus
.
AddHandler
(
handler
)
t
.
Run
(
"when a normal handler is registered"
,
func
(
t
*
testing
.
T
)
{
bus
.
Dispatch
(
&
testQuery
{})
err
:=
bus
.
Dispatch
(
&
testQuery
{})
require
.
Nil
(
t
,
err
)
if
handlerCallCount
!=
1
{
t
.
Errorf
(
"Expected normal handler to be called 1 time. was called %d"
,
handlerCallCount
)
}
require
.
Equal
(
t
,
handlerCallCount
,
1
,
"Expected normal handler to be called 1 time. was called %d"
,
handlerCallCount
)
t
.
Run
(
"when a ctx handler is registered"
,
func
(
t
*
testing
.
T
)
{
bus
.
AddHandlerCtx
(
handlerWithCtx
)
bus
.
Dispatch
(
&
testQuery
{})
err
:=
bus
.
Dispatch
(
&
testQuery
{})
require
.
Nil
(
t
,
err
)
if
handlerWithCtxCallCount
!=
1
{
t
.
Errorf
(
"Expected ctx handler to be called 1 time. was called %d"
,
handlerWithCtxCallCount
)
}
require
.
Equal
(
t
,
handlerWithCtxCallCount
,
1
,
"Expected ctx handler to be called 1 time. was called %d"
,
handlerWithCtxCallCount
)
})
})
...
...
@@ -56,23 +56,16 @@ func TestDispatchCtxCanUseNormalHandlers(t *testing.T) {
func
TestQueryHandlerReturnsError
(
t
*
testing
.
T
)
{
bus
:=
New
()
bus
.
AddHandler
(
func
(
query
*
testQuery
)
error
{
return
errors
.
New
(
"handler error"
)
})
err
:=
bus
.
Dispatch
(
&
testQuery
{})
if
err
==
nil
{
t
.
Fatal
(
"Send query failed"
)
}
else
{
t
.
Log
(
"Handler error received ok "
+
err
.
Error
())
}
require
.
Error
(
t
,
err
,
"Send query failed"
)
}
func
TestQueryHandlerReturn
(
t
*
testing
.
T
)
{
bus
:=
New
()
bus
.
AddHandler
(
func
(
q
*
testQuery
)
error
{
q
.
Resp
=
"hello from handler"
return
nil
...
...
@@ -81,32 +74,21 @@ func TestQueryHandlerReturn(t *testing.T) {
query
:=
&
testQuery
{}
err
:=
bus
.
Dispatch
(
query
)
if
err
!=
nil
{
t
.
Fatal
(
"Send query failed "
+
err
.
Error
())
}
else
if
query
.
Resp
!=
"hello from handler"
{
t
.
Fatal
(
"Failed to get response from handler"
)
}
require
.
Nil
(
t
,
err
,
"Send query failed"
)
}
func
TestEventListeners
(
t
*
testing
.
T
)
{
bus
:=
New
()
count
:=
0
bus
.
AddEventListener
(
func
(
query
*
testQuery
)
error
{
count
++
return
nil
})
bus
.
AddEventListener
(
func
(
query
*
testQuery
)
error
{
count
+=
10
return
nil
})
err
:=
bus
.
Publish
(
&
testQuery
{})
if
err
!=
nil
{
t
.
Fatal
(
"Publish event failed "
+
err
.
Error
())
}
else
if
count
!=
11
{
t
.
Fatal
(
fmt
.
Sprintf
(
"Publish event failed, listeners called: %v, expected: %v"
,
count
,
11
))
}
require
.
Nil
(
t
,
err
,
"Publish event failed"
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment