Skip to content

Commit

Permalink
fix: [#523] [Bug] The same listener can not be registered by multiple…
Browse files Browse the repository at this point in the history
… events, framework will threw an error: job signature duplicate (#730)
  • Loading branch information
hwbrzzl authored Nov 20, 2024
1 parent 0f9231b commit a40053e
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 5 deletions.
18 changes: 13 additions & 5 deletions event/application.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
package event

import (
"slices"

"github.com/goravel/framework/contracts/event"
queuecontract "github.com/goravel/framework/contracts/queue"
"github.com/goravel/framework/contracts/queue"
)

type Application struct {
events map[event.Event][]event.Listener
queue queuecontract.Queue
queue queue.Queue
}

func NewApplication(queue queuecontract.Queue) *Application {
func NewApplication(queue queue.Queue) *Application {
return &Application{
queue: queue,
}
}

func (app *Application) Register(events map[event.Event][]event.Listener) {
var jobs []queuecontract.Job
var (
jobs []queue.Job
jobNames []string
)

if app.events == nil {
app.events = map[event.Event][]event.Listener{}
Expand All @@ -26,7 +31,10 @@ func (app *Application) Register(events map[event.Event][]event.Listener) {
for e, listeners := range events {
app.events[e] = listeners
for _, listener := range listeners {
jobs = append(jobs, listener)
if !slices.Contains(jobNames, listener.Signature()) {
jobs = append(jobs, listener)
jobNames = append(jobNames, listener.Signature())
}
}
}

Expand Down
76 changes: 76 additions & 0 deletions event/application_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package event

import (
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"

"github.com/goravel/framework/contracts/event"
"github.com/goravel/framework/contracts/queue"
mocksevent "github.com/goravel/framework/mocks/event"
mocksqueue "github.com/goravel/framework/mocks/queue"
)

func TestApplication_Register(t *testing.T) {
var (
mockQueue *mocksqueue.Queue
)

tests := []struct {
name string
events func() map[event.Event][]event.Listener
}{
{
name: "MultipleEvents",
events: func() map[event.Event][]event.Listener {
event1 := mocksevent.NewEvent(t)
event2 := mocksevent.NewEvent(t)
listener1 := mocksevent.NewListener(t)
listener1.EXPECT().Signature().Return("listener1").Twice()
listener2 := mocksevent.NewListener(t)
listener2.EXPECT().Signature().Return("listener2").Times(3)

mockQueue.EXPECT().Register(mock.MatchedBy(func(listeners []queue.Job) bool {
return assert.ElementsMatch(t, []queue.Job{
listener1,
listener2,
}, listeners)
})).Once()

return map[event.Event][]event.Listener{
event1: {
listener1,
listener2,
},
event2: {
listener2,
},
}
},
},
{
name: "NoEvents",
events: func() map[event.Event][]event.Listener {
mockQueue.EXPECT().Register([]queue.Job(nil)).Once()

return nil
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
mockQueue = mocksqueue.NewQueue(t)
app := NewApplication(mockQueue)

events := tt.events()
app.Register(events)

assert.Equal(t, len(events), len(app.GetEvents()))
for e, listeners := range events {
assert.ElementsMatch(t, listeners, app.GetEvents()[e])
}
})
}
}

0 comments on commit a40053e

Please sign in to comment.