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

Receive() matcher does not work with "non-blocking" channel send #82

Closed
artemave opened this Issue Feb 11, 2015 · 3 comments

Comments

Projects
None yet
2 participants
@artemave

artemave commented Feb 11, 2015

Was that wrong to expect the following to work?

        It("works for non-blocking send", func() {
            channel := make(chan bool)

            go func() {
                time.Sleep(100 * time.Millisecond)

                // non-blocking send
                select {
                case channel <- true:
                default:
                }
            }()

            Eventually(channel).Should(Receive())
        })
@onsi

This comment has been minimized.

Show comment
Hide comment
@onsi

onsi Feb 11, 2015

Owner

Hi @artemave - your example is not expected to work but the reason is subtle.

The Receive matcher performs a non-blocking read on the channel.
Eventually periodically polls the Receive matcher.

It is very unlikely (in fact, impossible with GOMAXPROCS=1) for your non-blocking send to take place at the same time as the non-blocking read.

Instead you'll need to do something a bit more "traditional" like:

        It("works for non-blocking send", func() {
            channel := make(chan bool)

            go func() {
                time.Sleep(100 * time.Millisecond)

                // non-blocking send
                select {
                case channel <- true:
                default:
                }
            }()

            select {
                case <-channel:
                case <-time.After(time.Second):
                     Fail("timed out waiting to read from channel")
            }
        })
Owner

onsi commented Feb 11, 2015

Hi @artemave - your example is not expected to work but the reason is subtle.

The Receive matcher performs a non-blocking read on the channel.
Eventually periodically polls the Receive matcher.

It is very unlikely (in fact, impossible with GOMAXPROCS=1) for your non-blocking send to take place at the same time as the non-blocking read.

Instead you'll need to do something a bit more "traditional" like:

        It("works for non-blocking send", func() {
            channel := make(chan bool)

            go func() {
                time.Sleep(100 * time.Millisecond)

                // non-blocking send
                select {
                case channel <- true:
                default:
                }
            }()

            select {
                case <-channel:
                case <-time.After(time.Second):
                     Fail("timed out waiting to read from channel")
            }
        })
@artemave

This comment has been minimized.

Show comment
Hide comment
@artemave

artemave Feb 11, 2015

Yep, that is what I ended up doing.

Perhaps it might be worth mentioning this subtlety in the documentation? Because it certainly looked broken on the surface.

artemave commented Feb 11, 2015

Yep, that is what I ended up doing.

Perhaps it might be worth mentioning this subtlety in the documentation? Because it certainly looked broken on the surface.

@onsi

This comment has been minimized.

Show comment
Hide comment
@onsi

onsi Feb 11, 2015

Owner

yes good point. I'll work on that now.

Owner

onsi commented Feb 11, 2015

yes good point. I'll work on that now.

onsi added a commit that referenced this issue Feb 11, 2015

@onsi onsi closed this Feb 11, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment