test(utils): 添加事件驱动模型测试
- 在 sqrt_test.go 中添加了 fastSqr1 测试函数,用于测试事件驱动模型 - 新增了 Event 和 Uint32AsyncEvent 类型用于测试 - 更新了 go.work、go.mod 和
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
package events
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type EventState struct {
|
||||
Ctx context.Context
|
||||
Done chan struct{} `json:"-"`
|
||||
Error error
|
||||
}
|
||||
|
||||
func NewEventState(ctx context.Context) *EventState {
|
||||
return &EventState{
|
||||
Ctx: ctx,
|
||||
Done: make(chan struct{}),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *EventState) Close() {
|
||||
s.Error = s.Ctx.Err()
|
||||
close(s.Done)
|
||||
}
|
||||
|
||||
type NewOrder struct {
|
||||
ID int
|
||||
}
|
||||
|
||||
type CreateOrderEvent struct {
|
||||
NewOrder *NewOrder
|
||||
ProductIDs []int
|
||||
State *EventState
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
package request_reply
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/badu/bus/test_scenarios/request-reply-with-cancellation/orders"
|
||||
)
|
||||
|
||||
func TestRequestReplyWithCancellation(t *testing.T) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
|
||||
svc := orders.NewService()
|
||||
orders.NewRepository()
|
||||
|
||||
response, err := svc.CreateOrder(ctx, []int{1, 2, 3})
|
||||
switch err {
|
||||
default:
|
||||
t.Fatalf("error : it supposed to timeout, but it responded %#v and the error is %#v", response, err)
|
||||
case context.DeadlineExceeded:
|
||||
// what we were expecting
|
||||
}
|
||||
|
||||
cancel()
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/badu/bus"
|
||||
"github.com/badu/bus/test_scenarios/request-reply-with-cancellation/events"
|
||||
)
|
||||
|
||||
type Order struct {
|
||||
ID int
|
||||
ProductIDs []int
|
||||
}
|
||||
|
||||
type RepositoryImpl struct {
|
||||
calls int
|
||||
}
|
||||
|
||||
func NewRepository() RepositoryImpl {
|
||||
result := RepositoryImpl{}
|
||||
bus.Sub(result.OnCreateOrder)
|
||||
return result
|
||||
}
|
||||
|
||||
func (r *RepositoryImpl) OnCreateOrder(event events.CreateOrderEvent) {
|
||||
defer func() {
|
||||
r.calls++
|
||||
}()
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-time.After(4 * time.Second):
|
||||
event.NewOrder = &events.NewOrder{ID: r.calls}
|
||||
event.State.Close()
|
||||
return
|
||||
case <-event.State.Ctx.Done():
|
||||
event.State.Close()
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package orders
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/badu/bus"
|
||||
"github.com/badu/bus/test_scenarios/request-reply-with-cancellation/events"
|
||||
)
|
||||
|
||||
type ServiceImpl struct {
|
||||
}
|
||||
|
||||
func NewService() ServiceImpl {
|
||||
result := ServiceImpl{}
|
||||
return result
|
||||
}
|
||||
|
||||
func (s *ServiceImpl) CreateOrder(ctx context.Context, productIDs []int) (*Order, error) {
|
||||
event := events.CreateOrderEvent{State: events.NewEventState(ctx), ProductIDs: productIDs, NewOrder: &events.NewOrder{}}
|
||||
bus.Pub(event)
|
||||
<-event.State.Done
|
||||
|
||||
if event.NewOrder != nil && event.State.Error == nil {
|
||||
return &Order{ID: event.NewOrder.ID, ProductIDs: productIDs}, nil
|
||||
}
|
||||
|
||||
return nil, event.State.Error
|
||||
}
|
||||
Reference in New Issue
Block a user