diff --git a/persistence/trigger/listen.go b/persistence/trigger/listen.go index aeda1ba..14f3417 100644 --- a/persistence/trigger/listen.go +++ b/persistence/trigger/listen.go @@ -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]) } } diff --git a/samples/account_created_event_handler.go b/samples/account_created_event_handler.go index ff14008..e10593d 100644 --- a/samples/account_created_event_handler.go +++ b/samples/account_created_event_handler.go @@ -2,6 +2,7 @@ package account import ( "fmt" + "time" "github.com/tonybka/go-base-ddd/domain/event" ) @@ -9,6 +10,8 @@ import ( // AccountCreatedEventHandler triggered once new account created type AccountCreatedEventHandler struct { accountRepo *AccountRepository + isNotified bool + isCompleted bool } func NewAccountCreatedEventHandler(accountRepo *AccountRepository) *AccountCreatedEventHandler { @@ -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 { @@ -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 } diff --git a/samples/account_repository_test.go b/samples/account_repository_test.go index 60dbd91..3bd96df 100644 --- a/samples/account_repository_test.go +++ b/samples/account_repository_test.go @@ -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" @@ -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() { @@ -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 @@ -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() {