Skip to content

Commit

Permalink
Merge pull request #14 from tonybka/enhancement/tonybka/publish-domai…
Browse files Browse the repository at this point in the history
…n-event-goroutine

Trigger data model hooks with goroutine
  • Loading branch information
tonybka authored Apr 15, 2023
2 parents bd7b5e9 + cd803a0 commit 885e910
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 4 deletions.
2 changes: 1 addition & 1 deletion persistence/trigger/listen.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ func listenToTriggers(pool *pgxpool.Pool, callback func(uint, string, uint, stri

payloads := strings.Split(notification.Payload, ";")
rowID, _ := strconv.ParseUint(payloads[1], 10, 64)
callback(uint(notification.PID), payloads[0], uint(rowID), payloads[2])
go callback(uint(notification.PID), payloads[0], uint(rowID), payloads[2])
}
}
8 changes: 8 additions & 0 deletions samples/account_created_event_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package account

import (
"fmt"
"time"

"github.com/tonybka/go-base-ddd/domain/event"
)

// AccountCreatedEventHandler triggered once new account created
type AccountCreatedEventHandler struct {
accountRepo *AccountRepository
isNotified bool
isCompleted bool
}

func NewAccountCreatedEventHandler(accountRepo *AccountRepository) *AccountCreatedEventHandler {
Expand All @@ -17,7 +20,9 @@ func NewAccountCreatedEventHandler(accountRepo *AccountRepository) *AccountCreat

func (handler *AccountCreatedEventHandler) Notify(event event.IBaseDomainEvent) error {
fmt.Println("AccountCreatedEventHandler.Notify: get notified")
handler.isNotified = true
accountCreatedEvent := event.(*AccountCreatedEvent)
time.Sleep(1 * time.Second)

account, err := handler.accountRepo.FindById(accountCreatedEvent.AggregateID())
if err != nil {
Expand All @@ -27,5 +32,8 @@ func (handler *AccountCreatedEventHandler) Notify(event event.IBaseDomainEvent)

fmt.Printf("Account ID: %d\n", account.ID)
fmt.Printf("Account Name: %s\n", account.AccountName)

fmt.Println("AccountCreatedEventHandler.Notify: able to complete")
handler.isCompleted = true
return nil
}
19 changes: 16 additions & 3 deletions samples/account_repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/tonybka/go-base-ddd/domain/entity"
Expand Down Expand Up @@ -37,8 +38,9 @@ CREATE TABLE emails

type AccountRepositoryTestSuite struct {
suite.Suite
accountRepo *AccountRepository
emailRepo *EmailRepository
accountRepo *AccountRepository
emailRepo *EmailRepository
accountCreatedHandler *AccountCreatedEventHandler
}

func (ts *AccountRepositoryTestSuite) SetupSuite() {
Expand Down Expand Up @@ -73,7 +75,9 @@ func (ts *AccountRepositoryTestSuite) SetupSuite() {
require.NoError(ts.T(), err)

// Register handlers of domain event
accountCreatedSubscribers := []event.IDomainEvenHandler{NewAccountCreatedEventHandler(ts.accountRepo)}
accountCreatedHandler := NewAccountCreatedEventHandler(ts.accountRepo)
ts.accountCreatedHandler = accountCreatedHandler
accountCreatedSubscribers := []event.IDomainEvenHandler{accountCreatedHandler}
publisher.RegisterSubscriber(&AccountCreatedEvent{}, accountCreatedSubscribers...)

// Reset random seed to make sure the generated value is unique
Expand Down Expand Up @@ -125,8 +129,17 @@ func (ts *AccountRepositoryTestSuite) TestAccountWithEvent() {

account.AddEvent(DBTblNameAccounts, NewAccountCreatedEvent(uint(randId), nil))

assert.False(ts.T(), ts.accountCreatedHandler.isNotified)
_, err := ts.accountRepo.Create(account)
ts.NoError(err)

ts.Eventually(func() bool {
return ts.accountCreatedHandler.isNotified
}, 2*time.Second, 100*time.Microsecond, "Expect the event handler to be notified")

ts.Eventually(func() bool {
return ts.accountCreatedHandler.isCompleted
}, 2*time.Second, 100*time.Microsecond, "Expect the event handler processing is able to be completed")
}

func (ts *AccountRepositoryTestSuite) TearDownSuite() {
Expand Down

0 comments on commit 885e910

Please sign in to comment.