github.com/vmware/transport-go@v1.3.4/bus/eventbus_test.go (about) 1 // Copyright 2019-2020 VMware, Inc. 2 // SPDX-License-Identifier: BSD-2-Clause 3 4 package bus 5 6 import ( 7 "errors" 8 "fmt" 9 "github.com/google/uuid" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/mock" 12 "github.com/vmware/transport-go/bridge" 13 "github.com/vmware/transport-go/model" 14 "github.com/vmware/transport-go/stompserver" 15 "sync" 16 "sync/atomic" 17 "testing" 18 ) 19 20 var evtBusTest *transportEventBus 21 var evtbusTestChannelName string = "test-channel" 22 var evtbusTestManager ChannelManager 23 24 type MockBrokerConnector struct { 25 mock.Mock 26 } 27 28 func (mock *MockBrokerConnector) Connect(config *bridge.BrokerConnectorConfig, enableLogging bool) (bridge.Connection, error) { 29 args := mock.MethodCalled("Connect", config) 30 if args.Get(0) == nil { 31 return nil, args.Error(1) 32 } 33 return args.Get(0).(bridge.Connection), args.Error(1) 34 } 35 36 func (mock *MockBrokerConnector) StartTCPServer(address string) error { 37 args := mock.MethodCalled("StartTCPServer", address) 38 return args.Error(0) 39 } 40 41 func newTestEventBus() EventBus { 42 return NewEventBusInstance() 43 } 44 45 func init() { 46 evtBusTest = GetBus().(*transportEventBus) 47 } 48 49 func createTestChannel() *Channel { 50 51 //create new bus 52 //bf := new(transportEventBus) 53 //bf.init() 54 //evtBusTest = bf 55 //busInstance = bf // set GetBus() instance to return new instance also. 56 57 evtbusTestManager = evtBusTest.GetChannelManager() 58 return evtbusTestManager.CreateChannel(evtbusTestChannelName) 59 } 60 61 func inc(counter *int32) { 62 atomic.AddInt32(counter, 1) 63 } 64 65 func destroyTestChannel() { 66 evtbusTestManager.DestroyChannel(evtbusTestChannelName) 67 } 68 69 func TestEventBus_Boot(t *testing.T) { 70 71 bus1 := GetBus() 72 bus2 := GetBus() 73 bus3 := GetBus() 74 75 assert.EqualValues(t, bus1.GetId(), bus2.GetId()) 76 assert.EqualValues(t, bus2.GetId(), bus3.GetId()) 77 assert.NotNil(t, evtBusTest.GetChannelManager()) 78 } 79 80 func TestEventBus_SendResponseMessageNoChannel(t *testing.T) { 81 err := evtBusTest.SendResponseMessage("Channel-not-here", "hello melody", nil) 82 assert.NotNil(t, err) 83 } 84 85 func TestEventBus_SendRequestMessageNoChannel(t *testing.T) { 86 err := evtBusTest.SendRequestMessage("Channel-not-here", "hello melody", nil) 87 assert.NotNil(t, err) 88 } 89 90 func TestTransportEventBus_SendBroadcastMessageNoChannel(t *testing.T) { 91 err := evtBusTest.SendBroadcastMessage("Channel-not-here", "hello melody") 92 assert.NotNil(t, err) 93 } 94 95 func TestEventBus_ListenStream(t *testing.T) { 96 createTestChannel() 97 handler, err := evtBusTest.ListenStream(evtbusTestChannelName) 98 assert.Nil(t, err) 99 assert.NotNil(t, handler) 100 var count int32 = 0 101 handler.Handle( 102 func(msg *model.Message) { 103 assert.Equal(t, "hello melody", msg.Payload.(string)) 104 inc(&count) 105 }, 106 func(err error) {}) 107 108 for i := 0; i < 3; i++ { 109 evtBusTest.SendResponseMessage(evtbusTestChannelName, "hello melody", nil) 110 111 // send requests to make sure we're only getting requests 112 //evtBusTest.SendRequestMessage(evtbusTestChannelName, 0, nil) 113 evtBusTest.SendRequestMessage(evtbusTestChannelName, 1, nil) 114 } 115 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 116 assert.Equal(t, int32(3), count) 117 destroyTestChannel() 118 } 119 120 func TestTransportEventBus_ListenStreamForBroadcast(t *testing.T) { 121 createTestChannel() 122 handler, err := evtBusTest.ListenStream(evtbusTestChannelName) 123 assert.Nil(t, err) 124 assert.NotNil(t, handler) 125 var count int32 = 0 126 handler.Handle( 127 func(msg *model.Message) { 128 assert.Equal(t, "hello melody", msg.Payload.(string)) 129 inc(&count) 130 }, 131 func(err error) {}) 132 133 for i := 0; i < 3; i++ { 134 evtBusTest.SendBroadcastMessage(evtbusTestChannelName, "hello melody") 135 136 // send requests to make sure we're only getting requests 137 evtBusTest.SendRequestMessage(evtbusTestChannelName, 1, nil) 138 } 139 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 140 assert.Equal(t, int32(3), count) 141 destroyTestChannel() 142 } 143 144 func TestTransportEventBus_ListenStreamForDestination(t *testing.T) { 145 createTestChannel() 146 id := uuid.New() 147 handler, _ := evtBusTest.ListenStreamForDestination(evtbusTestChannelName, &id) 148 var count int32 = 0 149 handler.Handle( 150 func(msg *model.Message) { 151 assert.Equal(t, "hello melody", msg.Payload.(string)) 152 inc(&count) 153 }, 154 func(err error) {}) 155 156 for i := 0; i < 20; i++ { 157 evtBusTest.SendResponseMessage(evtbusTestChannelName, "hello melody", &id) 158 159 // send requests to make sure we're only getting requests 160 evtBusTest.SendRequestMessage(evtbusTestChannelName, 0, &id) 161 evtBusTest.SendRequestMessage(evtbusTestChannelName, 1, &id) 162 } 163 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 164 assert.Equal(t, int32(20), count) 165 destroyTestChannel() 166 } 167 168 func TestEventBus_ListenStreamNoChannel(t *testing.T) { 169 _, err := evtBusTest.ListenStream("missing-Channel") 170 assert.NotNil(t, err) 171 } 172 173 func TestEventBus_ListenOnce(t *testing.T) { 174 createTestChannel() 175 handler, _ := evtBusTest.ListenOnce(evtbusTestChannelName) 176 count := 0 177 handler.Handle( 178 func(msg *model.Message) { 179 count++ 180 }, 181 func(err error) {}) 182 183 for i := 0; i < 10; i++ { 184 evtBusTest.SendRequestMessage(evtbusTestChannelName, 0, handler.GetDestinationId()) 185 } 186 187 for i := 0; i < 2; i++ { 188 evtBusTest.SendResponseMessage(evtbusTestChannelName, 0, handler.GetDestinationId()) 189 190 // send requests to make sure we're only getting requests 191 evtBusTest.SendRequestMessage(evtbusTestChannelName, 0, handler.GetDestinationId()) 192 evtBusTest.SendRequestMessage(evtbusTestChannelName, 1, handler.GetDestinationId()) 193 } 194 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 195 assert.Equal(t, 1, count) 196 destroyTestChannel() 197 } 198 199 func TestEventBus_ListenOnceForDestination(t *testing.T) { 200 createTestChannel() 201 dest := uuid.New() 202 handler, _ := evtBusTest.ListenOnceForDestination(evtbusTestChannelName, &dest) 203 count := 0 204 handler.Handle( 205 func(msg *model.Message) { 206 count++ 207 }, 208 func(err error) {}) 209 210 for i := 0; i < 300; i++ { 211 evtBusTest.SendResponseMessage(evtbusTestChannelName, 0, &dest) 212 213 // send duplicate 214 evtBusTest.SendResponseMessage(evtbusTestChannelName, 0, &dest) 215 216 // send random noise 217 evtBusTest.SendResponseMessage(evtbusTestChannelName, 0, nil) 218 219 // send requests to make sure we're only getting requests 220 evtBusTest.SendRequestMessage(evtbusTestChannelName, 0, &dest) 221 evtBusTest.SendRequestMessage(evtbusTestChannelName, 1, &dest) 222 } 223 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 224 assert.Equal(t, 1, count) 225 destroyTestChannel() 226 } 227 228 func TestEventBus_ListenOnceNoChannel(t *testing.T) { 229 _, err := evtBusTest.ListenOnce("missing-Channel") 230 assert.NotNil(t, err) 231 } 232 233 func TestEventBus_ListenOnceForDestinationNoChannel(t *testing.T) { 234 _, err := evtBusTest.ListenOnceForDestination("missing-Channel", nil) 235 assert.NotNil(t, err) 236 } 237 238 func TestEventBus_ListenOnceForDestinationNoDestination(t *testing.T) { 239 createTestChannel() 240 _, err := evtBusTest.ListenOnceForDestination(evtbusTestChannelName, nil) 241 assert.NotNil(t, err) 242 destroyTestChannel() 243 } 244 245 func TestEventBus_ListenRequestStream(t *testing.T) { 246 createTestChannel() 247 handler, _ := evtBusTest.ListenRequestStream(evtbusTestChannelName) 248 var count int32 = 0 249 handler.Handle( 250 func(msg *model.Message) { 251 assert.Equal(t, "hello melody", msg.Payload.(string)) 252 inc(&count) 253 }, 254 func(err error) {}) 255 256 for i := 0; i < 10000; i++ { 257 evtBusTest.SendRequestMessage(evtbusTestChannelName, "hello melody", nil) 258 259 // send responses to make sure we're only getting requests 260 evtBusTest.SendResponseMessage(evtbusTestChannelName, "will fail assertion if picked up", nil) 261 evtBusTest.SendResponseMessage(evtbusTestChannelName, "will fail assertion again", nil) 262 } 263 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 264 assert.Equal(t, count, int32(10000)) 265 destroyTestChannel() 266 } 267 268 func TestEventBus_ListenRequestStreamForDestination(t *testing.T) { 269 createTestChannel() 270 id := uuid.New() 271 handler, _ := evtBusTest.ListenRequestStreamForDestination(evtbusTestChannelName, &id) 272 var count int32 = 0 273 handler.Handle( 274 func(msg *model.Message) { 275 assert.Equal(t, "hello melody", msg.Payload.(string)) 276 inc(&count) 277 }, 278 func(err error) {}) 279 280 for i := 0; i < 1000; i++ { 281 evtBusTest.SendRequestMessage(evtbusTestChannelName, "hello melody", &id) 282 283 // send responses to make sure we're only getting requests 284 evtBusTest.SendResponseMessage(evtbusTestChannelName, "will fail assertion if picked up", &id) 285 evtBusTest.SendResponseMessage(evtbusTestChannelName, "will fail assertion again", &id) 286 } 287 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 288 assert.Equal(t, count, int32(1000)) 289 destroyTestChannel() 290 } 291 292 func TestEventBus_ListenStreamForDestinationNoChannel(t *testing.T) { 293 _, err := evtBusTest.ListenStreamForDestination("missing-Channel", nil) 294 assert.NotNil(t, err) 295 } 296 297 func TestEventBus_ListenStreamForDestinationNoDestination(t *testing.T) { 298 createTestChannel() 299 _, err := evtBusTest.ListenStreamForDestination(evtbusTestChannelName, nil) 300 assert.NotNil(t, err) 301 } 302 303 func TestEventBus_ListenRequestStreamForDestinationNoDestination(t *testing.T) { 304 createTestChannel() 305 _, err := evtBusTest.ListenRequestStreamForDestination(evtbusTestChannelName, nil) 306 assert.NotNil(t, err) 307 } 308 309 func TestEventBus_ListenRequestStreamForDestinationNoChannel(t *testing.T) { 310 _, err := evtBusTest.ListenRequestStreamForDestination("nowhere", nil) 311 assert.NotNil(t, err) 312 } 313 314 func TestEventBus_ListenRequestOnce(t *testing.T) { 315 createTestChannel() 316 handler, _ := evtBusTest.ListenRequestOnce(evtbusTestChannelName) 317 count := 0 318 handler.Handle( 319 func(msg *model.Message) { 320 assert.Equal(t, "hello melody", msg.Payload.(string)) 321 count++ 322 }, 323 func(err error) {}) 324 325 for i := 0; i < 5; i++ { 326 evtBusTest.SendRequestMessage(evtbusTestChannelName, "hello melody", handler.GetDestinationId()) 327 } 328 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 329 assert.Equal(t, 1, count) 330 destroyTestChannel() 331 } 332 333 func TestEventBus_ListenRequestOnceForDestination(t *testing.T) { 334 createTestChannel() 335 dest := uuid.New() 336 handler, _ := evtBusTest.ListenRequestOnceForDestination(evtbusTestChannelName, &dest) 337 count := 0 338 handler.Handle( 339 func(msg *model.Message) { 340 assert.Equal(t, "hello melody", msg.Payload.(string)) 341 count++ 342 }, 343 func(err error) {}) 344 345 for i := 0; i < 5; i++ { 346 evtBusTest.SendRequestMessage(evtbusTestChannelName, "hello melody", &dest) 347 } 348 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 349 assert.Equal(t, 1, count) 350 destroyTestChannel() 351 } 352 353 func TestEventBus_ListenRequestOnceNoChannel(t *testing.T) { 354 _, err := evtBusTest.ListenRequestOnce("missing-Channel") 355 assert.NotNil(t, err) 356 } 357 358 func TestEventBus_ListenRequestStreamNoChannel(t *testing.T) { 359 _, err := evtBusTest.ListenRequestStream("missing-Channel") 360 assert.NotNil(t, err) 361 } 362 363 func TestEventBus_ListenRequestOnceForDestinationNoChannel(t *testing.T) { 364 _, err := evtBusTest.ListenRequestOnceForDestination("missing-Channel", nil) 365 assert.NotNil(t, err) 366 } 367 368 func TestEventBus_ListenRequestOnceForDestinationNoDestination(t *testing.T) { 369 createTestChannel() 370 _, err := evtBusTest.ListenRequestOnceForDestination(evtbusTestChannelName, nil) 371 assert.NotNil(t, err) 372 destroyTestChannel() 373 } 374 375 func TestEventBus_TestErrorMessageHandling(t *testing.T) { 376 createTestChannel() 377 378 err := evtBusTest.SendErrorMessage("invalid-Channel", errors.New("something went wrong"), nil) 379 assert.NotNil(t, err) 380 381 handler, _ := evtBusTest.ListenStream(evtbusTestChannelName) 382 var countError int32 = 0 383 handler.Handle( 384 func(msg *model.Message) {}, 385 func(err error) { 386 assert.Errorf(t, err, "something went wrong") 387 inc(&countError) 388 }) 389 390 for i := 0; i < 5; i++ { 391 err := errors.New("something went wrong") 392 evtBusTest.SendErrorMessage(evtbusTestChannelName, err, handler.GetId()) 393 } 394 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 395 assert.Equal(t, int32(5), countError) 396 destroyTestChannel() 397 } 398 399 func TestEventBus_ListenFirehose(t *testing.T) { 400 createTestChannel() 401 var counter int32 = 0 402 403 responseHandler, _ := evtBusTest.ListenFirehose(evtbusTestChannelName) 404 responseHandler.Handle( 405 func(msg *model.Message) { 406 inc(&counter) 407 }, 408 func(err error) { 409 inc(&counter) 410 }) 411 for i := 0; i < 5; i++ { 412 err := errors.New("something went wrong") 413 evtBusTest.SendErrorMessage(evtbusTestChannelName, err, nil) 414 evtBusTest.SendRequestMessage(evtbusTestChannelName, 0, nil) 415 evtBusTest.SendResponseMessage(evtbusTestChannelName, 1, nil) 416 } 417 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 418 assert.Equal(t, counter, int32(15)) 419 destroyTestChannel() 420 } 421 422 func TestEventBus_ListenFirehoseNoChannel(t *testing.T) { 423 _, err := evtBusTest.ListenFirehose("missing-Channel") 424 assert.NotNil(t, err) 425 } 426 427 func TestEventBus_RequestOnce(t *testing.T) { 428 createTestChannel() 429 handler, _ := evtBusTest.ListenRequestStream(evtbusTestChannelName) 430 handler.Handle( 431 func(msg *model.Message) { 432 assert.Equal(t, "who is a pretty baby?", msg.Payload.(string)) 433 evtBusTest.SendResponseMessage(evtbusTestChannelName, "why melody is of course", msg.DestinationId) 434 }, 435 func(err error) {}) 436 437 count := 0 438 responseHandler, _ := evtBusTest.RequestOnce(evtbusTestChannelName, "who is a pretty baby?") 439 responseHandler.Handle( 440 func(msg *model.Message) { 441 assert.Equal(t, "why melody is of course", msg.Payload.(string)) 442 count++ 443 }, 444 func(err error) {}) 445 446 responseHandler.Fire() 447 evtbusTestManager.WaitForChannel(evtbusTestChannelName) 448 assert.Equal(t, 1, count) 449 destroyTestChannel() 450 } 451 452 func TestEventBus_RequestOnceForDestination(t *testing.T) { 453 createTestChannel() 454 dest := uuid.New() 455 handler, _ := evtBusTest.ListenRequestStream(evtbusTestChannelName) 456 handler.Handle( 457 func(msg *model.Message) { 458 assert.Equal(t, "who is a pretty baby?", msg.Payload.(string)) 459 evtBusTest.SendResponseMessage(evtbusTestChannelName, "why melody is of course", msg.DestinationId) 460 }, 461 func(err error) {}) 462 463 count := 0 464 responseHandler, _ := evtBusTest.RequestOnceForDestination(evtbusTestChannelName, "who is a pretty baby?", &dest) 465 responseHandler.Handle( 466 func(msg *model.Message) { 467 assert.Equal(t, "why melody is of course", msg.Payload.(string)) 468 count++ 469 }, 470 func(err error) {}) 471 472 responseHandler.Fire() 473 assert.Equal(t, 1, count) 474 destroyTestChannel() 475 } 476 477 func TestEventBus_RequestOnceForDesintationNoChannel(t *testing.T) { 478 _, err := evtBusTest.RequestOnceForDestination("some-chan", nil, nil) 479 assert.NotNil(t, err) 480 } 481 482 func TestEventBus_RequestOnceForDesintationNoDestination(t *testing.T) { 483 createTestChannel() 484 _, err := evtBusTest.RequestOnceForDestination(evtbusTestChannelName, nil, nil) 485 assert.NotNil(t, err) 486 destroyTestChannel() 487 } 488 489 func TestEventBus_RequestStream(t *testing.T) { 490 channel := createTestChannel() 491 handler := func(message *model.Message) { 492 if message.Direction == model.RequestDir { 493 assert.Equal(t, "who has the cutest laugh?", message.Payload.(string)) 494 config := buildConfig(channel.Name, "why melody does of course", message.DestinationId) 495 496 // fire a few times, ensure that the handler only ever picks up a single response. 497 for i := 0; i < 5; i++ { 498 channel.Send(model.GenerateResponse(config)) 499 } 500 } 501 } 502 id := uuid.New() 503 channel.subscribeHandler(&channelEventHandler{callBackFunction: handler, runOnce: false, uuid: &id}) 504 505 var count int32 = 0 506 responseHandler, _ := evtBusTest.RequestStream(evtbusTestChannelName, "who has the cutest laugh?") 507 responseHandler.Handle( 508 func(msg *model.Message) { 509 assert.Equal(t, "why melody does of course", msg.Payload.(string)) 510 inc(&count) 511 }, 512 func(err error) {}) 513 514 responseHandler.Fire() 515 assert.Equal(t, int32(5), count) 516 destroyTestChannel() 517 } 518 519 func TestEventBus_RequestStreamForDesintation(t *testing.T) { 520 channel := createTestChannel() 521 dest := uuid.New() 522 handler := func(message *model.Message) { 523 if message.Direction == model.RequestDir { 524 assert.Equal(t, "who has the cutest laugh?", message.Payload.(string)) 525 config := buildConfig(channel.Name, "why melody does of course", message.DestinationId) 526 527 // fire a few times, ensure that the handler only ever picks up a single response. 528 for i := 0; i < 5; i++ { 529 channel.Send(model.GenerateResponse(config)) 530 } 531 } 532 } 533 id := uuid.New() 534 channel.subscribeHandler(&channelEventHandler{callBackFunction: handler, runOnce: false, uuid: &id}) 535 536 var count int32 = 0 537 responseHandler, _ := evtBusTest.RequestStreamForDestination(evtbusTestChannelName, "who has the cutest laugh?", &dest) 538 responseHandler.Handle( 539 func(msg *model.Message) { 540 assert.Equal(t, "why melody does of course", msg.Payload.(string)) 541 inc(&count) 542 }, 543 func(err error) {}) 544 545 responseHandler.Fire() 546 assert.Equal(t, int32(5), count) 547 destroyTestChannel() 548 } 549 550 func TestEventBus_RequestStreamForDestinationNoChannel(t *testing.T) { 551 _, err := evtBusTest.RequestStreamForDestination("missing-Channel", nil, nil) 552 assert.NotNil(t, err) 553 } 554 555 func TestEventBus_RequestStreamForDestinationNoDestination(t *testing.T) { 556 createTestChannel() 557 _, err := evtBusTest.RequestStreamForDestination(evtbusTestChannelName, nil, nil) 558 assert.NotNil(t, err) 559 destroyTestChannel() 560 } 561 562 func TestEventBus_RequestStreamNoChannel(t *testing.T) { 563 _, err := evtBusTest.RequestStream("missing-Channel", nil) 564 assert.NotNil(t, err) 565 } 566 567 func TestEventBus_HandleSingleRunError(t *testing.T) { 568 channel := createTestChannel() 569 handler := func(message *model.Message) { 570 if message.Direction == model.RequestDir { 571 config := buildError(channel.Name, fmt.Errorf("whoops!"), message.DestinationId) 572 573 // fire a few times, ensure that the handler only ever picks up a single response. 574 for i := 0; i < 5; i++ { 575 channel.Send(model.GenerateError(config)) 576 } 577 } 578 } 579 id := uuid.New() 580 channel.subscribeHandler(&channelEventHandler{callBackFunction: handler, runOnce: true, uuid: &id}) 581 582 count := 0 583 responseHandler, _ := evtBusTest.RequestOnce(evtbusTestChannelName, 0) 584 responseHandler.Handle( 585 func(msg *model.Message) {}, 586 func(err error) { 587 assert.Error(t, err, "whoops!") 588 count++ 589 }) 590 591 responseHandler.Fire() 592 assert.Equal(t, 1, count) 593 destroyTestChannel() 594 } 595 596 func TestEventBus_RequestOnceNoChannel(t *testing.T) { 597 _, err := evtBusTest.RequestOnce("missing-Channel", 0) 598 assert.NotNil(t, err) 599 } 600 601 func TestEventBus_HandlerWithoutRequestToFire(t *testing.T) { 602 createTestChannel() 603 responseHandler, _ := evtBusTest.ListenFirehose(evtbusTestChannelName) 604 responseHandler.Handle( 605 func(msg *model.Message) {}, 606 func(err error) {}) 607 err := responseHandler.Fire() 608 assert.Errorf(t, err, "nothing to fire, request is empty") 609 destroyTestChannel() 610 } 611 612 func TestEventBus_GetStoreManager(t *testing.T) { 613 assert.NotNil(t, evtBusTest.GetStoreManager()) 614 store := evtBusTest.GetStoreManager().CreateStore("test") 615 assert.NotNil(t, store) 616 assert.True(t, evtBusTest.GetStoreManager().DestroyStore("test")) 617 } 618 619 func TestChannelManager_TestConnectBroker(t *testing.T) { 620 621 // create new transportEventBus instance and replace the brokerConnector 622 // with MockBrokerConnector instance. 623 evtBusTest := newTestEventBus().(*transportEventBus) 624 evtBusTest.bc = new(MockBrokerConnector) 625 626 // connect to broker 627 cf := &bridge.BrokerConnectorConfig{ 628 Username: "test", 629 Password: "test", 630 UseWS: true, 631 WebSocketConfig: &bridge.WebSocketConfig{ 632 WSPath: "/", 633 }, 634 ServerAddr: "broker-url"} 635 636 id := uuid.New() 637 mockCon := &MockBridgeConnection{ 638 Id: &id, 639 } 640 evtBusTest.bc.(*MockBrokerConnector).On("Connect", cf).Return(mockCon, nil) 641 642 c, _ := evtBusTest.ConnectBroker(cf) 643 644 assert.Equal(t, c, mockCon) 645 assert.Equal(t, len(evtBusTest.brokerConnections), 1) 646 assert.Equal(t, evtBusTest.brokerConnections[mockCon.Id], mockCon) 647 } 648 649 func TestEventBus_TestCreateSyncTransaction(t *testing.T) { 650 tr := evtBusTest.CreateSyncTransaction() 651 assert.NotNil(t, tr) 652 assert.Equal(t, tr.(*busTransaction).transactionType, syncTransaction) 653 } 654 655 func TestEventBus_TestCreateAsyncTransaction(t *testing.T) { 656 tr := evtBusTest.CreateAsyncTransaction() 657 assert.NotNil(t, tr) 658 assert.Equal(t, tr.(*busTransaction).transactionType, asyncTransaction) 659 } 660 661 type MockRawConnListener struct { 662 stopped bool 663 connections chan stompserver.RawConnection 664 wg sync.WaitGroup 665 } 666 667 func (cl *MockRawConnListener) Accept() (stompserver.RawConnection, error) { 668 cl.wg.Done() 669 con := <-cl.connections 670 return con, nil 671 } 672 673 func (cl *MockRawConnListener) Close() error { 674 cl.stopped = true 675 cl.wg.Done() 676 return nil 677 } 678 679 func TestBifrostEventBus_StartFabricEndpoint(t *testing.T) { 680 bus := newTestEventBus().(*transportEventBus) 681 682 connListener := &MockRawConnListener{ 683 connections: make(chan stompserver.RawConnection), 684 } 685 686 err := bus.StartFabricEndpoint(connListener, EndpointConfig{}) 687 assert.EqualError(t, err, "invalid TopicPrefix") 688 689 err = bus.StartFabricEndpoint(connListener, EndpointConfig{TopicPrefix: "asd"}) 690 assert.EqualError(t, err, "invalid TopicPrefix") 691 692 err = bus.StartFabricEndpoint(connListener, EndpointConfig{TopicPrefix: "/topic", 693 AppRequestQueuePrefix: "/pub"}) 694 assert.EqualError(t, err, "missing UserQueuePrefix") 695 696 connListener.wg.Add(1) 697 go bus.StartFabricEndpoint(connListener, EndpointConfig{TopicPrefix: "/topic"}) 698 699 connListener.wg.Wait() 700 701 err = bus.StartFabricEndpoint(connListener, EndpointConfig{TopicPrefix: "/topic"}) 702 assert.EqualError(t, err, "unable to start: fabric endpoint is already running") 703 704 connListener.wg.Add(1) 705 bus.StopFabricEndpoint() 706 connListener.wg.Wait() 707 708 assert.Nil(t, bus.fabEndpoint) 709 assert.True(t, connListener.stopped) 710 711 assert.EqualError(t, bus.StopFabricEndpoint(), "unable to stop: fabric endpoint is not running") 712 } 713 714 func TestBifrostEventBus_AddMonitorEventListener(t *testing.T) { 715 716 bus := newTestEventBus() 717 718 listener1Count := 0 719 listener1 := bus.AddMonitorEventListener(func(event *MonitorEvent) { 720 listener1Count++ 721 }, ChannelCreatedEvt) 722 723 listener2Count := 0 724 listener2 := bus.AddMonitorEventListener(func(event *MonitorEvent) { 725 listener2Count++ 726 }, ChannelCreatedEvt, ChannelDestroyedEvt) 727 728 listener3Count := 0 729 listener3 := bus.AddMonitorEventListener(func(event *MonitorEvent) { 730 listener3Count++ 731 }) 732 733 assert.NotEqual(t, listener1, listener2) 734 assert.NotEqual(t, listener1, listener3) 735 assert.NotEqual(t, listener2, listener3) 736 737 bus.SendMonitorEvent(ChannelCreatedEvt, "test-channel", nil) 738 assert.Equal(t, listener1Count, 1) 739 assert.Equal(t, listener2Count, 1) 740 assert.Equal(t, listener3Count, 1) 741 742 bus.SendMonitorEvent(ChannelDestroyedEvt, "test-channel", nil) 743 assert.Equal(t, listener1Count, 1) 744 assert.Equal(t, listener2Count, 2) 745 assert.Equal(t, listener3Count, 2) 746 747 bus.SendMonitorEvent(StoreInitializedEvt, "store1", nil) 748 assert.Equal(t, listener1Count, 1) 749 assert.Equal(t, listener2Count, 2) 750 assert.Equal(t, listener3Count, 3) 751 752 bus.RemoveMonitorEventListener(listener2) 753 754 bus.SendMonitorEvent(ChannelCreatedEvt, "test-channel", nil) 755 assert.Equal(t, listener1Count, 2) 756 assert.Equal(t, listener2Count, 2) 757 assert.Equal(t, listener3Count, 4) 758 759 bus.SendMonitorEvent(ChannelDestroyedEvt, "test-channel", nil) 760 assert.Equal(t, listener1Count, 2) 761 assert.Equal(t, listener2Count, 2) 762 assert.Equal(t, listener3Count, 5) 763 764 bus.RemoveMonitorEventListener(listener3) 765 bus.SendMonitorEvent(ChannelCreatedEvt, "test-channel", nil) 766 assert.Equal(t, listener1Count, 3) 767 assert.Equal(t, listener2Count, 2) 768 assert.Equal(t, listener3Count, 5) 769 }