Skip to content

Commit

Permalink
Ensure reentrancy
Browse files Browse the repository at this point in the history
  • Loading branch information
yottahmd committed May 23, 2024
1 parent 6b3dcd0 commit 6d5523e
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
2 changes: 1 addition & 1 deletion command.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func resolveHandler[T Command](op OpType, bus Bus) (HandlerFunc[T], *Mux) {
if n != nil {
h := n.handler.handler
hh := convertInterface[HandlerFunc[T]](h.handler)
storeCache[T](&mx.cache, typ, h.mux, hh)
storeCache[T](mx.cache, typ, h.mux, hh)
return hh, h.mux
}

Expand Down
5 changes: 3 additions & 2 deletions mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type Mux struct {
tree *node
middlewares [mAll][]middleware
mHandlers [mAll]func(ctx Context, fn mHandlerFunc) error
cache syncMap
cache *syncMap

// context pool
pool *sync.Pool
Expand Down Expand Up @@ -68,7 +68,7 @@ func newMux() *Mux {
mux.pool.New = func() interface{} {
return NewContext()
}
mux.cache.kv = make(map[reflect.Type]any)
mux.cache = &syncMap{kv: make(map[reflect.Type]any)}
return mux
}

Expand Down Expand Up @@ -229,6 +229,7 @@ func (mx *Mux) child() Bus {
inline: true,
middlewares: mws,
tree: mx.tree,
cache: mx.cache,
}
}

Expand Down
46 changes: 46 additions & 0 deletions mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,47 @@ func TestMux_QueryAsync_Error(t *testing.T) {
}
}

func TestMux_Reentrant(t *testing.T) {
mux := dew.New()
mux.Register(new(userHandler))
mux.Register(new(postHandler))

type findUserPost struct {
ID int
Result struct {
User string
Post string
}
}

mux.Register(dew.HandlerFunc[findUserPost](
func(ctx context.Context, query *findUserPost) error {
findUserQuery, err := dew.Query(ctx, dew.NewQuery(dew.FromContext(ctx), &findUser{ID: query.ID}))
if err != nil {
return err
}
postQuery, err := dew.Query(ctx, dew.NewQuery(dew.FromContext(ctx), &findPost{ID: query.ID}))
if err != nil {
return err
}
query.Result.User = findUserQuery.Result
query.Result.Post = postQuery.Result
return nil
},
))

query, err := dew.Query(context.Background(), dew.NewQuery(mux, &findUserPost{ID: 1}))
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if query.Result.User != "john" {
t.Fatalf("unexpected result: %s", query.Result.User)
}
if query.Result.Post != "hello" {
t.Fatalf("unexpected result: %s", query.Result.Post)
}
}

type ctxKey struct {
name string
}
Expand Down Expand Up @@ -702,6 +743,11 @@ func (h *postHandler) CreatePost(_ context.Context, command *createPost) error {
return nil
}

func (h *postHandler) FindPost(_ context.Context, query *findPost) error {
query.Result = "hello"
return nil
}

func (*userHandler) FindUser(_ context.Context, query *findUser) error {
if query.ID == 1 {
query.Result = "john"
Expand Down

0 comments on commit 6d5523e

Please sign in to comment.