bus_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bus
  15. import (
  16. "context"
  17. "errors"
  18. "testing"
  19. "github.com/stretchr/testify/require"
  20. )
  21. type testQuery struct {
  22. ID int64
  23. Resp string
  24. }
  25. func TestDispatch(t *testing.T) {
  26. bus := New()
  27. var invoked bool
  28. bus.AddHandler(func(query *testQuery) error {
  29. invoked = true
  30. return nil
  31. })
  32. err := bus.Dispatch(&testQuery{})
  33. require.NoError(t, err)
  34. require.True(t, invoked, "expected handler to be called")
  35. }
  36. func TestDispatch_NoRegisteredHandler(t *testing.T) {
  37. bus := New()
  38. err := bus.Dispatch(&testQuery{})
  39. require.Equal(t, err, ErrHandlerNotFound,
  40. "expected bus to return HandlerNotFound since no handler is registered")
  41. }
  42. func TestDispatch_ContextHandler(t *testing.T) {
  43. bus := New()
  44. var invoked bool
  45. bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error {
  46. invoked = true
  47. return nil
  48. })
  49. err := bus.Dispatch(&testQuery{})
  50. require.NoError(t, err)
  51. require.True(t, invoked, "expected handler to be called")
  52. }
  53. func TestDispatchCtx(t *testing.T) {
  54. bus := New()
  55. var invoked bool
  56. bus.AddHandlerCtx(func(ctx context.Context, query *testQuery) error {
  57. invoked = true
  58. return nil
  59. })
  60. err := bus.DispatchCtx(context.Background(), &testQuery{})
  61. require.NoError(t, err)
  62. require.True(t, invoked, "expected handler to be called")
  63. }
  64. func TestDispatchCtx_NoRegisteredHandler(t *testing.T) {
  65. bus := New()
  66. err := bus.DispatchCtx(context.Background(), &testQuery{})
  67. require.Equal(t, err, ErrHandlerNotFound,
  68. "expected bus to return HandlerNotFound since no handler is registered")
  69. }
  70. func TestQuery(t *testing.T) {
  71. bus := New()
  72. want := "hello from handler"
  73. bus.AddHandler(func(q *testQuery) error {
  74. q.Resp = want
  75. return nil
  76. })
  77. q := &testQuery{}
  78. err := bus.Dispatch(q)
  79. require.NoError(t, err, "unable to dispatch query")
  80. require.Equal(t, want, q.Resp)
  81. }
  82. func TestQuery_HandlerReturnsError(t *testing.T) {
  83. bus := New()
  84. bus.AddHandler(func(query *testQuery) error {
  85. return errors.New("handler error")
  86. })
  87. err := bus.Dispatch(&testQuery{})
  88. require.Error(t, err, "expected error but got none")
  89. }
  90. func TestEvent(t *testing.T) {
  91. bus := New()
  92. var invoked bool
  93. bus.AddEventListener(func(query *testQuery) error {
  94. invoked = true
  95. return nil
  96. })
  97. err := bus.Publish(&testQuery{})
  98. require.NoError(t, err, "unable to publish event")
  99. require.True(t, invoked)
  100. }
  101. func TestEvent_NoRegisteredListener(t *testing.T) {
  102. bus := New()
  103. err := bus.Publish(&testQuery{})
  104. require.NoError(t, err, "unable to publish event")
  105. }