Cleaned up formatting of test_connect_rid. Set LINGER to 0 on sockets. This may address the test failing on some devices.

This commit is contained in:
Tim M 2014-01-21 11:24:39 -08:00
parent 2d6d8af0b8
commit 141e1b5966

View File

@ -1,4 +1,3 @@
/*
Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
@ -21,84 +20,130 @@
#include "testutil.hpp"
void test_stream_2_stream(void* ctx){
void test_stream_2_stream(void* ctx_){
void *rbind, *rconn1;
int ret;
char buff[256];
char msg[] = "hi 1";
const char *bindip = "tcp://127.0.0.1:12001";
rbind = zmq_socket(ctx,ZMQ_STREAM);
rconn1 = zmq_socket(ctx,ZMQ_STREAM);
assert(rbind && rconn1 );
const char *bindip = "tcp://127.0.0.1:5556";
int zero = 0;
// Set up listener STREAM.
rbind = zmq_socket (ctx_, ZMQ_STREAM);
assert (rbind);
ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
assert (0 == ret);
ret = zmq_bind (rbind, bindip);
assert(0 == ret);
// Set up connection stream.
rconn1 = zmq_socket (ctx_, ZMQ_STREAM);
assert (rconn1);
ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
assert (0 == ret);
// Do the connection.
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
assert (0 == ret);
ret = zmq_connect (rconn1, bindip);
/*test duplicate connect attempt*/
// Test duplicate connect attempt.
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
assert (0 == ret);
ret = zmq_connect (rconn1, bindip);
assert (0 == ret);
// Send data to the bound stream.
ret = zmq_send (rconn1, "conn1", 6, ZMQ_SNDMORE);
assert (6 == ret);
ret = zmq_send (rconn1, msg, 5, 0);
assert (5 == ret);
// Accept data on the bound stream.
ret = zmq_recv (rbind, buff, 256, 0);
assert (ret && 0 == buff[0]);
assert (0 == buff[0]);
ret = zmq_recv (rbind, buff, 256, 0);
assert (0 == ret);
// close the duplicate socket
// Close the duplicate socket.
ret = zmq_recv (rbind, buff, 256, 0);
assert(ret && 0 == buff[0]);
assert (ret);
assert (0 == buff[0]);
ret = zmq_recv (rbind, buff+128, 128, 0);
assert (0 == ret);
// handle the good socket
// Handle close of the socket.
ret = zmq_recv (rbind, buff, 256, 0);
assert(ret && 0 == buff[0]);
assert (ret);
assert (0 == buff[0]);
ret = zmq_recv (rbind, buff+128, 128, 0);
assert(5 == ret && 'h' == buff[128] );
assert (5 == ret);
assert ('h' == buff[128]);
zmq_unbind (rbind, bindip);
zmq_close (rbind);
zmq_close (rconn1);
}
void test_router_2_router(void* ctx,bool named){
void *rbind, *rconn1;
int ret;
char buff[256];
char msg[] = "hi 1";
const char *bindip = "tcp://127.0.0.1:12001";
const char *bindip = "tcp://127.0.0.1:5556";
int zero = 0;
// Create bind socket.
rbind = zmq_socket (ctx, ZMQ_ROUTER);
rconn1 = zmq_socket(ctx,ZMQ_ROUTER);
assert(rbind && rconn1 );
assert (rbind);
ret = zmq_setsockopt (rbind, ZMQ_LINGER, &zero, sizeof (zero));
assert (0 == ret);
ret = zmq_bind (rbind, bindip);
assert (0 == ret);
if(named){/*here we check if this interferes with bound socket naming */
// Create connection socket.
rconn1 = zmq_socket (ctx, ZMQ_ROUTER);
assert (rconn1);
ret = zmq_setsockopt (rconn1, ZMQ_LINGER, &zero, sizeof (zero));
assert (0 == ret);
// If we're in named mode, set some identities.
if (named) {
ret = zmq_setsockopt (rbind, ZMQ_IDENTITY, "X", 1);
ret = zmq_setsockopt (rconn1, ZMQ_IDENTITY, "Y", 1);
}
// Make call to connect using a connect_rid.
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
assert (0 == ret);
ret = zmq_connect (rconn1, bindip);
assert (0 == ret);
/*test duplicate connect attempt*/
// Test duplicate connect attempt.
ret = zmq_setsockopt (rconn1, ZMQ_CONNECT_RID, "conn1", 6);
assert (0 == ret);
ret = zmq_connect (rconn1, bindip);
assert (0 == ret);
// Send some data.
ret = zmq_send (rconn1, "conn1", 6, ZMQ_SNDMORE);
assert (6 == ret);
ret = zmq_send (rconn1, msg, 5, 0);
assert (5 == ret);
// Receive the name.
ret = zmq_recv (rbind, buff, 256, 0);
if(named) assert(ret && 'Y' == buff[0]);
else assert(ret && 0 == buff[0]);
if (named)
assert (ret && 'Y' == buff[0]);
else
assert (ret && 0 == buff[0]);
// Receive the data.
ret = zmq_recv (rbind, buff+128, 128, 0);
assert(5 == ret && 'h' == buff[128]);
// Send some data back.
if (named) {
ret = zmq_send (rbind, buff, 1, ZMQ_SNDMORE);
assert (1 == ret);
@ -110,15 +155,18 @@ void test_router_2_router(void* ctx,bool named){
ret = zmq_send_const (rbind, "ok", 3, 0);
assert (3 == ret);
/*if bound socket identity naming a problem, we'll likely see something funky here */
// If bound socket identity naming a problem, we'll likely see something funky here.
ret = zmq_recv (rconn1, buff, 256, 0);
assert ('c' == buff[0] && 6 == ret);
ret = zmq_recv (rconn1, buff+128, 128, 0);
assert (3 == ret && 'o' == buff[128]);
zmq_unbind(rbind,bindip);
zmq_close(rbind);
zmq_close(rconn1);
ret = zmq_unbind (rbind, bindip);
assert(0 == ret);
ret = zmq_close (rbind);
assert(0 == ret);
ret = zmq_close (rconn1);
assert(0 == ret);
}
int main (void)