| 1 |
#ifndef TESTS_PN_TEST_PROACTOR_HPP |
| 2 |
#define TESTS_PN_TEST_PROACTOR_HPP |
| 3 |
|
| 4 |
/* |
| 5 |
* Licensed to the Apache Software Foundation (ASF) under one |
| 6 |
* or more contributor license agreements. See the NOTICE file |
| 7 |
* distributed with this work for additional information |
| 8 |
* regarding copyright ownership. The ASF licenses this file |
| 9 |
* to you under the Apache License, Version 2.0 (the |
| 10 |
* "License"); you may not use this file except in compliance |
| 11 |
* with the License. You may obtain a copy of the License at |
| 12 |
* |
| 13 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 14 |
* |
| 15 |
* Unless required by applicable law or agreed to in writing, |
| 16 |
* software distributed under the License is distributed on an |
| 17 |
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 18 |
* KIND, either express or implied. See the License for the |
| 19 |
* specific language governing permissions and limitations |
| 20 |
* under the License. |
| 21 |
*/ |
| 22 |
|
| 23 |
/// @file |
| 24 |
/// |
| 25 |
/// Wrapper for driving proactor tests. |
| 26 |
|
| 27 |
#include "./pn_test.hpp" |
| 28 |
#include <proton/proactor.h> |
| 29 |
|
| 30 |
namespace pn_test { |
| 31 |
|
| 32 |
// Get the listening port, l must be open. |
| 33 |
std::string listening_port(pn_listener_t *l); |
| 34 |
|
| 35 |
// Test proactor with an optional global handler. |
| 36 |
// For connection and listener events, if pn_*_get_context() is non-NULL |
| 37 |
// then it is cast to a handler and used instead of the global one. |
| 38 |
struct proactor : auto_free<pn_proactor_t, pn_proactor_free> { |
| 39 |
struct handler *handler; |
| 40 |
|
| 41 |
proactor(struct handler *h = 0); |
| 42 |
|
| 43 |
// Listen on addr using optional listener handler lh |
| 44 |
pn_listener_t *listen(const std::string &addr = ":0", struct handler *lh = 0); |
| 45 |
|
| 46 |
// Connect to addr use optional handler, and optionally providing the |
| 47 |
// connection object. |
| 48 |
pn_connection_t *connect(const std::string &addr, struct handler *h = 0, |
| 49 |
pn_connection_t *c = 0); |
| 50 |
|
| 51 |
// Connect to listenr's address useing optional handler. |
| 52 |
pn_connection_t *connect(pn_listener_t *l, struct handler *h = 0, |
| 53 |
pn_connection_t *c = 0) { |
| 54 |
return connect(":" + pn_test::listening_port(l), h, c); |
| 55 |
} |
| 56 |
|
| 57 |
// Accept a connection, associate with optional connection handler. |
| 58 |
pn_connection_t *accept(pn_listener_t *l, struct handler *h = 0); |
| 59 |
|
| 60 |
// Wait for events and dispatch them until: |
| 61 |
// * A handler returns true. |
| 62 |
// * The `stop` event type is handled. |
| 63 |
// Return the event-type of the last event handled or PN_EVENT_NONE |
| 64 |
// if something went wrong. |
| 65 |
pn_event_type_t run(pn_event_type_t stop = PN_EVENT_NONE); |
| 66 |
|
| 67 |
// Dispatch immediately-available events until: |
| 68 |
// * A handler returns true. |
| 69 |
// * The `stop` event type is handled. |
| 70 |
// * All available events are flushed. |
| 71 |
// |
| 72 |
// Returns the number of events processed and |
| 73 |
// - PN_EVENT_NONE if all events were handled without stopping |
| 74 |
// - The type of the last event handled otherwise |
| 75 |
// |
| 76 |
std::pair<int, pn_event_type_t> flush(pn_event_type_t stop = PN_EVENT_NONE); |
| 77 |
|
| 78 |
// Alternate flushing this proactor and `other` until |
| 79 |
// * A handler on this proactor returns true. |
| 80 |
// * The `stop` event type is handled by this proactor. |
| 81 |
// * Both proactors become idle. |
| 82 |
// |
| 83 |
// Return the event-type of the last event handled or PN_EVENT_NONE |
| 84 |
// if the proactors are idle. |
| 85 |
pn_event_type_t corun(proactor &other, pn_event_type_t stop = PN_EVENT_NONE); |
| 86 |
|
| 87 |
// Wait for and handle a single event, return it's type. |
| 88 |
pn_event_type_t wait_next(); |
| 89 |
|
| 90 |
private: |
| 91 |
bool dispatch(pn_event_t *e); |
| 92 |
}; |
| 93 |
|
| 94 |
// CHECK/REQUIRE macros to run a proactor up to an expected event and |
| 95 |
// include the last condition in the error message if the expected event is not |
| 96 |
// returned. |
| 97 |
|
| 98 |
#define CHECK_RUN(P, E) \ |
| 99 |
CHECKED_IF((E) == (P).run(E)) {} \ |
| 100 |
else if ((P).handler) { \ |
| 101 |
FAIL_CHECK(*(P).handler->last_condition); \ |
| 102 |
} |
| 103 |
|
| 104 |
#define REQUIRE_RUN(P, E) \ |
| 105 |
CHECKED_IF((E) == (P).run(E)) {} \ |
| 106 |
else if ((P).handler) { \ |
| 107 |
FAIL(*(P).handler->last_condition); \ |
| 108 |
} |
| 109 |
|
| 110 |
#define CHECK_CORUN(P, O, E) \ |
| 111 |
CHECKED_IF((E) == (P).corun(O, E)) {} \ |
| 112 |
else if ((P).handler) { \ |
| 113 |
FAIL_CHECK(*(P).handler->last_condition); \ |
| 114 |
} |
| 115 |
|
| 116 |
#define REQUIRE_CORUN(P, E) \ |
| 117 |
CHECKED_IF((E) == (P).corun(O, E)) {} \ |
| 118 |
else if ((P).handler_) { \ |
| 119 |
FAIL(*(P).handler_->last_condition); \ |
| 120 |
} |
| 121 |
|
| 122 |
} // namespace pn_test |
| 123 |
|
| 124 |
#endif // TESTS_PN_TEST_PROACTOR_HPP |