forked from apache/qpid-dispatch
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_http_listener_connector.cpp
160 lines (128 loc) · 5.4 KB
/
test_http_listener_connector.cpp
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
/*
* 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 <Python.h>
#include "./qdr_doctest.hpp"
#include "./helpers.hpp" // must come after ./qdr_doctest.hpp
#include <map>
#include <thread>
#include <variant>
extern "C" {
#include "adaptors/http_common.h"
}
using entity_value_t = std::variant<std::string, const char *, bool>;
/// Builds a std::unique_ptr<qd_entity_t> instance from a C++ map with std::string-typed keys.
/// Caller must hold the Python GIL (qd_entity_t is a PyObject).
static auto create_entity_from_config_map(std::map<std::string, entity_value_t> const &config)
{
PyObject *pyObject = PyDict_New();
for (auto [k, v] : config) {
PyObject *item = nullptr;
if (std::holds_alternative<std::string>(v)) {
item = PyUnicode_FromString(std::get<std::string>(v).c_str());
} else if (std::holds_alternative<const char *>(v)) {
// gcc 8.5. 0 (on RHEL 8) prefers to cast const char * to bool and not std::string
item = PyUnicode_FromString(std::get<const char *>(v));
} else if (std::holds_alternative<bool>(v)) {
item = std::get<bool>(v) ? Py_True : Py_False;
Py_IncRef(item);
}
REQUIRE_MESSAGE(item != nullptr, "Test utils cannot handle value of a given type");
PyDict_SetItemString(pyObject, k.c_str(), item);
Py_DECREF(item);
}
// return a std::unique_ptr with a deleter the runs Py_DECREF when the value goes out of scope
qd_entity_t *entity = reinterpret_cast<qd_entity_t *>(pyObject);
return qd_make_unique(entity, [](qd_entity_t *entity) { Py_DECREF(entity); });
}
TEST_CASE("Configure and start HTTP_ADAPTOR listener")
{
std::thread([] {
QDR qdr{};
qdr.initialize("./minimal_trace.conf");
qdr.wait();
qd_dispatch_t *qd = qdr.qd;
CaptureCStream css(stderr);
// previous functions dropped Python GIL
auto state = PyGILState_Ensure();
SUBCASE("empty config should fail but not crash")
{
std::map<std::string, entity_value_t> config = {
// empty map
};
// build entity
auto entity = create_entity_from_config_map(config);
// perform test
CHECK(qd_dispatch_configure_http_listener(qd, entity.get()) == nullptr);
// check logging
std::string logging = css.str();
const std::string unable = R"EOS(HTTP_ADAPTOR (error) Unable to load config information)EOS";
CHECK_MESSAGE(logging.find(unable) != std::string::npos, unable, " not found in ", logging);
}
SUBCASE("minimal config should create listener")
{
std::map<std::string, entity_value_t> config = {
{"protocolVersion", "HTTP1"},
{"name", "my_config"}, //
{"host", "localhost"}, //
{"port", "0"}, //
{"address", "my_address"}, //
};
// build entity
auto entity = create_entity_from_config_map(config);
// perform test
qd_http_listener_t *listener = qd_dispatch_configure_http_listener(qd, entity.get());
REQUIRE(listener != nullptr);
// clean up
qd_dispatch_delete_http_listener(qd, listener);
// check logging
std::string logging = css.str();
const std::string configured =
R"EOS(HTTP_ADAPTOR (info) Configured HTTP_ADAPTOR listener on localhost:0)EOS";
CHECK_MESSAGE(logging.find(configured) != std::string::npos, configured, " not found in ", logging);
}
PyGILState_Release(state);
qdr.deinitialize();
}).join();
}
TEST_CASE("Configure and start HTTP connector")
{
QDR qdr{};
qdr.initialize("./minimal_trace.conf");
qdr.wait();
qd_dispatch_t *qd = qdr.qd;
CaptureCStream css(stderr);
// previous functions dropped Python GIL
auto state = PyGILState_Ensure();
SUBCASE("empty config should fail but not crash")
{
std::map<std::string, entity_value_t> config = {
// empty map
};
// build entity
auto entity = create_entity_from_config_map(config);
// perform test
CHECK(qd_dispatch_configure_http_connector(qd, entity.get()) == nullptr);
// check logging
std::string logging = css.str();
const std::string unable = R"EOS(HTTP_ADAPTOR (error) Unable to load config information)EOS";
CHECK_MESSAGE(logging.find(unable) != std::string::npos, unable, " not found in ", logging);
}
PyGILState_Release(state);
qdr.deinitialize();
}