~ubuntu-branches/ubuntu/wily/zeromq3/wily-proposed

« back to all changes in this revision

Viewing changes to tests/test_ctx_destroy.cpp

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2014-03-16 14:02:28 UTC
  • mfrom: (1.1.6) (6.1.1 experimental)
  • Revision ID: package-import@ubuntu.com-20140316140228-ig1sgh7czk59m9ux
Tags: 4.0.4+dfsg-1
* QA upload; orphan the package
  - Upload to unstable
* New upstream release
* Update repack.stub script
* Drop 02_fix-exported-symbols.patch and 03_fix-s390-rdtsc.patch
  (merged upstream)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
    Copyright (c) 2007-2013 Contributors as noted in the AUTHORS file
 
3
 
 
4
    This file is part of 0MQ.
 
5
 
 
6
    0MQ is free software; you can redistribute it and/or modify it under
 
7
    the terms of the GNU Lesser General Public License as published by
 
8
    the Free Software Foundation; either version 3 of the License, or
 
9
    (at your option) any later version.
 
10
 
 
11
    0MQ is distributed in the hope that it will be useful,
 
12
    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
    GNU Lesser General Public License for more details.
 
15
 
 
16
    You should have received a copy of the GNU Lesser General Public License
 
17
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
*/
 
19
 
 
20
#include "testutil.hpp"
 
21
 
 
22
static void receiver (void *socket)
 
23
{
 
24
    char buffer[16];
 
25
    int rc = zmq_recv (socket, &buffer, sizeof (buffer), 0);
 
26
    assert(rc == -1);
 
27
}
 
28
 
 
29
void test_ctx_destroy()
 
30
{
 
31
    int rc;
 
32
    
 
33
    //  Set up our context and sockets
 
34
    void *ctx = zmq_ctx_new ();
 
35
    assert (ctx);
 
36
    
 
37
    void *socket = zmq_socket (ctx, ZMQ_PULL);
 
38
    assert (socket);
 
39
 
 
40
    // Close the socket
 
41
    rc = zmq_close (socket);
 
42
    assert (rc == 0);
 
43
    
 
44
    // Destroy the context
 
45
    rc = zmq_ctx_destroy (ctx);
 
46
    assert (rc == 0);
 
47
}
 
48
 
 
49
void test_ctx_shutdown()
 
50
{
 
51
    int rc;
 
52
    
 
53
    //  Set up our context and sockets
 
54
    void *ctx = zmq_ctx_new ();
 
55
    assert (ctx);
 
56
    
 
57
    void *socket = zmq_socket (ctx, ZMQ_PULL);
 
58
    assert (socket);
 
59
 
 
60
    // Spawn a thread to receive on socket
 
61
    void *receiver_thread = zmq_threadstart (&receiver, socket);
 
62
 
 
63
    // Wait for thread to start up and block
 
64
    msleep (SETTLE_TIME);
 
65
 
 
66
    // Shutdown context, if we used destroy here we would deadlock.
 
67
    rc = zmq_ctx_shutdown (ctx);
 
68
    assert (rc == 0);
 
69
 
 
70
    // Wait for thread to finish
 
71
    zmq_threadclose (receiver_thread);
 
72
 
 
73
    // Close the socket.
 
74
    rc = zmq_close (socket);
 
75
    assert (rc == 0);
 
76
 
 
77
    // Destory the context, will now not hang as we have closed the socket.
 
78
    rc = zmq_ctx_destroy (ctx);
 
79
    assert (rc == 0);
 
80
}
 
81
 
 
82
int main (void)
 
83
{
 
84
    setup_test_environment();
 
85
 
 
86
    test_ctx_destroy();
 
87
    test_ctx_shutdown();
 
88
 
 
89
    return 0;
 
90
}