test_linger.cpp 2.79 KB
Newer Older
MinRK's avatar
MinRK committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
    Copyright (c) 2007-2013 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/>.
*/

20 21 22 23
//  This test case should be moved or added to issues repository, perhaps,
//  or alternatively we can change policy that issue test cases should be 
//  added to the main build when they're useful. -- PH 2013/09/02

MinRK's avatar
MinRK committed
24 25 26 27
#include "testutil.hpp"

#define URL "tcp://127.0.0.1:5560"

28 29 30
//  Sender connects a PUSH socket and sends a message of a given size,
//  closing the socket and terminating the context immediately.
//  LINGER should prevent this from dropping messages.
MinRK's avatar
MinRK committed
31

32
static void sender (void *vsize)
MinRK's avatar
MinRK committed
33
{
34 35 36
    void *context = zmq_ctx_new ();
    void *push = zmq_socket (context, ZMQ_PUSH);
    size_t size = ((size_t *) vsize) [0];
MinRK's avatar
MinRK committed
37

38
    int rc = zmq_connect (push, URL);
MinRK's avatar
MinRK committed
39 40
    assert (rc == 0);
  
41 42
    zmq_msg_t msg;
    rc = zmq_msg_init_size (&msg, size);
MinRK's avatar
MinRK committed
43 44
    assert (rc == 0);

45
    char *buf = (char *) zmq_msg_data (&msg);
MinRK's avatar
MinRK committed
46
    assert (buf != NULL);
47
    memset (buf, 'x', size);
MinRK's avatar
MinRK committed
48

49
    rc = zmq_msg_send (&msg, push, 0);
50
    assert ((size_t) rc == size);
MinRK's avatar
MinRK committed
51

52
    rc = zmq_msg_close (&msg);
MinRK's avatar
MinRK committed
53 54
    assert (rc == 0);

55
    rc = zmq_close (push);
MinRK's avatar
MinRK committed
56 57
    assert (rc == 0);

58
    rc = zmq_ctx_term (context);
MinRK's avatar
MinRK committed
59 60 61 62 63
    assert (rc == 0);
}

int main (void)
{
64 65
    void *context = zmq_ctx_new ();
    void *pull = zmq_socket (context, ZMQ_PULL);
MinRK's avatar
MinRK committed
66 67
    zmq_msg_t msg;
    
68
    int rc = zmq_bind (pull, URL);
MinRK's avatar
MinRK committed
69 70
    assert (rc == 0);
    
71 72 73
    //  Symptom of test failure is message never received
    int rcvtimeo = 100;
    rc = zmq_setsockopt (pull, ZMQ_RCVTIMEO, &rcvtimeo, sizeof (int));
MinRK's avatar
MinRK committed
74 75
    assert (rc == 0);
    
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
    //  Single test case that currently provokes failure in libzmq
    //  Note that socket will wait as long as receive timeout, before
    //  returning a "resource unavailable" or an assertion failure in
    //  poller_base.cpp:31.
    size_t size = 4000000;
    
    //  Start the sender thread and get message
    void *send_thread = zmq_threadstart (&sender, (void *) &size);
    zmq_msg_init (&msg);
    rc = zmq_msg_recv (&msg, pull, 0);
    assert ((size_t) rc == size);
    zmq_msg_close (&msg);
    zmq_threadclose (send_thread);
    
    zmq_close (pull);
    zmq_ctx_term (context);
    return 0;
}