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

poller.Wait unexpected behaviour #277

Open
braatvedt opened this issue Apr 29, 2020 · 0 comments
Open

poller.Wait unexpected behaviour #277

braatvedt opened this issue Apr 29, 2020 · 0 comments

Comments

@braatvedt
Copy link

I've created a small test to illustrate this, but in summary I have a poller.Wait(5000) on a dealer socket, yes 5 seconds. And a whole lot of responses queued up waiting to be read off. Now I'd expect the messages to all be read off and then when there's been no new messages for 5 seconds the socket returned from the poller.Wait(5000) would be nil. I seem to get a few nil sockets whilst reading the messages, and within 5 seconds of each other.

Code here:

package main

import (
	"fmt"
	"github.com/zeromq/goczmq"
	"testing"
	"time"
)

func TestWaitNotWaiting(t *testing.T) {
	// create an echo router
	router, err := goczmq.NewRouter("tcp://127.0.0.1:9999")
	if err != nil {
		t.Fatal(err)
	}
	defer router.Destroy()

	// echo all messages received by router back to dealer
	go func() {
		poller, err := goczmq.NewPoller(router)
		if err != nil {
			t.Fatal(err)
		}

		for {
			sock := poller.Wait(100)
			if sock != nil {
				req, err := sock.RecvMessage()
				if err != nil {
					t.Error("error getting message", err)
				}
				err = sock.SendMessage(req)
				if err != nil {
					t.Error("error sending message", err)
				}
			}
		}
	}()

	dealer, err := goczmq.NewDealer("tcp://127.0.0.1:9999")
	if err != nil {
		t.Fatal(err)
	}
	defer dealer.Destroy()

	for i := 0; i < 100000; i++ {
		err := dealer.SendMessage([][]byte{
			[]byte(string(i)),
		})
		if err != nil {
			t.Fatal("error sending message", err)
		}
	}

	poller, err := goczmq.NewPoller(dealer)
	if err != nil {
		t.Fatal("error setting up poller", err)
	}

	// now for the fun
	for {
		sock := poller.Wait(5000) // wait 5 seconds, this is long for illustration purposes.
		if sock != nil {
			_, err := sock.RecvMessage()
			if err != nil {
				t.Fatal(err)
			}
		} else {
			fmt.Println("Time", time.Now())
		}
	}
}

and sample output, first 3 calls very close together and then the wait time appears to be working when nothing is on the queue anymore:

=== RUN   TestWaitNotWaiting
Time 2020-04-29 12:58:07.837648709 +0200 SAST m=+0.254047379
Time 2020-04-29 12:58:07.977043834 +0200 SAST m=+0.393442503
Time 2020-04-29 12:58:08.001565177 +0200 SAST m=+0.417963859
Time 2020-04-29 12:58:13.006290839 +0200 SAST m=+5.422689588
Time 2020-04-29 12:58:18.011217611 +0200 SAST m=+10.427616376
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant