Skip to content

Commit

Permalink
Merge pull request #202 from wepala/feature/200
Browse files Browse the repository at this point in the history
Feature/200
  • Loading branch information
akeemphilbert authored Aug 6, 2022
2 parents 7dc7a09 + c6102da commit 5eb1cc6
Show file tree
Hide file tree
Showing 4 changed files with 146 additions and 122 deletions.
184 changes: 92 additions & 92 deletions .github/workflows/dev.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,98 +38,98 @@ jobs:
run: go test -v ./projections -driver=postgres
# - name: Run Mysql projections tests
# run: go test -v ./projections -driver=mysql
- uses: crazy-max/ghaction-xgo@v1
name: build linux and mac
with:
xgo_version: latest
go_version: 1.16.x
dest: .
prefix: weos
targets: linux/386,linux/amd64,darwin/amd64,windows/386,windows/amd64
v: true
x: false
race: false
ldflags: -s -w
buildmode: default
- name: setup app
run: |
#setup go tests
go install github.com/cucumber/godog/cmd/godog@latest
godog -t long
#setup mac build
mkdir -p build/mac
cp api.yaml build/mac/api.yaml
cp README.md build/mac/README.md
cp LICENSE build/mac/LICENSE
cp NOTICE.txt build/mac/NOTICE.txt
cp weos-darwin-10.16-amd64 build/mac/weos
chmod +x build/mac/weos
tar -czf weos-mac-amd64.tar.gz -C build/mac .
#setup linux 32 bit build
mkdir -p build/linux32/apps/admin
cp api.yaml build/linux32/api.yaml
cp README.md build/linux32/README.md
cp LICENSE build/linux32/LICENSE
cp NOTICE.txt build/linux32/NOTICE.txt
cp weos-linux-386 build/linux32/weos
chmod +x build/linux32/weos
tar -czf weos-linux-386.tar.gz -C build/linux32 .
#setup linux 64 bit build
mkdir -p build/linux64/apps/admin
cp api.yaml build/linux64/api.yaml
cp README.md build/linux64/README.md
cp LICENSE build/linux64/LICENSE
cp NOTICE.txt build/linux64/NOTICE.txt
cp weos-linux-amd64 build/linux64/weos
chmod +x build/linux64/weos
tar -czf weos-linux-amd64.tar.gz -C build/linux64 .
#setup windows 32 bit build
mkdir -p build/win32/apps/admin
cp api.yaml build/win32/api.yaml
cp README.md build/win32/README.md
cp LICENSE build/win32/LICENSE
cp NOTICE.txt build/win32/NOTICE.txt
cp weos-windows-4.0-386.exe build/win32/weos.exe
chmod +x build/win32/weos.exe
cd build/win32
zip -r ../../weos-win32.zip .
cd ../../
#setup windows 64 bit build
mkdir -p build/win64/apps/admin
cp api.yaml build/win64/api.yaml
cp README.md build/win64/README.md
cp LICENSE build/win64/LICENSE
cp NOTICE.txt build/win64/NOTICE.txt
cp weos-windows-4.0-amd64.exe build/win64/weos.exe
chmod +x build/win64/weos.exe
cd build/win64
zip -r ../../weos-win64.zip .
- uses: actions/upload-artifact@v2
with:
name: Mac OS
path: weos-mac-amd64.tar.gz
- uses: actions/upload-artifact@v2
with:
name: Linux 32 Bit
path: weos-linux-386.tar.gz
- uses: actions/upload-artifact@v2
with:
name: Linux 64 Bit
path: weos-linux-amd64.tar.gz
- uses: actions/upload-artifact@v2
with:
name: Windows 32 Bit
path: weos-win32.zip
- uses: actions/upload-artifact@v2
with:
name: Windows 64 bit
path: weos-win64.zip
# - uses: crazy-max/ghaction-xgo@v1
# name: build linux and mac
# with:
# xgo_version: latest
# go_version: 1.16.x
# dest: .
# prefix: weos
# targets: linux/386,linux/amd64,darwin/amd64,windows/386,windows/amd64
# v: true
# x: false
# race: false
# ldflags: -s -w
# buildmode: default
# - name: setup app
# run: |
# #setup go tests
# go install github.com/cucumber/godog/cmd/godog@latest
# godog -t long
#
# #setup mac build
# mkdir -p build/mac
# cp api.yaml build/mac/api.yaml
# cp README.md build/mac/README.md
# cp LICENSE build/mac/LICENSE
# cp NOTICE.txt build/mac/NOTICE.txt
# cp weos-darwin-10.16-amd64 build/mac/weos
# chmod +x build/mac/weos
# tar -czf weos-mac-amd64.tar.gz -C build/mac .
#
# #setup linux 32 bit build
# mkdir -p build/linux32/apps/admin
# cp api.yaml build/linux32/api.yaml
# cp README.md build/linux32/README.md
# cp LICENSE build/linux32/LICENSE
# cp NOTICE.txt build/linux32/NOTICE.txt
# cp weos-linux-386 build/linux32/weos
# chmod +x build/linux32/weos
# tar -czf weos-linux-386.tar.gz -C build/linux32 .
#
# #setup linux 64 bit build
# mkdir -p build/linux64/apps/admin
# cp api.yaml build/linux64/api.yaml
# cp README.md build/linux64/README.md
# cp LICENSE build/linux64/LICENSE
# cp NOTICE.txt build/linux64/NOTICE.txt
# cp weos-linux-amd64 build/linux64/weos
# chmod +x build/linux64/weos
# tar -czf weos-linux-amd64.tar.gz -C build/linux64 .
#
# #setup windows 32 bit build
# mkdir -p build/win32/apps/admin
# cp api.yaml build/win32/api.yaml
# cp README.md build/win32/README.md
# cp LICENSE build/win32/LICENSE
# cp NOTICE.txt build/win32/NOTICE.txt
# cp weos-windows-4.0-386.exe build/win32/weos.exe
# chmod +x build/win32/weos.exe
# cd build/win32
# zip -r ../../weos-win32.zip .
# cd ../../
#
# #setup windows 64 bit build
# mkdir -p build/win64/apps/admin
# cp api.yaml build/win64/api.yaml
# cp README.md build/win64/README.md
# cp LICENSE build/win64/LICENSE
# cp NOTICE.txt build/win64/NOTICE.txt
# cp weos-windows-4.0-amd64.exe build/win64/weos.exe
# chmod +x build/win64/weos.exe
# cd build/win64
# zip -r ../../weos-win64.zip .
#
# - uses: actions/upload-artifact@v2
# with:
# name: Mac OS
# path: weos-mac-amd64.tar.gz
# - uses: actions/upload-artifact@v2
# with:
# name: Linux 32 Bit
# path: weos-linux-386.tar.gz
# - uses: actions/upload-artifact@v2
# with:
# name: Linux 64 Bit
# path: weos-linux-amd64.tar.gz
# - uses: actions/upload-artifact@v2
# with:
# name: Windows 32 Bit
# path: weos-win32.zip
# - uses: actions/upload-artifact@v2
# with:
# name: Windows 64 bit
# path: weos-win64.zip
- name: Send release notification
uses: rtCamp/action-slack-notify@v2
env:
Expand Down
52 changes: 29 additions & 23 deletions model/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,41 +37,47 @@ func (e *DefaultCommandDispatcher) Dispatch(ctx context.Context, command *Comman
defer e.dispatch.Unlock()
var wg sync.WaitGroup
var err error
if handlers, ok := e.handlers[command.Type]; ok {
var allHandlers []CommandHandler
//lets see if there are any global handlers and add those
if globalHandlers, ok := e.handlers["*"]; ok {
allHandlers = append(allHandlers, globalHandlers...)
}
//now lets add the specific command handlers
var allHandlers []CommandHandler
//first preference is handlers for specific command type and entity type
if handlers, ok := e.handlers[command.Type+command.Metadata.EntityType]; ok {
allHandlers = append(allHandlers, handlers...)

for i := 0; i < len(allHandlers); i++ {
handler := allHandlers[i]
wg.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
e.handlerPanicked = true
err = fmt.Errorf("handler error '%s'", r)
}
wg.Done()
}()
err = handler(ctx, command, container, eventStore, projection, logger)
}()
}
//if there are no handler then let's fall back to checking just handlers for the command type.
if len(allHandlers) == 0 {
if handlers, ok := e.handlers[command.Type]; ok {
allHandlers = append(allHandlers, handlers...)
}
}
//lets see if there are any global handlers and add those
if globalHandlers, ok := e.handlers["*"]; ok {
allHandlers = append(allHandlers, globalHandlers...)
}

wg.Wait()
for i := 0; i < len(allHandlers); i++ {
handler := allHandlers[i]
wg.Add(1)
go func() {
defer func() {
if r := recover(); r != nil {
e.handlerPanicked = true
err = fmt.Errorf("handler error '%s'", r)
}
wg.Done()
}()
err = handler(ctx, command, container, eventStore, projection, logger)
}()
}

wg.Wait()

return err
}

func (e *DefaultCommandDispatcher) AddSubscriber(command *Command, handler CommandHandler) map[string][]CommandHandler {
if e.handlers == nil {
e.handlers = map[string][]CommandHandler{}
}
e.handlers[command.Type] = append(e.handlers[command.Type], handler)
e.handlers[command.Type+command.Metadata.EntityType] = append(e.handlers[command.Type+command.Metadata.EntityType], handler)

return e.handlers
}
Expand Down
28 changes: 23 additions & 5 deletions model/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ func TestCommandDisptacher_Dispatch(t *testing.T) {
Version: 1,
},
}
mockCommand2 := &weos.Command{
Type: "TEST_COMMAND",
Payload: nil,
Metadata: weos.CommandMetadata{
Version: 1,
EntityType: "Blog",
},
}
dispatcher := &weos.DefaultCommandDispatcher{}
handlersCalled := 0

dispatcher.AddSubscriber(mockCommand, func(ctx context.Context, command *weos.Command, container weos.Container, eventRepository weos.EventRepository, projection weos.Projection, logger weos.Log) error {
handlersCalled += 1
return nil
Expand All @@ -33,9 +40,20 @@ func TestCommandDisptacher_Dispatch(t *testing.T) {
}
return nil
})
dispatcher.Dispatch(context.TODO(), mockCommand, nil, nil, nil, nil)

if handlersCalled != 2 {
t.Errorf("expected %d handler to be called, %d called", 2, handlersCalled)
}
t.Run("call command for specific type", func(t *testing.T) {
handlersCalled = 0
dispatcher.Dispatch(context.TODO(), mockCommand2, nil, nil, nil, nil)
if handlersCalled != 2 {
t.Errorf("expected %d handler to be called, %d called", 2, handlersCalled)
}
})
t.Run("should call handler with global handler", func(t *testing.T) {
handlersCalled = 0
dispatcher.Dispatch(context.TODO(), mockCommand, nil, nil, nil, nil)
if handlersCalled != 2 {
t.Errorf("expected %d handler to be called, %d called", 2, handlersCalled)
}
})

}
4 changes: 2 additions & 2 deletions model/receiver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func TestCreateContentType(t *testing.T) {
t.Fatalf("error converting payload to bytes %s", err)
}

err1 := commandDispatcher.Dispatch(ctx1, model.Create(ctx1, reqBytes, contentEntity1, "fsdf32432"), nil, mockEventRepository, projectionMock, echo.New().Logger)
err1 := commandDispatcher.Dispatch(ctx1, model.Create(ctx1, reqBytes, contentEntity, "fsdf32432"), nil, mockEventRepository, projectionMock, echo.New().Logger)
if err1 != nil {
t.Fatalf("unexpected error dispatching command '%s'", err1)
}
Expand All @@ -128,7 +128,7 @@ func TestCreateContentType(t *testing.T) {
t.Fatalf("error converting payload to bytes %s", err)
}

err1 := commandDispatcher.Dispatch(ctx2, model.CreateBatch(ctx2, reqBytes, contentEntity2), nil, mockEventRepository, projectionMock, echo.New().Logger)
err1 := commandDispatcher.Dispatch(ctx2, model.CreateBatch(ctx2, reqBytes, contentEntity), nil, mockEventRepository, projectionMock, echo.New().Logger)
if err1 == nil {
t.Fatalf("expected error dispatching command but got nil")
}
Expand Down

0 comments on commit 5eb1cc6

Please sign in to comment.