avs-device-sdk/ACL/test/Transport/MockHTTP2Connection.h

273 lines
9.3 KiB
C
Raw Normal View History

Version 1.10 alexa-client-sdk Changes in this update: **Enhancements** * New optional configuration for [EqualizerController](https://github.com/alexa/avs-device-sdk/blob/v1.10.0/Integration/AlexaClientSDKConfig.json#L154). The EqualizerController interface allows you to adjust equalizer settings on your product, such as decibel (dB) levels and modes. * Added reference implementation of the EqualizerController for GStreamer-based (MacOS, Linux, and Raspberry Pi) and OpenSL ES-based (Android) MediaPlayers. Note: In order to use with Android, it must support OpenSL ES. * New optional configuration for the [TemplateRuntime display card value](https://github.com/alexa/avs-device-sdk/blob/v1.10.0/Integration/AlexaClientSDKConfig.json#L144). * A configuration file generator script, `genConfig.sh` is now included with the SDK in the **tools/Install** directory. `genConfig.sh` and it's associated arguments populate `AlexaClientSDKConfig.json` with the data required to authorize with LWA. * Added Bluetooth A2DP source and AVRCP target support for Linux. * Added Amazon for Business (A4B) support, which includes support for handling the new [RevokeAuthorization](https://developer.amazon.com/docs/alexa-voice-service/system.html#revokeauth) directive in the Settings interface. A new CMake option has been added to enable A4B within the SDK, `-DA4B`. * Added locale support for IT and ES. * The Alexa Communication Library (ACL), `CBLAUthDelegate`, and sample app have been enhanced to detect de-authorization using the new `z` command. * Added `ExternalMediaPlayerObserver`, which receives notification of player state, track, and username changes. * `HTTP2ConnectionInterface` was factored out of `HTTP2Transport` to enable unit testing of `HTTP2Transport` and re-use of `HTTP2Connection` logic. **Bug Fixes** * Fixed a bug in which `ExternalMediaPlayer` adapter playback wasn't being recognized by AVS. * [Issue 973](https://github.com/alexa/avs-device-sdk/issues/973) - Fixed issues related to `AudioPlayer` where progress reports were being sent out of order or with incorrect offsets. * An `EXPECTING`, state has been added to `DialogUXState` in order to handle `EXPECT_SPEECH` state for hold-to-talk devices. * [Issue 948](https://github.com/alexa/avs-device-sdk/issues/948) - Fixed a bug in which the sample app was stuck in a listening state. * Fixed a bug where there was a delay between receiving a `DeleteAlert` directive, and deleting the alert. * [Issue 839](https://github.com/alexa/avs-device-sdk/issues/839) - Fixed an issue where speech was being truncated due to the `DialogUXStateAggregator` transitioning between a `THINKING` and `IDLE` state. * Fixed a bug in which the `AudioPlayer` attempted to play when it wasn't in the `FOREGROUND` focus. * `CapabilitiesDelegateTest` now works on Android. * [Issue 950](https://github.com/alexa/avs-device-sdk/issues/950) - Improved Android Media Player audio quality. * [Issue 908](https://github.com/alexa/avs-device-sdk/issues/908) - Fixed compile error on g++ 7.x in which includes were missing.
2018-10-24 17:01:29 +00:00
/*
* Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file 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.
*/
#ifndef ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKHTTP2CONNECTION_H_
#define ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKHTTP2CONNECTION_H_
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <deque>
#include <memory>
#include <mutex>
#include <string>
#include <gmock/gmock.h>
#include <gtest/gtest.h>
#include <ACL/Transport/MessageConsumerInterface.h>
#include <AVSCommon/Utils/PromiseFuturePair.h>
#include <AVSCommon/Utils/HTTP2/HTTP2ConnectionInterface.h>
#include <AVSCommon/Utils/LibcurlUtils/HttpResponseCodes.h>
#include "MockHTTP2Request.h"
namespace alexaClientSDK {
namespace avsCommon {
namespace utils {
namespace http2 {
namespace test {
/**
* A mock class of @c HTTP2ConnectionInterface.
*/
class MockHTTP2Connection : public HTTP2ConnectionInterface {
public:
/**
* Constructor
* @param dURL The URL for downchannel requests.
* @param pingURL The URL for ping requests.
*/
MockHTTP2Connection(std::string dURL, std::string pingURL);
~MockHTTP2Connection() = default;
/// @name HTTP2ConnectionInterface methods
/// @{
std::shared_ptr<HTTP2RequestInterface> createAndSendRequest(const HTTP2RequestConfig& config);
MOCK_METHOD0(disconnect, void());
/// @}
/**
* Check whether there are any HTTP requests sent.
*
* @return True if there are no HTTP requests sent by @c HTTP2Transport.
*/
bool isRequestQueueEmpty();
/**
* Wait for an HTTP request to be sent.
*
* @param timeout Wait timeout in milliseconds.
* @param requestNum The number of HTTP2 requests to wait for.
* @return A pointer to the request if there is one before the timeout occurs, otherwise nullptr
*/
std::shared_ptr<MockHTTP2Request> waitForRequest(std::chrono::milliseconds timeout, unsigned requestNum = 1);
/**
* Pop the latest HTTP2 Request from queue.
*
* @return The oldest HTTP2 Request in the queue.
*/
std::shared_ptr<MockHTTP2Request> dequeRequest();
/**
* Set the Header content to be matched by @c waitForRequestWithHeader.
*
* @param matchString The contents of the header to match.
*/
void setWaitRequestHeader(const std::string& matchString);
/**
* Wait for a request with a particular Header content.
*
* @param timeout The wait timeout in milliseconds
* @param matchString The contents of the header that it is waiting for
* @return True if a match occurred before timeout
*/
bool waitForRequestWithHeader(std::chrono::milliseconds timeout);
/**
* Wait for a POST HTTP2request.
*
* @param timeout The wait timeout in milliseconds.
* @return A pointer to the POST request if there is one before the timeout occurs, otherwise nullptr
*/
std::shared_ptr<MockHTTP2Request> waitForPostRequest(const std::chrono::milliseconds timeout);
/**
* Wait for a Ping HTTP2request.
*
* @param timeout The wait timeout in milliseconds.
* @return A pointer to the Ping request if there is one before the timeout occurs, otherwise nullptr
*/
std::shared_ptr<MockHTTP2Request> waitForPingRequest(const std::chrono::milliseconds timeout);
/**
* Respond to downchannel requests with a response code and notify onResponseFinished() if needed.
*
* @param responseCode The HTTP response code to reply.
* @param sendResponseFinished True if needed to notify onResponseFinished(), otherwise false.
* @param timeout Timeout for wating for downchannel HTTP2 requests.
* @return True if a response has been sent to the downchannel request before timeout, otherwise false.
*/
bool respondToDownchannelRequests(
long responseCode,
bool sendResponseFinished,
const std::chrono::milliseconds timeout);
/**
* Set the response code for POST requests that will be immediately replied when an HTTP POST request is sent.
*
* @param responseCode The HTTP response code to reply to the request. If set to @c
* HTTPResponseCode::HTTP_RESPONSE_CODE_UNDEFINED, a response code will not be sent.
*/
void setResponseToPOSTRequests(HTTPResponseCode responseCode);
/**
* Retrieve the first HTTP2 request made on the downchannel.
*
* @return The first downchannel request or nullptr if none found
*/
std::shared_ptr<MockHTTP2Request> getDownchannelRequest(
std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
/**
* Check if a pause is received while sending data.
*
* @return True if a pause is received, false otherwise
*/
bool isPauseOnSendReceived(std::chrono::milliseconds timeout = std::chrono::milliseconds(0));
/*
* Get the number of POST requests in the queue.
*
* @return The number of POST requests in the queue.
*/
std::size_t getPostRequestsNum();
/*
* Get the number of HTTP2 requests in the queue.
*
* @return The number of HTTP2 requests in the queue.
*/
std::size_t getRequestsNum();
/*
* Get the number of downchannel requests in the queue.
*
* @return The number of downchannel requests in the queue.
*/
std::size_t getDownchannelRequestsNum();
/*
* Pop the oldest HTTP2 POST request from queue if it is not empty, otherwise wait for at most @c timeout for a
* request to be enqueued
*
* @return The oldest HTTP2 POST request in the queue or nullptr if the timeout expires
*/
std::shared_ptr<MockHTTP2Request> dequePostRequest(const std::chrono::milliseconds timeout);
/*
* Pop the oldest HTTP2 POST request from queue.
*
* @return The oldest HTTP2 POST request in the queue.
*/
std::shared_ptr<MockHTTP2Request> dequePostRequest();
/*
* Pop the oldest HTTP2 Ping request from queue.
*
* @return The oldest HTTP2 Ping request in the queue.
*/
std::shared_ptr<MockHTTP2Request> dequePingRequest();
/*
* Retrieve the maximum number of POST requests in the queue at any given time.
*
* @return The maximum number of POST requests in the queue at any given time.
*/
std::size_t getMaxPostRequestsEnqueud();
private:
/// Queue of HTTP2 requests from @c HTTP2Transport. Serialized by @c m_requestMutex.
std::deque<std::shared_ptr<MockHTTP2Request>> m_requestQueue;
/// Serializes concurrent access to the m_requestQueue and m_isStopping members.
std::mutex m_requestMutex;
/// Used to wake a thread that processes HTTP2Request in @c m_requestQueue.
std::condition_variable m_requestCv;
/// A string that identifies the downchannel URL.
std::string m_downchannelURL;
/// A string that identifies the ping URL.
std::string m_pingURL;
/// Queue of HTTP2 requests that are only for the downchannel. Serialized by @c m_downchannelRequestMutex.
std::deque<std::shared_ptr<MockHTTP2Request>> m_downchannelRequestQueue;
/// Serializes access to receiving downchannel requests.
std::mutex m_downchannelRequestMutex;
// Used to wake a thread that is waiting for a downchannel request.
std::condition_variable m_downchannelRequestCv;
/// Queue of HTTP2 POST requests. Serialized by @c m_postRequestMutex.
std::deque<std::shared_ptr<MockHTTP2Request>> m_postRequestQueue;
/// Serializes access to receiving POST requests.
std::mutex m_postRequestMutex;
/// Used to wake a thread that is waiting a POST HTTP2Request.
std::condition_variable m_requestPostCv;
/// Queue of Ping requests. Serialized by @c m_pingRequestMutex.
std::deque<std::shared_ptr<MockHTTP2Request>> m_pingRequestQueue;
/// Serializes access to receiving Ping requests.
std::mutex m_pingRequestMutex;
// Used to wake a thread that is waiting for a Ping request.
std::condition_variable m_pingRequestCv;
/// A string that contains the header to match in @c waitForRequestWithHeader().
std::string m_headerMatch;
/// Used to wake a thread that processes HTTP2Request with a header content matching @c m_headerMatch.
std::condition_variable m_requestHeaderCv;
/// Indicator that a pause is received while doing @c HTTP2RequestSourceInterface::onSendData()
PromiseFuturePair<void> m_receivedPauseOnSend;
/// The response code to be replied for every POST request received.
HTTPResponseCode m_postResponseCode;
/// The maximum number of POST requests in the queue at any given time.
std::size_t m_maxPostRequestsEnqueued;
/// A constant to specify the buffer size to be used in reading HTTP2 data.
/// Set to 100 bytes for test purposes
static const unsigned READ_DATA_BUF_SIZE = 100;
};
} // namespace test
} // namespace http2
} // namespace utils
} // namespace avsCommon
} // namespace alexaClientSDK
#endif // ALEXA_CLIENT_SDK_ACL_TEST_TRANSPORT_MOCKHTTP2CONNECTION_H_