summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/PollerTest.cpp
blob: 9fa5689c5f11cc0853b0c2dae369058dcd9d640c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
/*
 *
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 *
 */

/**
 * Use socketpair to test the poller
 */

#include "qpid/sys/IOHandle.h"
#include "qpid/sys/Poller.h"
#include "qpid/sys/posix/PrivatePosix.h"

#include <string>
#include <iostream>
#include <memory>
#include <exception>

#include <assert.h>

#include <sys/types.h>
#include <sys/socket.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>

using namespace std;
using namespace qpid::sys;

int writeALot(int fd, const string& s) {
    int bytesWritten = 0;
    do {
        errno = 0;
        int lastWrite = ::write(fd, s.c_str(), s.size());
        if ( lastWrite >= 0) {
            bytesWritten += lastWrite;
        }
    } while (errno != EAGAIN);
    return bytesWritten;
}

int readALot(int fd) {
    int bytesRead = 0;
    char buf[1024];

    do {
        errno = 0;
        int lastRead = ::read(fd, buf, sizeof(buf));
        if ( lastRead >= 0) {
            bytesRead += lastRead;
        }
    } while (errno != EAGAIN);
    return bytesRead;
}

void makesocketpair(int (&sv)[2]) {
    int rc = ::socketpair(AF_UNIX, SOCK_STREAM, 0, sv);
    assert(rc >= 0);

    // Set non-blocking
    rc = ::fcntl(sv[0], F_SETFL, O_NONBLOCK);
    assert(rc >= 0);

    rc = ::fcntl(sv[1], F_SETFL, O_NONBLOCK);
    assert(rc >= 0);
}

int main(int /*argc*/, char** /*argv*/)
{
    try
    {
        int sv[2];
        makesocketpair(sv);

        // Make up a large string
        string testString = "This is only a test ... 1,2,3,4,5,6,7,8,9,10;";
        for (int i = 0; i < 6; i++)
            testString += testString;

        // Read as much as we can from socket 0
        int bytesRead = readALot(sv[0]);
        assert(bytesRead == 0);

        // Write as much as we can to socket 0
        int bytesWritten = writeALot(sv[0], testString);

        // Read as much as we can from socket 1
        bytesRead = readALot(sv[1]);
        assert(bytesRead == bytesWritten);

        auto_ptr<Poller> poller(new Poller);

        PosixIOHandle f0(sv[0]);
        PosixIOHandle f1(sv[1]);

        PollerHandle h0(f0);
        PollerHandle h1(f1);

        poller->registerHandle(h0);
        poller->monitorHandle(h0, Poller::INOUT);

        // h0 should be writable
        Poller::Event event = poller->wait();
        assert(event.handle == &h0);
        assert(event.type == Poller::WRITABLE);

        // Write as much as we can to socket 0
        bytesWritten = writeALot(sv[0], testString);

        // Wait for 500ms - h0 no longer writable
        event = poller->wait(500000000);
        assert(event.handle == 0);

        // Test we can read it all now
        poller->registerHandle(h1);
        poller->monitorHandle(h1, Poller::INOUT);
        event = poller->wait();
        assert(event.handle == &h1);
        assert(event.type == Poller::READ_WRITABLE);

        bytesRead = readALot(sv[1]);
        assert(bytesRead == bytesWritten);

        // Test poller interrupt
        assert(poller->interrupt(h0) == true);
        event = poller->wait();
        assert(event.handle == &h0);
        assert(event.type == Poller::INTERRUPTED);

        // Test multiple interrupts
        assert(poller->interrupt(h0) == true);
        assert(poller->interrupt(h1) == true);

        // Make sure we can interrupt them again
        assert(poller->interrupt(h0) == true);
        assert(poller->interrupt(h1) == true);

        // Make sure that they both come out
        event = poller->wait();
        assert(event.type == Poller::INTERRUPTED);
        assert(event.handle == &h0 || event.handle == &h1);
        if (event.handle == &h0) {
            event = poller->wait();
            assert(event.type == Poller::INTERRUPTED);
            assert(event.handle == &h1);
        } else {
            event = poller->wait();
            assert(event.type == Poller::INTERRUPTED);
            assert(event.handle == &h0);
        }

        poller->unmonitorHandle(h1, Poller::INOUT);

        event = poller->wait();
        assert(event.handle == &h0);
        assert(event.type == Poller::WRITABLE);

        // We didn't write anything so it should still be writable
        event = poller->wait();
        assert(event.handle == &h0);
        assert(event.type == Poller::WRITABLE);

        poller->unmonitorHandle(h0, Poller::INOUT);

        event = poller->wait(500000000);
        assert(event.handle == 0);

        poller->unregisterHandle(h1);
        assert(poller->interrupt(h1) == false);

        // close the other end to force a disconnect
        ::close(sv[1]);

        // Now make sure that we are readable followed by disconnected
        // and after that we never return again
        poller->monitorHandle(h0, Poller::INOUT);
        event = poller->wait(500000000);
        assert(event.handle == &h0);
        assert(event.type == Poller::READABLE);
        event = poller->wait(500000000);
        assert(event.handle == &h0);
        assert(event.type == Poller::DISCONNECTED);
        event = poller->wait(1500000000);
        assert(event.handle == 0);

        // Now we're disconnected monitoring should have no effect at all
        poller->unmonitorHandle(h0, Poller::INOUT);
        event = poller->wait(1500000000);
        assert(event.handle == 0);

        poller->unregisterHandle(h0);
        assert(poller->interrupt(h0) == false);

        // Test shutdown
        poller->shutdown();
        event = poller->wait();
        assert(event.handle == 0);
        assert(event.type == Poller::SHUTDOWN);

        event = poller->wait();
        assert(event.handle == 0);
        assert(event.type == Poller::SHUTDOWN);

        ::close(sv[0]);

	// Test for correct interaction of shutdown and interrupts - need to have new poller
	// etc. for this
        makesocketpair(sv);
	
        auto_ptr<Poller> poller1(new Poller);

        PosixIOHandle f2(sv[0]);
        PosixIOHandle f3(sv[1]);

        PollerHandle h2(f2);
        PollerHandle h3(f3);

        poller1->registerHandle(h2);
        poller1->monitorHandle(h2, Poller::INOUT);
        event = poller1->wait();
        assert(event.handle == &h2);
        assert(event.type == Poller::WRITABLE);

	// Shutdown
        poller1->shutdown();
        event = poller1->wait();
        assert(event.handle == 0);
        assert(event.type == Poller::SHUTDOWN);
        
        assert(poller1->interrupt(h2) == true);
        event = poller1->wait();
        assert(event.handle == &h2);
        assert(event.type == Poller::INTERRUPTED);
        poller1->unmonitorHandle(h2, Poller::INOUT);

        event = poller1->wait();
        assert(event.handle == 0);
        assert(event.type == Poller::SHUTDOWN);

        poller1->unregisterHandle(h2);
        return 0;
    } catch (exception& e) {
        cout << "Caught exception  " << e.what() << "\n";
    }
}