summaryrefslogtreecommitdiff
path: root/cpp/tests/EventChannelTest.cpp
blob: 67b8b03ce287ad0aa8ee364c8cb26305b06844a8 (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
/*
 *
 * 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.
 *
 */
#include <posix/EventChannel.h>
#include <posix/check.h>
#include <sys/Runnable.h>
#include <sys/Socket.h>
#include <sys/Thread.h>
#include <qpid_test_plugin.h>

#include <sys/socket.h>
#include <signal.h>
#include <netinet/in.h>
#include <netdb.h>
#include <iostream>

using namespace qpid::sys;


const char hello[] = "hello";
const size_t size = sizeof(hello);

struct RunMe : public Runnable 
{
    bool ran;
    RunMe() : ran(false) {}
    void run() { ran = true; }
};

class EventChannelTest : public CppUnit::TestCase  
{
    CPPUNIT_TEST_SUITE(EventChannelTest);
    CPPUNIT_TEST(testDispatch);
    CPPUNIT_TEST(testRead);
    CPPUNIT_TEST(testPartialRead);
    CPPUNIT_TEST(testFailedRead);
    CPPUNIT_TEST(testWrite);
    CPPUNIT_TEST(testFailedWrite);
    CPPUNIT_TEST(testReadWrite);
    CPPUNIT_TEST(testAccept);
    CPPUNIT_TEST_SUITE_END();

  private:
    EventChannel::shared_ptr ec;
    int pipe[2];
    char readBuf[size];

  public:

    void setUp()
    {
        memset(readBuf, size, 0);
        ec = EventChannel::create();
        if (::pipe(pipe) != 0) throw QPID_POSIX_ERROR(errno);
        // Ignore SIGPIPE, otherwise we will crash writing to broken pipe.
        signal(SIGPIPE, SIG_IGN);
    }

    // Verify that calling wait returns event.
    template <class T> bool isNextEvent(T& event)
    {
        return &event == dynamic_cast<T*>(ec->wait(5*TIME_SEC));
    }

    template <class T> bool isNextEventOk(T& event)
    {
        Event* next = ec->wait(TIME_SEC);
        if (next) next->throwIfException();
        return &event == next;
    }
        
    void testDispatch()
    {
        RunMe runMe;
        CPPUNIT_ASSERT(!runMe.ran);
        // Instances of Event just pass thru the channel immediately.
        DispatchEvent e(runMe.functor());
        ec->post(e);
        CPPUNIT_ASSERT(isNextEventOk(e));
        e.dispatch();
        CPPUNIT_ASSERT(runMe.ran);
    }

    void testRead() {
        ReadEvent re(pipe[0], readBuf, size);
        ec->post(re);
        CPPUNIT_ASSERT_EQUAL(ssize_t(size), ::write(pipe[1], hello, size));
        CPPUNIT_ASSERT(isNextEventOk(re));
        CPPUNIT_ASSERT_EQUAL(size, re.getBytesRead());
        CPPUNIT_ASSERT_EQUAL(std::string(hello), std::string(readBuf));
    }

    void testPartialRead() {
        ReadEvent re(pipe[0], readBuf, size, 0, true);
        ec->post(re);
        CPPUNIT_ASSERT_EQUAL(ssize_t(size/2), ::write(pipe[1], hello, size/2));
        CPPUNIT_ASSERT(isNextEventOk(re));
        CPPUNIT_ASSERT_EQUAL(size/2, re.getBytesRead());
        CPPUNIT_ASSERT_EQUAL(std::string(hello, size/2),
                             std::string(readBuf, size/2));
    }
    

    void testFailedRead() 
    {
        ReadEvent re(pipe[0], readBuf, size);
        ec->post(re);

        // EOF before all data read.
        ::close(pipe[1]);
        CPPUNIT_ASSERT(isNextEvent(re));
        CPPUNIT_ASSERT(re.getException());
        try {
            re.throwIfException();
            CPPUNIT_FAIL("Expected QpidError.");
        }
        catch (const qpid::QpidError&) { }

        
        //  Try to read from closed file descriptor.
        try {
            ec->post(re);
            CPPUNIT_ASSERT(isNextEvent(re));
            re.throwIfException();
            CPPUNIT_FAIL("Expected an exception.");
        }
        catch (const qpid::QpidError&) {}
        
        //  Bad file descriptor. Note in this case we fail
        //  in post and throw immediately.
        try {
            ReadEvent bad(-1, readBuf, size);
            ec->post(bad);
            CPPUNIT_FAIL("Expected QpidError.");
        }
        catch (const qpid::QpidError&) {}
    }

    void testWrite() {
        WriteEvent wr(pipe[1], hello, size);
        ec->post(wr);
        CPPUNIT_ASSERT(isNextEventOk(wr));
        CPPUNIT_ASSERT_EQUAL(size, wr.getBytesWritten());
        CPPUNIT_ASSERT_EQUAL(ssize_t(size), ::read(pipe[0], readBuf, size));;
        CPPUNIT_ASSERT_EQUAL(std::string(hello), std::string(readBuf));
    }

    void testFailedWrite() {
        WriteEvent wr(pipe[1], hello, size);
        ::close(pipe[0]);
        ec->post(wr);
        CPPUNIT_ASSERT(isNextEvent(wr));
        CPPUNIT_ASSERT(wr.getException());
    }

    void testReadWrite()
    {
        ReadEvent re(pipe[0], readBuf, size);
        WriteEvent wr(pipe[1], hello, size);
        ec->post(re);
        ec->post(wr);
        ec->wait(TIME_SEC);
        ec->wait(TIME_SEC);
        CPPUNIT_ASSERT_EQUAL(std::string(hello), std::string(readBuf));
    }

    void connectSendRead(AcceptEvent& ae, int port, Socket client)
    {
        ec->post(ae);
        // Connect a client, send some data, read the data.
        client.connect("localhost", port);
        CPPUNIT_ASSERT(isNextEvent(ae));
        ae.throwIfException();

        char readBuf[size];
        ReadEvent re(ae.getAcceptedDesscriptor(), readBuf, size);
        ec->post(re);
        CPPUNIT_ASSERT_EQUAL(ssize_t(size),
                             client.send(hello, sizeof(hello)));
        CPPUNIT_ASSERT(isNextEvent(re));
        re.throwIfException();
        CPPUNIT_ASSERT_EQUAL(std::string(hello), std::string(readBuf));
    }

    void testAccept() {
        Socket s = Socket::createTcp();
        int port = s.listen(0, 10);
        CPPUNIT_ASSERT(port != 0);

        AcceptEvent ae(s.fd());
        Socket client = Socket::createTcp();
        connectSendRead(ae, port,  client);
        Socket client2 = Socket::createTcp();
        connectSendRead(ae, port,  client2);
        client.close();
        client2.close();
        Socket client3 = Socket::createTcp();
        connectSendRead(ae, port,  client3);
    }
};

// Make this test suite a plugin.
CPPUNIT_PLUGIN_IMPLEMENT();
CPPUNIT_TEST_SUITE_REGISTRATION(EventChannelTest);