Skip to content

Commit

Permalink
refactor(marketing): 添加新event及生产者、引入业务无关的event key生成器,修复相关测试 (#134)
Browse files Browse the repository at this point in the history
Signed-off-by: longyue0521 <[email protected]>
  • Loading branch information
longyue0521 authored May 8, 2024
1 parent 4497161 commit fea1a31
Show file tree
Hide file tree
Showing 9 changed files with 366 additions and 24 deletions.
16 changes: 16 additions & 0 deletions internal/marketing/internal/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package event
const (
MemberUpdateEventName = "member_update_events"
OrderEventName = "order_events"
CreditEventName = "credit_increase_events"
PermissionEventName = "permission_events"
)

type MemberEvent struct {
Expand All @@ -38,3 +40,17 @@ type SPU struct {
ID int64 `json:"id"`
Category string `json:"category"`
}

type CreditIncreaseEvent struct {
Key string `json:"key"`
Uid int64 `json:"uid"` // 用户A 用户C
Amount uint64 `json:"amount"` // 增加100 增加1000
Biz string `json:"biz"` // user order
BizId int64 `json:"biz_id"` // user_id=B order_id
Action string `json:"action"` // 邀请注册 购买商品
}

type PermissionEvent struct {
// todo: 权限模块
Key string `json:"key"`
}
78 changes: 78 additions & 0 deletions internal/marketing/internal/event/mocks/credit.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 78 additions & 0 deletions internal/marketing/internal/event/mocks/permission.mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package producer

import (
"context"
"encoding/json"
"fmt"

"github.com/ecodeclub/mq-api"
"github.com/ecodeclub/webook/internal/marketing/internal/event"
)

//go:generate mockgen -source=./credit_event_producer.go -package=evtmocks -destination=../mocks/credit.mock.go -typed CreditEventProducer
type CreditEventProducer interface {
Produce(ctx context.Context, evt event.CreditIncreaseEvent) error
}

type creditEventProducer struct {
producer mq.Producer
}

func NewCreditEventProducer(q mq.MQ) (CreditEventProducer, error) {
producer, err := q.Producer(event.PermissionEventName)
if err != nil {
return nil, err
}
return &creditEventProducer{
producer: producer,
}, nil
}

func (s *creditEventProducer) Produce(ctx context.Context, evt event.CreditIncreaseEvent) error {
data, err := json.Marshal(&evt)
if err != nil {
return fmt.Errorf("序列化失败: %w", err)
}
_, err = s.producer.Produce(ctx, &mq.Message{
Key: []byte(evt.Key),
Value: data,
})
return err
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2023 ecodeclub
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package producer

import (
"context"
"encoding/json"
"fmt"

"github.com/ecodeclub/mq-api"
"github.com/ecodeclub/webook/internal/marketing/internal/event"
)

//go:generate mockgen -source=./permission_event_producer.go -package=evtmocks -destination=../mocks/permission.mock.go -typed PermissionEventProducer
type PermissionEventProducer interface {
Produce(ctx context.Context, evt event.PermissionEvent) error
}

type permissionEventProducer struct {
producer mq.Producer
}

func NewPermissionEventProducer(q mq.MQ) (PermissionEventProducer, error) {
producer, err := q.Producer(event.PermissionEventName)
if err != nil {
return nil, err
}
return &permissionEventProducer{
producer: producer,
}, nil
}

func (s *permissionEventProducer) Produce(ctx context.Context, evt event.PermissionEvent) error {
data, err := json.Marshal(&evt)
if err != nil {
return fmt.Errorf("序列化失败: %w", err)
}
_, err = s.producer.Produce(ctx, &mq.Message{
Key: []byte(evt.Key),
Value: data,
})
return err
}
23 changes: 19 additions & 4 deletions internal/marketing/internal/integration/module_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (s *ModuleTestSuite) TestConsumer_ConsumeOrderEvent() {

mockProducer := mocks.NewMockProducer(ctrl)
memberEvent := s.newMemberEventMessage(t, event.MemberEvent{
Key: evt.OrderSN,
Key: fmt.Sprintf("event-key-%s", evt.OrderSN),
Uid: evt.BuyerID,
Days: 14,
Biz: "order",
Expand Down Expand Up @@ -168,7 +168,10 @@ func (s *ModuleTestSuite) TestConsumer_ConsumeOrderEvent() {
memberEventProducer, err := producer.NewMemberEventProducer(q)
require.NoError(t, err)

return service.NewService(mockOrderSvc, memberEventProducer, nil, nil)
eventKeyGenerator := func() string {
return fmt.Sprintf("event-key-%s", evt.OrderSN)
}
return service.NewService(nil, mockOrderSvc, nil, eventKeyGenerator, memberEventProducer, nil, nil)
},
evt: event.OrderEvent{
OrderSN: "OrderSN-marketing-member",
Expand Down Expand Up @@ -271,7 +274,7 @@ func (s *ModuleTestSuite) TestConsumer_ConsumeOrderEvent() {
mockOrderSvc := ordermocks.NewMockService(ctrl)
memberEventProducer, err := producer.NewMemberEventProducer(q)
require.NoError(t, err)
return service.NewService(mockOrderSvc, memberEventProducer, nil, nil)
return service.NewService(nil, mockOrderSvc, nil, nil, memberEventProducer, nil, nil)
},
evt: event.OrderEvent{
OrderSN: "OrderSN-marketing-other",
Expand Down Expand Up @@ -356,7 +359,9 @@ func (s *ModuleTestSuite) TestHandler_ListRedemptionCode() {
name: "获取成功",
newHandlerFunc: func(t *testing.T, ctrl *gomock.Controller) *web.Handler {
t.Helper()
svc := service.NewService(nil, nil, sequencenumber.NewGenerator(), s.repo)

redemptionCodeGenerator := s.getRedemptionCodeGenerator(sequencenumber.NewGenerator())
svc := service.NewService(s.repo, nil, redemptionCodeGenerator, nil, nil, nil, nil)
return web.NewHandler(svc)
},
req: web.ListRedemptionCodesReq{
Expand Down Expand Up @@ -400,6 +405,16 @@ func (s *ModuleTestSuite) TestHandler_ListRedemptionCode() {
}
}

func (s *ModuleTestSuite) getRedemptionCodeGenerator(g *sequencenumber.Generator) func(id int64) string {
redemptionCodeGenerator := func(generator *sequencenumber.Generator) func(id int64) string {
return func(id int64) string {
code, _ := generator.Generate(id)
return code
}
}
return redemptionCodeGenerator(g)
}

func (s *ModuleTestSuite) assertListRedemptionCodesRespEqual(t *testing.T, expected web.ListRedemptionCodesResp, actual web.ListRedemptionCodesResp) {
assert.Equal(t, expected.Total, actual.Total)
assert.Equal(t, len(expected.Codes), len(actual.Codes))
Expand Down
Loading

0 comments on commit fea1a31

Please sign in to comment.