test(utils): 添加事件驱动模型测试
- 在 sqrt_test.go 中添加了 fastSqr1 测试函数,用于测试事件驱动模型 - 新增了 Event 和 Uint32AsyncEvent 类型用于测试 - 更新了 go.work、go.mod 和
This commit is contained in:
@@ -0,0 +1,35 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/badu/bus"
|
||||
"github.com/badu/bus/test_scenarios/request-reply-callback/events"
|
||||
)
|
||||
|
||||
type ServiceImpl struct {
|
||||
sb *strings.Builder
|
||||
}
|
||||
|
||||
func NewService(sb *strings.Builder) ServiceImpl {
|
||||
result := ServiceImpl{sb: sb}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) RegisterOrder(ctx context.Context, productIDs []int) (*Order, error) {
|
||||
event := events.NewRequestEvent[Order](Order{ProductIDs: productIDs})
|
||||
s.sb.WriteString(fmt.Sprintf("dispatching event typed %T\n", event))
|
||||
bus.Pub(event)
|
||||
<-event.Done // wait for "reply"
|
||||
return event.Callback() // return the callback, which is containing the actual result
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) GetOrderStatus(ctx context.Context, orderID int) (*OrderStatus, error) {
|
||||
event := events.NewRequestEvent[OrderStatus](OrderStatus{OrderID: orderID})
|
||||
s.sb.WriteString(fmt.Sprintf("dispatching event typed %T\n", event))
|
||||
bus.Pub(event)
|
||||
<-event.Done // wait for "reply"
|
||||
return event.Callback() // return the callback, which is containing the actual result
|
||||
}
|
||||
Reference in New Issue
Block a user