Skip to content

Commit

Permalink
improved and fixed test suit
Browse files Browse the repository at this point in the history
  • Loading branch information
stanipetrosyan committed Aug 24, 2023
1 parent feab3f6 commit 36dadff
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 60 deletions.
14 changes: 0 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,20 +45,6 @@ for {
}
```

If you want handle once:
```go
var eventbus = goeventbus.NewEventBus()

address := "topic"

eventbus.SubscribeOnce(address, func(dc goeventbus.DeliveryContext) {
fmt.Printf("This Message %s\n will be printed once time", dc.Result().Data)
})

message := goeventbus.CreateMessage().SetBody("Hi Topic")
eventbus.Publish(address, message)
```

## Request/Reply messaging

```go
Expand Down
2 changes: 1 addition & 1 deletion eventbus.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (e *DefaultEventBus) AddInBoundInterceptor(address string, callback func(co
}

channels := e.topics[address].GetChannels()
handler := NewConsumer(address, callback).SetContext(NewDeliveryContext(channels))
handler := NewInterceptor(address, callback).SetContext(NewDeliveryContext(channels))

e.rm.Lock()
e.topics[address].AddHandler(handler)
Expand Down
95 changes: 50 additions & 45 deletions eventbus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,76 +12,81 @@ var wg sync.WaitGroup
func TestSubscribeHandler(t *testing.T) {
var eventBus = NewEventBus()

wg.Add(1)
t.Run("should handle a message", func(t *testing.T) {
wg.Add(1)

eventBus.Subscribe("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})
eventBus.Subscribe("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})

message := CreateMessage().SetBody("Hi There")
eventBus.Publish("address", message)
wg.Wait()
}
message := CreateMessage().SetBody("Hi There")
eventBus.Publish("address", message)
wg.Wait()
})

func TestTwiceSubscribe(t *testing.T) {
var eventBus = NewEventBus()
t.Run("should handle message for more of one handlers", func(t *testing.T) {
wg.Add(2)

wg.Add(2)
eventBus.Subscribe("newaddress", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})

eventBus.Subscribe("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})
eventBus.Subscribe("newaddress", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})

eventBus.Subscribe("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
message := CreateMessage().SetBody("Hi There")
eventBus.Publish("newaddress", message)
wg.Wait()
})

message := CreateMessage().SetBody("Hi There")
eventBus.Publish("address", message)
wg.Wait()
}

func TestRequestReplyHandler(t *testing.T) {
var eventBus = NewEventBus()

wg.Add(1)
t.Run("should send a request and reply", func(t *testing.T) {
wg.Add(1)

eventBus.Subscribe("address", func(context DeliveryContext) {
context.Reply("Hello")
})
eventBus.Subscribe("address", func(context DeliveryContext) {
context.Reply("Hello")
})

message := CreateMessage().SetBody("Hi There")
eventBus.Request("address", message, func(context DeliveryContext) {
context.Handle(func(message Message) {
assert.Equal(t, "Hello", message.Data)
wg.Done()
message := CreateMessage().SetBody("Hi There")
eventBus.Request("address", message, func(context DeliveryContext) {
context.Handle(func(message Message) {
assert.Equal(t, "Hello", message.Data)
wg.Done()
})
})
wg.Wait()
})
wg.Wait()
}

func TestInBoundInterceptorHandler(t *testing.T) {
var eventBus = NewEventBus()

wg.Add(2)
t.Run("should pass interceptor handler", func(t *testing.T) {
wg.Add(2)

eventBus.Subscribe("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})
eventBus.Subscribe("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
})

eventBus.AddInBoundInterceptor("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
context.Next()
})
eventBus.AddInBoundInterceptor("address", func(context DeliveryContext) {
assert.Equal(t, "Hi There", context.Result().Data)
wg.Done()
context.Next()
})

message := CreateMessage().SetBody("Hi There")
eventBus.Publish("address", message)
wg.Wait()
message := CreateMessage().SetBody("Hi There")
eventBus.Publish("address", message)
wg.Wait()
})
}

func TestMessageOptions(t *testing.T) {
Expand Down

0 comments on commit 36dadff

Please sign in to comment.