Skip to content

Commit

Permalink
Added basic zmq_log tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pjkundert committed Oct 13, 2011
1 parent 567c0fb commit 429006a
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 60 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
*.pyc
*~
test_subs
test_log
test_main
29 changes: 22 additions & 7 deletions test/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -111,15 +111,30 @@ ifeq ($(ARCH),hppa)
CXXFLAGS += -mt
endif

all: test_subs
CXXFLAGS += -I.
CXXFLAGS += -I../../zeromq3-0/include -I../../cut
CXXFLAGS += -I../../cut
LDFLAGS += -L.
LDFLAGS += -L../../zeromq3-0/src/.libs
#LDFLAGS += -static
LOADLIBES += -lzmq -luuid -lpthread

CXXFLAGS += -I. -I../../zeromq3-0/include -I../../cut
LDFLAGS += -L. -L../../zeromq3-0/src/.libs -static
LOADLIBES += -lzmq
all: test

test: test_main
./test_main

# Compile and test all unit tests in a single executable.
test_main: test_main.C test_subs.C test_log.C

# Compile and test each unit test module separately
test-standalone: test_subs test_log
./test_subs
./test_log

test_subs: CXXFLAGS += -DTESTSTANDALONE
test_subs: LOADLIBES += -lzmq -luuid -lpthread
test_subs: test_subs.C

test: all
./test_subs
test_log: CXXFLAGS += -DTESTSTANDALONE
test_log: test_log.C

60 changes: 60 additions & 0 deletions test/test_log.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <cut>

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "zmq.h"
#include "zmq_utils.h"

#if defined( TESTSTANDALONE )
# include "test_main.C"
#endif // TESTSTANDALONE

namespace cut {
static force force_log;
CUT( root, log, "log" ) {
void *sock[10];
int socknum = 0;

void *ctx = zmq_init (1);
assert.ISTRUE (ctx);

// Create a subscriber.
void *sub = zmq_socket (ctx, ZMQ_SUB);
assert.ISTRUE (sub);
sock[socknum++] = sub;
int rc = zmq_connect (sub, "sys://log");
assert.ISEQUAL (rc, 0);

// Subscribe for all messages.
rc = zmq_setsockopt (sub, ZMQ_SUBSCRIBE, "", 0);
assert.ISEQUAL (rc, 0);

int timeout = 250;
rc = zmq_setsockopt (sub, ZMQ_RCVTIMEO, &timeout, sizeof timeout);
assert.ISEQUAL (rc, 0);

char buff [32];
rc = zmq_recv (sub, buff, sizeof (buff), 0);
assert.ISEQUAL (rc, -1);

rc = zmq_log (ctx, "Hello, %s!", "World");
assert.ISEQUAL (rc, 0);

rc = zmq_recv (sub, buff, sizeof (buff), 0);
assert.ISEQUAL (rc, 14);
if (rc >0)
printf("sub sys://log received: %s\n", buff);

// Clean up.
for (int i = 0
; i < socknum
; ++i) {
rc = zmq_close (sock[i]);
assert.ISEQUAL (rc, 0);
}
rc = zmq_term (ctx);
assert.ISEQUAL (rc, 0);
}
}
27 changes: 27 additions & 0 deletions test/test_main.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <cut>
#include <stdlib.h>

namespace cut {
test root( "0MQ Unit Tests" );
}

int
main( int, char ** )
{
bool success;
if ( getenv( "REQUEST_METHOD" )) {
//
// Lets run our tests with CGI HTML output:
//
// target sparse flat cgi
// ------ ------ ---- ---
success = cut::htmlrunner( std::cout, false, true, true ).run();
} else {
//
// But, here's the (simpler) textual output:
//
success = cut::runner( std::cout ).run();
}

return ! success;
}
61 changes: 8 additions & 53 deletions test/test_subs.C
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
/*
Copyright (c) 2007-2011 iMatix Corporation
Copyright (c) 2007-2011 Other contributors as noted in the AUTHORS file
This file is part of 0MQ.
0MQ is free software; you can redistribute it and/or modify it under
the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
0MQ is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include <cut>

#include <stdio.h>
Expand All @@ -27,11 +7,12 @@
#include "zmq.h"
#include "zmq_utils.h"

namespace cut {

#if defined( TESTSTANDALONE )
test root( "0MQ Unit Tests -- subs" );
#endif
# include "test_main.C"
#endif // TESTSTANDALONE

namespace cut {
static force force_subs;
CUT( root, subs, "subs" ) {

void *sock[10];
Expand Down Expand Up @@ -67,8 +48,8 @@ namespace cut {
rc = zmq_connect (sub, "tcp://127.0.0.1:5560");
assert.ISEQUAL (rc, 0);

// Confirm no subs yet, either in general (using zmq_subs), or specifically
// (using getsockopt w/ ZMQ_SUBSCRIBE)
// Confirm no subs yet, either in general (using zmq_subs), or
// specifically (using getsockopt w/ ZMQ_SUBSCRIBE)
rc = zmq_subs (pub, "", 0);
assert.ISEQUAL (rc, 0);
unsigned char term[10];
Expand Down Expand Up @@ -197,8 +178,7 @@ namespace cut {
++msgs;
}

// We should only see 2 upstream subscription, because the "B" subset of
// "BOO" is transmitted, and it satisfies both.
// We should only see 2 upstream subscriptions; "B" and "BOO"
assert.ISEQUAL (msgs, 2);

// No new subscriptions activated 'til we publish something...
Expand Down Expand Up @@ -447,28 +427,3 @@ namespace cut {
assert.ISEQUAL (rc, 0);
}
}

#if defined( TESTSTANDALONE )

int
main( int, char ** )
{
bool success;
if ( getenv( "REQUEST_METHOD" )) {
//
// Lets run our tests with CGI HTML output:
//
// target sparse flat cgi
// ------ ------ ---- ---
success = cut::htmlrunner( std::cout, false, true, true ).run();
} else {
//
// But, here's the (simpler) textual output:
//
success = cut::runner( std::cout ).run();
}

return ! success;
}

#endif // TESTSTANDALONE

0 comments on commit 429006a

Please sign in to comment.