Debian Codegraph @

c/tests/pn_test_proactor.cpp

raw
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20 #include "./pn_test_proactor.hpp"
21 #include "./thread.h"
22
23 #include <proton/condition.h>
24 #include <proton/connection.h>
25 #include <proton/delivery.h>
26 #include <proton/event.h>
27 #include <proton/link.h>
28 #include <proton/listener.h>
29 #include <proton/message.h>
30 #include <proton/netaddr.h>
31 #include <proton/object.h>
32 #include <proton/proactor.h>
33 #include <proton/transport.h>
34
35 namespace pn_test {
36
37 std::string listening_port(pn_listener_t *l) {
38 const pn_netaddr_t *na = pn_listener_addr(l);
39 char port[PN_MAX_ADDR];
40 pn_netaddr_host_port(na, NULL, 0, port, sizeof(port));
41 return port;
42 }
43
44 proactor::proactor(struct handler *h)
45 : auto_free<pn_proactor_t, pn_proactor_free>(pn_proactor()), handler(h) {}
46
47 bool proactor::dispatch(pn_event_t *e) {
48 void *ctx = NULL;
49 if (pn_event_listener(e))
50 ctx = pn_listener_get_context(pn_event_listener(e));
51 else if (pn_event_connection(e))
52 ctx = pn_connection_get_context(pn_event_connection(e));
53 struct handler *h = ctx ? reinterpret_cast<struct handler *>(ctx) : handler;
54 bool ret = h ? h->dispatch(e) : false;
55 // pn_test::handler doesn't know about listeners so save listener condition
56 // here.
57 if (pn_event_listener(e)) {
58 pn_condition_copy(h->last_condition,
59 pn_listener_condition(pn_event_listener(e)));
60 }
61 return ret;
62 }
63
64 // RAII for event batches
65 class auto_batch {
66 pn_proactor_t *p_;
67 pn_event_batch_t *b_;
68
69 public:
70 auto_batch(pn_proactor_t *p, pn_event_batch_t *b) : p_(p), b_(b) {}
71 ~auto_batch() {
72 if (b_) pn_proactor_done(p_, b_);
73 }
74 pn_event_t *next() { return b_ ? pn_event_batch_next(b_) : NULL; }
75 pn_event_batch_t *get() { return b_; }
76 };
77
78 pn_event_type_t proactor::run(pn_event_type_t stop) {
79 // This will hang in the underlying poll if test expectations are never met.
80 // Not ideal, but easier to debug than race conditions caused by buggy
81 // test-harness code that attempts to detect the problem and recover early.
82 // The larger test or CI harness will kill us after some time limit
83 while (true) {
84 auto_batch b(*this, pn_proactor_wait(*this));
85 if (b.get()) {
86 pn_event_t *e;
87 while ((e = b.next())) {
88 pn_event_type_t et = pn_event_type(e);
89 if (dispatch(e) || et == stop) return et;
90 }
91 }
92 }
93 }
94
95 std::pair<int, pn_event_type_t> proactor::flush(pn_event_type_t stop) {
96 auto_batch b(*this, pn_proactor_get(*this));
97 int n = 0;
98 if (b.get()) {
99 pn_event_t *e;
100 while ((e = b.next())) {
101 pn_event_type_t et = pn_event_type(e);
102 if (dispatch(e) || et == stop) return std::make_pair(n, et);
103 }
104 }
105 return std::make_pair(n, PN_EVENT_NONE);
106 }
107
108 pn_event_type_t proactor::corun(proactor &other, pn_event_type_t stop) {
109 // We can't wait() on either proactor as it might be idle.
110 // We can't give up immediately if both proactors are idle
111 // something happens on the other, so spin between the two for a limited
112 // number of attempts that should be large enough if the test is going to
113 // pass.
114 int spin_limit = 1000;
115 do {
116 std::pair<int, pn_event_type_t> n_et = flush(stop);
117 if (n_et.second) return n_et.second;
118 if (n_et.first + other.flush().first == 0) {
119 // Both idle, sleep and retry in case network traffic is in flight.
120 millisleep(1);
121 --spin_limit;
122 }
123 } while (spin_limit);
124
125 return PN_EVENT_NONE;
126 }
127
128 pn_event_type_t proactor::wait_next() {
129 // pn_proactor_wait() should never return an empty batch, so we shouldn't need
130 // a loop here. Due to bug https://issues.apache.org/jira/browse/PROTON-1964
131 // we need to re-wait if we get an empty batch.
132 //
133 // To reproduce PROTON-1964 remove the loop below and run
134 // TEST_CASE("proactor_proton_1586") from proactor_test.cpp
135 //
136 // You will pn_proactor_wait() return a non-NULL batch, but the
137 // first call to pn_event_batch_next() returns a NULL event.
138 //
139 while (true) {
140 auto_batch b(*this, pn_proactor_wait(*this));
141 pn_event_t *e = b.next();
142 if (e) {
143 dispatch(e);
144 return pn_event_type(e);
145 } // Try again on an empty batch.
146 }
147 }
148
149 pn_listener_t *proactor::listen(const std::string &addr,
150 struct handler *handler) {
151 pn_listener_t *l = pn_listener();
152 pn_listener_set_context(l, handler);
153 pn_proactor_listen(*this, l, addr.c_str(), 4);
154 return l;
155 }
156
157 pn_connection_t *proactor::connect(const std::string &addr, struct handler *h,
158 pn_connection_t *c) {
159 if (!c) c = pn_connection();
160 if (h) pn_connection_set_context(c, h);
161 pn_proactor_connect2(*this, c, NULL, addr.c_str());
162 return c;
163 }
164
165 } // namespace pn_test