예제 #1
0
func createPushSocket() (*zmq4.Socket, error) {
	var err error
	var pushSocket *zmq4.Socket

	if pushSocket, err = zmq4.NewSocket(zmq4.PUSH); err != nil {
		return nil, fmt.Errorf("NewSocket %s", err)
	}

	if err = pushSocket.SetSndhwm(pushSocketSendHWM); err != nil {
		return nil, fmt.Errorf("pushSocket.SetSndhwm(%d) %s",
			pushSocketSendHWM, err)
	}

	return pushSocket, nil
}
예제 #2
0
파일: main.go 프로젝트: HackLinux/nimbus.io
func createWriterSocket() (*zmq4.Socket, error) {
	var err error
	var writerSocket *zmq4.Socket

	if writerSocket, err = zmq4.NewSocket(zmq4.REP); err != nil {
		return nil, fmt.Errorf("NewSocket %s", err)
	}

	if err = writerSocket.SetRcvhwm(writerSocketReceiveHWM); err != nil {
		return nil, fmt.Errorf("writerSocket.SetRcvhwm(%d) %s",
			writerSocketReceiveHWM, err)
	}

	if err = writerSocket.SetSndhwm(writerSocketSendHWM); err != nil {
		return nil, fmt.Errorf("writerSocket.SetSndhwm(%d) %s",
			writerSocketSendHWM, err)
	}

	return writerSocket, nil
}
예제 #3
0
파일: zmq4_test.go 프로젝트: pebbe/zmq4
func TestHwm(t *testing.T) {

	MAX_SENDS := 10000
	BIND_FIRST := 1
	CONNECT_FIRST := 2

	test_defaults := func() (result int) {

		result = -1

		// Set up bind socket
		bind_socket, err := zmq.NewSocket(zmq.PULL)
		if err != nil {
			t.Error("NewSocket:", err)
			return
		}
		defer func() {
			err := bind_socket.Close()
			if err != nil {
				t.Error("bind_socket.Close:", err)
			}
		}()

		err = bind_socket.Bind("inproc://a")
		if err != nil {
			t.Error("bind_socket.Bind:", err)
			return
		}

		// Set up connect socket
		connect_socket, err := zmq.NewSocket(zmq.PUSH)
		if err != nil {
			t.Error("NewSocket:", err)
			return
		}
		defer func() {
			err := connect_socket.Close()
			if err != nil {
				t.Error("connect_socket.Close:", err)
			}
		}()

		err = connect_socket.Connect("inproc://a")
		if err != nil {
			t.Error("connect_socket.Connect:", err)
			return
		}

		// Send until we block
		send_count := 0
		for send_count < MAX_SENDS {
			_, err := connect_socket.Send("", zmq.DONTWAIT)
			if err != nil {
				break
			}
			send_count++
		}

		// Now receive all sent messages
		recv_count := 0
		for {
			_, err := bind_socket.Recv(zmq.DONTWAIT)
			if err != nil {
				break
			}
			recv_count++
		}
		if send_count != recv_count {
			t.Error("test_defaults: send_count == recv_count")
		}

		return send_count
	}

	count_msg := func(send_hwm, recv_hwm, testType int) (result int) {

		result = -1

		var bind_socket, connect_socket *zmq.Socket
		var err error

		if testType == BIND_FIRST {
			// Set up bind socket
			bind_socket, err = zmq.NewSocket(zmq.PULL)
			if err != nil {
				t.Error("NewSocket:", err)
				return
			}
			defer func() {
				err := bind_socket.Close()
				if err != nil {
					t.Error("bind_socket.Close:", err)
				}
			}()

			err = bind_socket.SetRcvhwm(recv_hwm)
			if err != nil {
				t.Error("bind_socket.SetRcvhwm:", err)
				return
			}

			err = bind_socket.Bind("inproc://a")
			if err != nil {
				t.Error("bind_socket.Bind:", err)
				return
			}

			// Set up connect socket
			connect_socket, err = zmq.NewSocket(zmq.PUSH)
			if err != nil {
				t.Error("NewSocket:", err)
				return
			}
			defer func() {
				err := connect_socket.Close()
				if err != nil {
					t.Error(err)
				}
			}()

			err = connect_socket.SetSndhwm(send_hwm)
			if err != nil {
				t.Error("connect_socket.SetSndhwm:", err)
				return
			}

			err = connect_socket.Connect("inproc://a")
			if err != nil {
				t.Error("connect_socket.Connect:", err)
				return
			}
		} else {
			// Set up connect socket
			connect_socket, err = zmq.NewSocket(zmq.PUSH)
			if err != nil {
				t.Error("NewSocket:", err)
				return
			}
			defer func() {
				err := connect_socket.Close()
				if err != nil {
					t.Error("connect_socket.Close:", err)
				}
			}()

			err = connect_socket.SetSndhwm(send_hwm)
			if err != nil {
				t.Error("connect_socket.SetSndhwm:", err)
				return
			}

			err = connect_socket.Connect("inproc://a")
			if err != nil {
				t.Error("connect_socket.Connect:", err)
				return
			}

			// Set up bind socket
			bind_socket, err = zmq.NewSocket(zmq.PULL)
			if err != nil {
				t.Error("NewSocket:", err)
				return
			}
			defer func() {
				err := bind_socket.Close()
				if err != nil {
					t.Error("bind_socket.Close:", err)
				}
			}()

			err = bind_socket.SetRcvhwm(recv_hwm)
			if err != nil {
				t.Error("bind_socket.SetRcvhwm:", err)
				return
			}

			err = bind_socket.Bind("inproc://a")
			if err != nil {
				t.Error("bind_socket.Bind:", err)
				return
			}
		}

		// Send until we block
		send_count := 0
		for send_count < MAX_SENDS {
			_, err := connect_socket.Send("", zmq.DONTWAIT)
			if err != nil {
				break
			}
			send_count++
		}

		// Now receive all sent messages
		recv_count := 0
		for {
			_, err := bind_socket.Recv(zmq.DONTWAIT)
			if err != nil {
				break
			}
			recv_count++
		}
		if send_count != recv_count {
			t.Error("count_msg: send_count != recv_count")
		}

		// Now it should be possible to send one more.
		_, err = connect_socket.Send("", 0)
		if err != nil {
			t.Error("connect_socket.Send:", err)
			return
		}

		//  Consume the remaining message.
		_, err = bind_socket.Recv(0)
		if err != nil {
			t.Error("bind_socket.Recv:", err)
		}

		return send_count
	}

	test_inproc_bind_first := func(send_hwm, recv_hwm int) int {
		return count_msg(send_hwm, recv_hwm, BIND_FIRST)
	}

	test_inproc_connect_first := func(send_hwm, recv_hwm int) int {
		return count_msg(send_hwm, recv_hwm, CONNECT_FIRST)
	}

	test_inproc_connect_and_close_first := func(send_hwm, recv_hwm int) (result int) {

		result = -1

		// Set up connect socket
		connect_socket, err := zmq.NewSocket(zmq.PUSH)
		if err != nil {
			t.Error("NewSocket:", err)
			return
		}
		defer func() {
			if connect_socket != nil {
				connect_socket.Close()
			}
		}()

		err = connect_socket.SetSndhwm(send_hwm)
		if err != nil {
			t.Error("connect_socket.SetSndhwm:", err)
			return
		}

		err = connect_socket.Connect("inproc://a")
		if err != nil {
			t.Error("connect_socket.Connect:", err)
			return
		}

		// Send until we block
		send_count := 0
		for send_count < MAX_SENDS {
			_, err := connect_socket.Send("", zmq.DONTWAIT)
			if err != nil {
				break
			}
			send_count++
		}

		// Close connect
		err = connect_socket.Close()
		connect_socket = nil
		if err != nil {
			t.Error("connect_socket.Close:", err)
			return
		}

		// Set up bind socket
		bind_socket, err := zmq.NewSocket(zmq.PULL)
		if err != nil {
			t.Error("NewSocket:", err)
			return
		}
		defer func() {
			err := bind_socket.Close()
			if err != nil {
				t.Error("bind_socket.Close:", err)
			}
		}()

		err = bind_socket.SetRcvhwm(recv_hwm)
		if err != nil {
			t.Error("bind_socket.SetRcvhwm:", err)
			return
		}

		err = bind_socket.Bind("inproc://a")
		if err != nil {
			t.Error("bind_socket.Bind:", err)
			return
		}

		// Now receive all sent messages
		recv_count := 0
		for {
			_, err := bind_socket.Recv(zmq.DONTWAIT)
			if err != nil {
				break
			}
			recv_count++
		}
		if send_count != recv_count {
			t.Error("test_inproc_connect_and_close_first: send_count != recv_count")
		}
		return send_count
	}

	// Default values are 1000 on send and 1000 one receive, so 2000 total
	if count := test_defaults(); count != 2000 {
		t.Errorf("test_defaults: expected 2000, got %d", count)
	}
	time.Sleep(100 * time.Millisecond)

	// Infinite send and receive buffer
	if count := test_inproc_bind_first(0, 0); count != MAX_SENDS {
		t.Errorf("test_inproc_bind_first(0, 0): expected %d, got %d", MAX_SENDS, count)
	}
	time.Sleep(100 * time.Millisecond)
	if count := test_inproc_connect_first(0, 0); count != MAX_SENDS {
		t.Errorf("test_inproc_connect_first(0, 0): expected %d, got %d", MAX_SENDS, count)
	}
	time.Sleep(100 * time.Millisecond)

	// Infinite send buffer
	if count := test_inproc_bind_first(1, 0); count != MAX_SENDS {
		t.Errorf("test_inproc_bind_first(1, 0): expected %d, got %d", MAX_SENDS, count)
	}
	time.Sleep(100 * time.Millisecond)
	if count := test_inproc_connect_first(1, 0); count != MAX_SENDS {
		t.Errorf("test_inproc_connect_first(1, 0): expected %d, got %d", MAX_SENDS, count)
	}
	time.Sleep(100 * time.Millisecond)

	// Infinite receive buffer
	if count := test_inproc_bind_first(0, 1); count != MAX_SENDS {
		t.Errorf("test_inproc_bind_first(0, 1): expected %d, got %d", MAX_SENDS, count)
	}
	time.Sleep(100 * time.Millisecond)
	if count := test_inproc_connect_first(0, 1); count != MAX_SENDS {
		t.Errorf("test_inproc_connect_first(0, 1): expected %d, got %d", MAX_SENDS, count)
	}
	time.Sleep(100 * time.Millisecond)

	// Send and recv buffers hwm 1, so total that can be queued is 2
	if count := test_inproc_bind_first(1, 1); count != 2 {
		t.Errorf("test_inproc_bind_first(1, 1): expected 2, got %d", count)
	}
	time.Sleep(100 * time.Millisecond)
	if count := test_inproc_connect_first(1, 1); count != 2 {
		t.Errorf("test_inproc_connect_first(1, 1): expected 2, got %d", count)
	}
	time.Sleep(100 * time.Millisecond)

	// Send hwm of 1, send before bind so total that can be queued is 1
	if count := test_inproc_connect_and_close_first(1, 0); count != 1 {
		t.Errorf("test_inproc_connect_and_close_first(1, 0): expected 1, got %d", count)
	}
	time.Sleep(100 * time.Millisecond)
}
예제 #4
0
파일: zmq4_test.go 프로젝트: rdterner/zmq4
func Example_test_hwm() {

	MAX_SENDS := 10000
	BIND_FIRST := 1
	CONNECT_FIRST := 2

	test_defaults := func() int {

		// Set up bind socket
		bind_socket, err := zmq.NewSocket(zmq.PULL)
		if checkErr(err) {
			return 0
		}
		defer func() {
			err := bind_socket.Close()
			checkErr(err)
		}()

		err = bind_socket.Bind("inproc://a")
		if checkErr(err) {
			return 0
		}

		// Set up connect socket
		connect_socket, err := zmq.NewSocket(zmq.PUSH)
		if checkErr(err) {
			return 0
		}
		defer func() {
			err := connect_socket.Close()
			checkErr(err)
		}()

		err = connect_socket.Connect("inproc://a")
		if checkErr(err) {
			return 0
		}

		// Send until we block
		send_count := 0
		for send_count < MAX_SENDS {
			_, err := connect_socket.Send("", zmq.DONTWAIT)
			if err != nil {
				break
			}
			send_count++
		}

		// Now receive all sent messages
		recv_count := 0
		for {
			_, err := bind_socket.Recv(zmq.DONTWAIT)
			if err != nil {
				break
			}
			recv_count++
		}
		fmt.Println("send_count == recv_count:", send_count == recv_count)

		return send_count
	}

	count_msg := func(send_hwm, recv_hwm, testType int) int {

		var bind_socket, connect_socket *zmq.Socket
		var err error

		if testType == BIND_FIRST {
			// Set up bind socket
			bind_socket, err = zmq.NewSocket(zmq.PULL)
			if checkErr(err) {
				return 0
			}
			defer func() {
				err := bind_socket.Close()
				checkErr(err)
			}()

			err = bind_socket.SetRcvhwm(recv_hwm)
			if checkErr(err) {
				return 0
			}

			err = bind_socket.Bind("inproc://a")
			if checkErr(err) {
				return 0
			}

			// Set up connect socket
			connect_socket, err = zmq.NewSocket(zmq.PUSH)
			if checkErr(err) {
				return 0
			}
			defer func() {
				err := connect_socket.Close()
				checkErr(err)
			}()

			err = connect_socket.SetSndhwm(send_hwm)
			if checkErr(err) {
				return 0
			}

			err = connect_socket.Connect("inproc://a")
			if checkErr(err) {
				return 0
			}
		} else {
			// Set up connect socket
			connect_socket, err = zmq.NewSocket(zmq.PUSH)
			if checkErr(err) {
				return 0
			}
			defer func() {
				err := connect_socket.Close()
				checkErr(err)
			}()

			err = connect_socket.SetSndhwm(send_hwm)
			if checkErr(err) {
				return 0
			}

			err = connect_socket.Connect("inproc://a")
			if checkErr(err) {
				return 0
			}

			// Set up bind socket
			bind_socket, err = zmq.NewSocket(zmq.PULL)
			if checkErr(err) {
				return 0
			}
			defer func() {
				err := bind_socket.Close()
				checkErr(err)
			}()

			err = bind_socket.SetRcvhwm(recv_hwm)
			if checkErr(err) {
				return 0
			}

			err = bind_socket.Bind("inproc://a")
			if checkErr(err) {
				return 0
			}
		}

		// Send until we block
		send_count := 0
		for send_count < MAX_SENDS {
			_, err := connect_socket.Send("", zmq.DONTWAIT)
			if err != nil {
				break
			}
			send_count++
		}

		// Now receive all sent messages
		recv_count := 0
		for {
			_, err := bind_socket.Recv(zmq.DONTWAIT)
			if err != nil {
				break
			}
			recv_count++
		}
		fmt.Println("send_count == recv_count:", send_count == recv_count)

		// Now it should be possible to send one more.
		_, err = connect_socket.Send("", 0)
		if checkErr(err) {
			return 0
		}

		//  Consume the remaining message.
		_, err = bind_socket.Recv(0)
		checkErr(err)

		return send_count
	}

	test_inproc_bind_first := func(send_hwm, recv_hwm int) int {
		return count_msg(send_hwm, recv_hwm, BIND_FIRST)
	}

	test_inproc_connect_first := func(send_hwm, recv_hwm int) int {
		return count_msg(send_hwm, recv_hwm, CONNECT_FIRST)
	}

	test_inproc_connect_and_close_first := func(send_hwm, recv_hwm int) int {

		// Set up connect socket
		connect_socket, err := zmq.NewSocket(zmq.PUSH)
		if checkErr(err) {
			return 0
		}

		err = connect_socket.SetSndhwm(send_hwm)
		if checkErr(err) {
			connect_socket.Close()
			return 0
		}

		err = connect_socket.Connect("inproc://a")
		if checkErr(err) {
			connect_socket.Close()
			return 0
		}

		// Send until we block
		send_count := 0
		for send_count < MAX_SENDS {
			_, err := connect_socket.Send("", zmq.DONTWAIT)
			if err != nil {
				break
			}
			send_count++
		}

		// Close connect
		err = connect_socket.Close()
		if checkErr(err) {
			return 0
		}

		// Set up bind socket
		bind_socket, err := zmq.NewSocket(zmq.PULL)
		if checkErr(err) {
			return 0
		}
		defer func() {
			err := bind_socket.Close()
			checkErr(err)
		}()

		err = bind_socket.SetRcvhwm(recv_hwm)
		if checkErr(err) {
			return 0
		}

		err = bind_socket.Bind("inproc://a")
		if checkErr(err) {
			return 0
		}

		// Now receive all sent messages
		recv_count := 0
		for {
			_, err := bind_socket.Recv(zmq.DONTWAIT)
			if err != nil {
				break
			}
			recv_count++
		}
		fmt.Println("send_count == recv_count:", send_count == recv_count)

		return send_count
	}

	// Default values are 1000 on send and 1000 one receive, so 2000 total
	fmt.Println("Default values")
	count := test_defaults()
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)

	// Infinite send and receive buffer
	fmt.Println("\nInfinite send and receive")
	count = test_inproc_bind_first(0, 0)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)
	count = test_inproc_connect_first(0, 0)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)

	// Infinite send buffer
	fmt.Println("\nInfinite send buffer")
	count = test_inproc_bind_first(1, 0)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)
	count = test_inproc_connect_first(1, 0)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)

	// Infinite receive buffer
	fmt.Println("\nInfinite receive buffer")
	count = test_inproc_bind_first(0, 1)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)
	count = test_inproc_connect_first(0, 1)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)

	// Send and recv buffers hwm 1, so total that can be queued is 2
	fmt.Println("\nSend and recv buffers hwm 1")
	count = test_inproc_bind_first(1, 1)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)
	count = test_inproc_connect_first(1, 1)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)

	// Send hwm of 1, send before bind so total that can be queued is 1
	fmt.Println("\nSend hwm of 1, send before bind")
	count = test_inproc_connect_and_close_first(1, 0)
	fmt.Println("count:", count)
	time.Sleep(100 * time.Millisecond)

	fmt.Println("\nDone")
	// Output:
	// Default values
	// send_count == recv_count: true
	// count: 2000
	//
	// Infinite send and receive
	// send_count == recv_count: true
	// count: 10000
	// send_count == recv_count: true
	// count: 10000
	//
	// Infinite send buffer
	// send_count == recv_count: true
	// count: 10000
	// send_count == recv_count: true
	// count: 10000
	//
	// Infinite receive buffer
	// send_count == recv_count: true
	// count: 10000
	// send_count == recv_count: true
	// count: 10000
	//
	// Send and recv buffers hwm 1
	// send_count == recv_count: true
	// count: 2
	// send_count == recv_count: true
	// count: 2
	//
	// Send hwm of 1, send before bind
	// send_count == recv_count: true
	// count: 1
	//
	// Done
}