Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handling of nil passed as an arg to a typed interface parameter … #161

Open
wants to merge 3 commits into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ func (h *reflectFunc) fnArgs(msg *Message) ([]reflect.Value, error) {
inType := h.ft.In(inStart + i)

if inType.Kind() == reflect.Interface {
if !v.Type().Implements(inType) {
// If the input value is an untyped nil, simply create a new
// typed nil so that the subsequent .Call() works
if !v.IsValid() {
v = reflect.Zero(inType)
} else if !v.Type().Implements(inType) {
hasWrongType = true
break
}
Expand Down
45 changes: 45 additions & 0 deletions memqueue/memqueue_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,51 @@ var _ = Describe("message with args", func() {
})
})

type testInterface interface {
TestFunction() int
}

type testInterfaceImpl struct {
x int
}

func (t *testInterfaceImpl) TestFunction() int {
return t.x
}

var _ = Describe("message with interface args", func() {
ctx := context.Background()
ch := make(chan bool, 10)

BeforeEach(func() {
q := memqueue.NewQueue(&taskq.QueueOptions{
Name: "test",
Storage: taskq.NewLocalStorage(),
})
expected := 7
task := taskq.RegisterTask(&taskq.TaskOptions{
Name: "test",
Handler: func(notNilArg testInterface, nilArg testInterface) {
Expect(notNilArg).ToNot(BeNil())
Expect(nilArg).To(BeNil())
Expect(notNilArg.TestFunction()).To(Equal(expected))
ch <- true
},
})
notNilInput := testInterface(&testInterfaceImpl{x: expected})
err := q.Add(task.WithArgs(ctx, notNilInput, nil))
Expect(err).NotTo(HaveOccurred())

err = q.Close()
Expect(err).NotTo(HaveOccurred())
})

It("handler is called with args", func() {
Expect(ch).To(Receive())
Expect(ch).NotTo(Receive())
})
})

var _ = Describe("context.Context", func() {
ctx := context.Background()
ch := make(chan bool, 10)
Expand Down