AlexaClientSDK  1.26.0
A cross-platform, modular SDK for interacting with the Alexa Voice Service
MockHTTP2Connection.h
Go to the documentation of this file.
1 /*
2  * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License").
5  * You may not use this file except in compliance with the License.
6  * A copy of the License is located at
7  *
8  * http://aws.amazon.com/apache2.0/
9  *
10  * or in the "license" file accompanying this file. This file is distributed
11  * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12  * express or implied. See the License for the specific language governing
13  * permissions and limitations under the License.
14  */
15 #ifndef ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKHTTP2CONNECTION_H_
16 #define ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKHTTP2CONNECTION_H_
17 
18 #include <chrono>
19 #include <condition_variable>
20 #include <cstddef>
21 #include <deque>
22 #include <memory>
23 #include <mutex>
24 #include <string>
25 #include <unordered_set>
26 
27 #include <gmock/gmock.h>
28 #include <gtest/gtest.h>
29 
34 
35 #include "MockHTTP2Request.h"
36 
37 namespace alexaClientSDK {
38 namespace avsCommon {
39 namespace utils {
40 namespace http2 {
41 namespace test {
42 
47 public:
55  MockHTTP2Connection(std::string dURL, std::string pingURL, bool delegateToReal = true);
56 
57  ~MockHTTP2Connection() = default;
58 
61  MOCK_METHOD1(createAndSendRequest, std::shared_ptr<HTTP2RequestInterface>(const HTTP2RequestConfig& config));
62  MOCK_METHOD0(disconnect, void());
63  void addObserver(std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionObserverInterface> observer);
64  void removeObserver(std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionObserverInterface> observer);
66 
73  std::shared_ptr<HTTP2RequestInterface> createAndSendRequestConcrete(const HTTP2RequestConfig& config);
74 
80  bool isRequestQueueEmpty();
81 
89  std::shared_ptr<MockHTTP2Request> waitForRequest(std::chrono::milliseconds timeout, unsigned requestNum = 1);
90 
96  std::shared_ptr<MockHTTP2Request> dequeRequest();
97 
103  void setWaitRequestHeader(const std::string& matchString);
104 
112  bool waitForRequestWithHeader(std::chrono::milliseconds timeout);
113 
120  std::shared_ptr<MockHTTP2Request> waitForPostRequest(const std::chrono::milliseconds timeout);
121 
128  std::shared_ptr<MockHTTP2Request> waitForPingRequest(const std::chrono::milliseconds timeout);
129 
139  long responseCode,
140  bool sendResponseFinished,
141  const std::chrono::milliseconds timeout);
142 
150 
156  std::shared_ptr<MockHTTP2Request> getDownchannelRequest(
157  std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
158 
164  bool isPauseOnSendReceived(std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
165 
166  /*
167  * Get the number of POST requests in the queue.
168  *
169  * @return The number of POST requests in the queue.
170  */
171  std::size_t getPostRequestsNum();
172 
173  /*
174  * Get the number of HTTP2 requests in the queue.
175  *
176  * @return The number of HTTP2 requests in the queue.
177  */
178  std::size_t getRequestsNum();
179 
180  /*
181  * Get the number of downchannel requests in the queue.
182  *
183  * @return The number of downchannel requests in the queue.
184  */
185  std::size_t getDownchannelRequestsNum();
186 
187  /*
188  * Pop the oldest HTTP2 POST request from queue if it is not empty, otherwise wait for at most @c timeout for a
189  * request to be enqueued
190  *
191  * @return The oldest HTTP2 POST request in the queue or nullptr if the timeout expires
192  */
193  std::shared_ptr<MockHTTP2Request> dequePostRequest(const std::chrono::milliseconds timeout);
194 
195  /*
196  * Pop the oldest HTTP2 POST request from queue.
197  *
198  * @return The oldest HTTP2 POST request in the queue.
199  */
200  std::shared_ptr<MockHTTP2Request> dequePostRequest();
201 
202  /*
203  * Pop the oldest HTTP2 Ping request from queue.
204  *
205  * @return The oldest HTTP2 Ping request in the queue.
206  */
207  std::shared_ptr<MockHTTP2Request> dequePingRequest();
208 
209  /*
210  * Retrieve the maximum number of POST requests in the queue at any given time.
211  *
212  * @return The maximum number of POST requests in the queue at any given time.
213  */
214  std::size_t getMaxPostRequestsEnqueud();
215 
216 private:
218  std::deque<std::shared_ptr<MockHTTP2Request>> m_requestQueue;
219 
221  std::mutex m_requestMutex;
222 
224  std::condition_variable m_requestCv;
225 
227  std::string m_downchannelURL;
228 
230  std::string m_pingURL;
231 
233  std::deque<std::shared_ptr<MockHTTP2Request>> m_downchannelRequestQueue;
234 
236  std::mutex m_downchannelRequestMutex;
237 
238  // Used to wake a thread that is waiting for a downchannel request.
239  std::condition_variable m_downchannelRequestCv;
240 
242  std::deque<std::shared_ptr<MockHTTP2Request>> m_postRequestQueue;
243 
245  std::mutex m_postRequestMutex;
246 
248  std::condition_variable m_requestPostCv;
249 
251  std::deque<std::shared_ptr<MockHTTP2Request>> m_pingRequestQueue;
252 
254  std::mutex m_observersMutex;
255 
257  std::unordered_set<std::shared_ptr<avsCommon::utils::http2::HTTP2ConnectionObserverInterface>> m_observers;
258 
260  std::mutex m_pingRequestMutex;
261 
262  // Used to wake a thread that is waiting for a Ping request.
263  std::condition_variable m_pingRequestCv;
264 
266  std::string m_headerMatch;
267 
269  std::condition_variable m_requestHeaderCv;
270 
272  PromiseFuturePair<void> m_receivedPauseOnSend;
273 
275  http::HTTPResponseCode m_postResponseCode;
276 
278  std::size_t m_maxPostRequestsEnqueued;
279 
282  static const unsigned READ_DATA_BUF_SIZE = 100;
283 };
284 
285 } // namespace test
286 } // namespace http2
287 } // namespace utils
288 } // namespace avsCommon
289 } // namespace alexaClientSDK
290 
291 #endif // ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKHTTP2CONNECTION_H_
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::waitForPingRequest
std::shared_ptr< MockHTTP2Request > waitForPingRequest(const std::chrono::milliseconds timeout)
Definition: MockHTTP2Connection.cpp:168
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::getDownchannelRequestsNum
std::size_t getDownchannelRequestsNum()
Definition: MockHTTP2Connection.cpp:219
MockHTTP2Request.h
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::createAndSendRequestConcrete
std::shared_ptr< HTTP2RequestInterface > createAndSendRequestConcrete(const HTTP2RequestConfig &config)
Definition: MockHTTP2Connection.cpp:38
alexaClientSDK::avsCommon::utils::PromiseFuturePair< void >
Definition: PromiseFuturePair.h:75
alexaClientSDK::avsCommon::utils::http2::HTTP2ConnectionInterface
Definition: HTTP2ConnectionInterface.h:34
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::waitForRequest
std::shared_ptr< MockHTTP2Request > waitForRequest(std::chrono::milliseconds timeout, unsigned requestNum=1)
Definition: MockHTTP2Connection.cpp:88
alexaClientSDK::avsCommon::utils::http::HTTPResponseCode
HTTPResponseCode
Definition: HttpResponseCode.h:29
alexaClientSDK::avsCommon::utils::http2::HTTP2RequestConfig
Definition: HTTP2RequestConfig.h:42
MessageConsumerInterface.h
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::dequePingRequest
std::shared_ptr< MockHTTP2Request > dequePingRequest()
Definition: MockHTTP2Connection.cpp:246
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::isPauseOnSendReceived
bool isPauseOnSendReceived(std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Definition: MockHTTP2Connection.cpp:207
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::MOCK_METHOD1
MOCK_METHOD1(createAndSendRequest, std::shared_ptr< HTTP2RequestInterface >(const HTTP2RequestConfig &config))
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::~MockHTTP2Connection
~MockHTTP2Connection()=default
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::addObserver
void addObserver(std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionObserverInterface > observer)
Definition: MockHTTP2Connection.cpp:260
alexaClientSDK::avsCommon::utils::http2::HTTP2ConnectionInterface::disconnect
virtual void disconnect()=0
PromiseFuturePair.h
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::dequePostRequest
std::shared_ptr< MockHTTP2Request > dequePostRequest()
Definition: MockHTTP2Connection.cpp:223
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::respondToDownchannelRequests
bool respondToDownchannelRequests(long responseCode, bool sendResponseFinished, const std::chrono::milliseconds timeout)
Definition: MockHTTP2Connection.cpp:177
test
Definition: AmdMetricWrapperTest.cpp:24
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::waitForPostRequest
std::shared_ptr< MockHTTP2Request > waitForPostRequest(const std::chrono::milliseconds timeout)
Definition: MockHTTP2Connection.cpp:121
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::MockHTTP2Connection
MockHTTP2Connection(std::string dURL, std::string pingURL, bool delegateToReal=true)
Definition: MockHTTP2Connection.cpp:27
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::getPostRequestsNum
std::size_t getPostRequestsNum()
Definition: MockHTTP2Connection.cpp:211
alexaClientSDK
Whether or not curl logs should be emitted.
Definition: AVSConnectionManager.h:36
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::isRequestQueueEmpty
bool isRequestQueueEmpty()
Definition: MockHTTP2Connection.cpp:83
HTTP2ConnectionInterface.h
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::getDownchannelRequest
std::shared_ptr< MockHTTP2Request > getDownchannelRequest(std::chrono::milliseconds timeout=std::chrono::milliseconds(0))
Definition: MockHTTP2Connection.cpp:198
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection
Definition: MockHTTP2Connection.h:46
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::removeObserver
void removeObserver(std::shared_ptr< avsCommon::utils::http2::HTTP2ConnectionObserverInterface > observer)
Definition: MockHTTP2Connection.cpp:264
alexaClientSDK::avsCommon::utils::http2::HTTP2ConnectionInterface::createAndSendRequest
virtual std::shared_ptr< HTTP2RequestInterface > createAndSendRequest(const HTTP2RequestConfig &config)=0
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::waitForRequestWithHeader
bool waitForRequestWithHeader(std::chrono::milliseconds timeout)
Definition: MockHTTP2Connection.cpp:112
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::setResponseToPOSTRequests
void setResponseToPOSTRequests(http::HTTPResponseCode responseCode)
Definition: MockHTTP2Connection.cpp:193
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::getRequestsNum
std::size_t getRequestsNum()
Definition: MockHTTP2Connection.cpp:215
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::getMaxPostRequestsEnqueud
std::size_t getMaxPostRequestsEnqueud()
Definition: MockHTTP2Connection.cpp:256
HttpResponseCode.h
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::MOCK_METHOD0
MOCK_METHOD0(disconnect, void())
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::dequeRequest
std::shared_ptr< MockHTTP2Request > dequeRequest()
Definition: MockHTTP2Connection.cpp:100
alexaClientSDK::avsCommon::utils::http2::test::MockHTTP2Connection::setWaitRequestHeader
void setWaitRequestHeader(const std::string &matchString)
Definition: MockHTTP2Connection.cpp:107

AlexaClientSDK 1.26.0 - Copyright 2016-2022 Amazon.com, Inc. or its affiliates. All Rights Reserved. Licensed under the Apache License, Version 2.0