Skip to content

Commit

Permalink
(wip) add subscribe
Browse files Browse the repository at this point in the history
  • Loading branch information
CorrectRoadH committed Aug 5, 2024
1 parent 5bc8933 commit 34dd160
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
42 changes: 41 additions & 1 deletion pkg/ysk/ysk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,63 @@ import (
"context"
"testing"

"github.com/IceWhaleTech/CasaOS-Common/utils/logger"
"github.com/IceWhaleTech/CasaOS-MessageBus/model"
"github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk"
"github.com/IceWhaleTech/CasaOS-MessageBus/repository"
"github.com/IceWhaleTech/CasaOS-MessageBus/service"
"github.com/IceWhaleTech/CasaOS-MessageBus/utils"
"gotest.tools/assert"
)

func mockPublish(context.Context, string, string, map[string]string) {
var ws *service.EventServiceWS

func setup(t *testing.T) (*service.EventServiceWS, *service.YSKService, func()) {
repository, err := repository.NewDatabaseRepositoryInMemory()
assert.NilError(t, err)
s := service.NewServices(&repository)
wsService := s.EventServiceWS
yskService := s.YSKService

ctx := context.Background()
go s.Start(&ctx)
return wsService, yskService, func() {
repository.Close()
}
}
func mockPublish(ctx context.Context, sourceID string, eventName string, body map[string]string) {
if ws != nil {
ws.Publish(model.Event{
SourceID: sourceID,
Name: eventName,
Properties: body,
})
}
}

func TestUpdateProgress(t *testing.T) {
logger.LogInitConsoleOnly()

wsService, yskService, cleanup := setup(t)
defer cleanup()
ws = wsService

err := ysk.NewYSKCard(context.Background(), utils.ApplicationInstallProgress.WithProgress(
"Installing LinuxServer/Jellyfin", 50,
), mockPublish)
assert.NilError(t, err)

cards, err := yskService.YskCardList(context.Background())
assert.NilError(t, err)
assert.Equal(t, len(cards), 1)

assert.NilError(t, err)
err = ysk.DeleteCard(context.Background(), utils.ApplicationInstallProgress.Id, mockPublish)
assert.NilError(t, err)

cards, err = yskService.YskCardList(context.Background())
assert.NilError(t, err)
assert.Equal(t, len(cards), 0)
}

func TestNoticeDiskInsert(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion service/services.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,6 @@ func NewServices(repository *repository.Repository) Services {

ActionTypeService: actionTypeService,
ActionServiceWS: NewActionServiceWS(actionTypeService),
YSKService: NewYSKService(repository, eventServiceWS),
YSKService: NewYSKService(repository, eventServiceWS, eventTypeService),
}
}
28 changes: 23 additions & 5 deletions service/ysk.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,26 @@ import (
"fmt"
"log"

"github.com/IceWhaleTech/CasaOS-MessageBus/model"
"github.com/IceWhaleTech/CasaOS-MessageBus/pkg/ysk"
"github.com/IceWhaleTech/CasaOS-MessageBus/repository"
)

type YSKService struct {
repository *repository.Repository
ws *EventServiceWS
repository *repository.Repository
ws *EventServiceWS
eventTypeService *EventTypeService
}

func NewYSKService(repository *repository.Repository, ws *EventServiceWS) *YSKService {
func NewYSKService(
repository *repository.Repository,
ws *EventServiceWS,
ets *EventTypeService,
) *YSKService {
return &YSKService{
repository: repository,
ws: ws,
repository: repository,
ws: ws,
eventTypeService: ets,
}
}

Expand All @@ -43,6 +50,17 @@ func (s *YSKService) DeleteYSKCard(ctx context.Context, id string) error {
}

func (s *YSKService) Start() {
// register event
s.eventTypeService.RegisterEventType(model.EventType{
SourceID: ysk.SERVICENAME,
Name: "ysk:card:create",
})

s.eventTypeService.RegisterEventType(model.EventType{
SourceID: ysk.SERVICENAME,
Name: "ysk:card:delete",
})

channel, err := s.ws.Subscribe(ysk.SERVICENAME, []string{
"ysk:card:create", "ysk:card:delete",
})
Expand Down
2 changes: 1 addition & 1 deletion service/ysk_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func setup(t *testing.T) (*service.YSKService, func()) {
repository, err := repository.NewDatabaseRepositoryInMemory()
assert.NilError(t, err)

yskService := service.NewYSKService(&repository, nil)
yskService := service.NewYSKService(&repository, nil, nil)
return yskService, func() {
repository.Close()
}
Expand Down

0 comments on commit 34dd160

Please sign in to comment.