![]() |
libzmq master
The Intelligent Transport Layer
|
00001 /* 00002 Copyright (c) 2010-2011 250bpm s.r.o. 00003 Copyright (c) 2010-2011 Other contributors as noted in the AUTHORS file 00004 00005 This file is part of 0MQ. 00006 00007 0MQ is free software; you can redistribute it and/or modify it under 00008 the terms of the GNU Lesser General Public License as published by 00009 the Free Software Foundation; either version 3 of the License, or 00010 (at your option) any later version. 00011 00012 0MQ is distributed in the hope that it will be useful, 00013 but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 GNU Lesser General Public License for more details. 00016 00017 You should have received a copy of the GNU Lesser General Public License 00018 along with this program. If not, see <http://www.gnu.org/licenses/>. 00019 */ 00020 00021 00022 #include <assert.h> 00023 #include <stdio.h> 00024 00025 #include "testutil.hpp" 00026 00027 int main (int argc, char *argv []) 00028 { 00029 fprintf (stderr, "test_hwm running...\n"); 00030 00031 void *ctx = zmq_init (1); 00032 assert (ctx); 00033 00034 // Create pair of socket, each with high watermark of 2. Thus the total 00035 // buffer space should be 4 messages. 00036 void *sb = zmq_socket (ctx, ZMQ_PULL); 00037 assert (sb); 00038 int hwm = 2; 00039 int rc = zmq_setsockopt (sb, ZMQ_RCVHWM, &hwm, sizeof (hwm)); 00040 assert (rc == 0); 00041 rc = zmq_bind (sb, "inproc://a"); 00042 assert (rc == 0); 00043 00044 void *sc = zmq_socket (ctx, ZMQ_PUSH); 00045 assert (sc); 00046 rc = zmq_setsockopt (sc, ZMQ_SNDHWM, &hwm, sizeof (hwm)); 00047 assert (rc == 0); 00048 rc = zmq_connect (sc, "inproc://a"); 00049 assert (rc == 0); 00050 00051 // Try to send 10 messages. Only 4 should succeed. 00052 for (int i = 0; i < 10; i++) 00053 { 00054 int rc = zmq_send (sc, NULL, 0, ZMQ_DONTWAIT); 00055 if (i < 4) 00056 assert (rc == 0); 00057 else 00058 assert (rc < 0 && errno == EAGAIN); 00059 } 00060 00061 // There should be now 4 messages pending, consume them. 00062 for (int i = 0; i != 4; i++) { 00063 rc = zmq_recv (sb, NULL, 0, 0); 00064 assert (rc == 0); 00065 } 00066 00067 // Now it should be possible to send one more. 00068 rc = zmq_send (sc, NULL, 0, 0); 00069 assert (rc == 0); 00070 00071 // Consume the remaining message. 00072 rc = zmq_recv (sb, NULL, 0, 0); 00073 assert (rc == 0); 00074 00075 rc = zmq_close (sc); 00076 assert (rc == 0); 00077 00078 rc = zmq_close (sb); 00079 assert (rc == 0); 00080 00081 rc = zmq_term (ctx); 00082 assert (rc == 0); 00083 00084 return 0; 00085 }