avs-device-sdk/AVSCommon/Utils/test/TaskQueueTest.cpp

285 lines
9.5 KiB
C++
Raw Normal View History

/*
Version 1.5.0 alexa-client-sdk Changes in this update: **Enhancements** * Added the `ExternalMediaPlayer` Capability Agent. This allows playback from music providers that control their own playback queue. Example: Spotify. * Added support for AU and NZ to the `SampleApp`. * Firmware version can now be sent to Alexa via the `SoftwareInfo` event. The firmware version is specified in the config file under the `sampleApp` object as an integer value named [`firmwareVersion`](https://github.com/alexa/avs-device-sdk/blob/master/Integration/AlexaClientSDKConfig.json#L52). * The new `f` command was added to the `SampleApp` which allows the firmware version to be updated at run-time. * Optional configuration changes have been introduced. Now a [default log level](https://github.com/alexa/avs-device-sdk/blob/master/Integration/AlexaClientSDKConfig.json#L93) can be set for `ACSDK_LOG_MODULE` components, globally or individually. This value is specified under a new root level configuration object called `logger`, and the value itself is named `logLevel`. This allows you to limit the degree of logging to that default value, such as `ERROR`or `INFO`. **Bug Fixes** * Fixed bug where `AudioPlayer` progress reports were not being sent, or were being sent incorrectly. * [Issue 408](https://github.com/alexa/avs-device-sdk/issues/408) - Irrelevant code related to `UrlSource` was removed from the `GStreamer-based MediaPlayer` implementation. * The `TZ` variable no longer needs to be set to `UTC` when building the `SampleApp`. * Fixed a bug where `CurlEasyHandleWrapper` logged unwanted data on failure conditions. * Fixed a bug to improve `SIGPIPE` handling. * Fixed a bug where the filename and classname were mismatched. Changed `UrlToAttachmentConverter.h` to `UrlContentToAttachmentConverter.h`,and `UrlToAttachmentConverter.cpp` to `UrlContentToAttachmentConverter.cpp` **Known Issues** * The `ACL` may encounter issues if audio attachments are received but not consumed. * Display Cards for Kindle don't render. * If using the GStreamer-based `MediaPlayer` implementation, after muting and un-muting an audio item, the next item in the queue will begin playing rather than continuing playback of the originally muted audio item. * `SpeechSynthesizerState` currently uses `GAINING_FOCUS` and `LOSING_FOCUS` as a workaround for handling intermediate state. These states may be removed in a future release. * Music playback doesn't immediately stop when a user barges-in on iHeartRadio.
2018-02-12 23:31:53 +00:00
* Copyright 2017-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.
*/
#include <gtest/gtest.h>
#include "ExecutorTestUtils.h"
#include "AVSCommon/Utils/Threading/Executor.h"
#include "AVSCommon/Utils/Threading/TaskQueue.h"
namespace alexaClientSDK {
namespace avsCommon {
namespace utils {
namespace threading {
namespace test {
Version 1.1.0 alexa-client-sdk - Changes in this update: - Better GStreamer error reporting. MediaPlayer used to only report `MEDIA_ERROR_UNKNOWN`, now reports more specific errors as defined in `ErrorType.h`. - Codebase has been formatted for easier reading. - `DirectiveRouter::removeDirectiveHandler()` signature changed and now returns a bool indicating if given handler should be successfully removed or not. - Cleanup of raw and shared pointers in the creation of `Transport` objects. - `HTTP2Stream`s now have IDs assigned as they are acquired as opposed to created, making associated logs easier to interpret. - `AlertsCapabilityAgent` has been refactored. - Alert management has been factored out into an `AlertScheduler` class. - Creation of Reminder (implements Alert) class. - Added new capability agent for `PlaybackController` with unit tests. - Added Settings interface with unit tests. - Return type of `getOffsetInMilliseconds()` changed from `int64_t` to `std::chronology::milliseconds`. - Added `AudioPlayer` unit tests. - Added teardown for all Integration tests except Alerts. - Implemented PlaylistParser. - Bug fixes: - AIP getting stuck in `LISTENING` or `THINKING` and refusing user input on network outage. - SampleApp crashing if running for 5 minutes after network disconnect. - Issue where on repeated user barge-ins, `AudioPlayer` would not pause. Specifically, the third attempt to “Play iHeartRadio” would not result in currently-playing music pausing. - Utterances being ignored after particularly long TTS. - GStreamer errors cropping up on SampleApp exit as a result of accessing the pipeline before it’s been setup. - Crashing when playing one URL after another. - Buffer overrun in Alerts Renderer. - [SampleApp crashing when issuing "Alexa skip" command with iHeartRadio.](https://github.com/alexa/avs-device-sdk/issues/153) - [`HTTP2Transport` network thread triggering a join on itself.](https://github.com/alexa/avs-device-sdk/issues/127) - [`HTTP2Stream` request handling truncating exception messages.](https://github.com/alexa/avs-device-sdk/issues/67) - [`AudioPlayer` was attempting an incorrect state transition from `STOPPED` to `PLAYING` through a `playbackResumed`.](https://github.com/alexa/avs-device-sdk/issues/138)
2017-10-02 22:59:05 +00:00
class TaskQueueTest : public ::testing::Test {
public:
/**
* Asserts that a call to pop on an empty queue is blocking, and will be awoken by a task being pushed onto
* the queue
*/
void testQueueBlocksWhenEmpty() {
// Have another thread blocked on the queue
auto future = std::async(std::launch::async, [=]() {
auto t = queue.pop();
return t->operator()();
});
// This is expected to timeout
auto failedStatus = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(failedStatus, std::future_status::timeout);
// Push a task to unblock the queue
auto pushFuture = queue.push(TASK, VALUE);
// This is expected to succeed
auto successStatus = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(successStatus, std::future_status::ready);
// Verify the pushed future behaved correctly
auto pushStatus = pushFuture.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(pushStatus, std::future_status::ready);
ASSERT_EQ(pushFuture.get(), VALUE);
}
TaskQueue queue;
};
TEST_F(TaskQueueTest, pushStdFunctionAndVerifyPopReturnsIt) {
std::function<void()> function([]() {});
auto future = queue.push(function);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushStdBindAndVerifyPopReturnsIt) {
auto future = queue.push(std::bind(exampleFunctionParams, 0));
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushLambdaAndVerifyPopReturnsIt) {
Version 1.1.0 alexa-client-sdk - Changes in this update: - Better GStreamer error reporting. MediaPlayer used to only report `MEDIA_ERROR_UNKNOWN`, now reports more specific errors as defined in `ErrorType.h`. - Codebase has been formatted for easier reading. - `DirectiveRouter::removeDirectiveHandler()` signature changed and now returns a bool indicating if given handler should be successfully removed or not. - Cleanup of raw and shared pointers in the creation of `Transport` objects. - `HTTP2Stream`s now have IDs assigned as they are acquired as opposed to created, making associated logs easier to interpret. - `AlertsCapabilityAgent` has been refactored. - Alert management has been factored out into an `AlertScheduler` class. - Creation of Reminder (implements Alert) class. - Added new capability agent for `PlaybackController` with unit tests. - Added Settings interface with unit tests. - Return type of `getOffsetInMilliseconds()` changed from `int64_t` to `std::chronology::milliseconds`. - Added `AudioPlayer` unit tests. - Added teardown for all Integration tests except Alerts. - Implemented PlaylistParser. - Bug fixes: - AIP getting stuck in `LISTENING` or `THINKING` and refusing user input on network outage. - SampleApp crashing if running for 5 minutes after network disconnect. - Issue where on repeated user barge-ins, `AudioPlayer` would not pause. Specifically, the third attempt to “Play iHeartRadio” would not result in currently-playing music pausing. - Utterances being ignored after particularly long TTS. - GStreamer errors cropping up on SampleApp exit as a result of accessing the pipeline before it’s been setup. - Crashing when playing one URL after another. - Buffer overrun in Alerts Renderer. - [SampleApp crashing when issuing "Alexa skip" command with iHeartRadio.](https://github.com/alexa/avs-device-sdk/issues/153) - [`HTTP2Transport` network thread triggering a join on itself.](https://github.com/alexa/avs-device-sdk/issues/127) - [`HTTP2Stream` request handling truncating exception messages.](https://github.com/alexa/avs-device-sdk/issues/67) - [`AudioPlayer` was attempting an incorrect state transition from `STOPPED` to `PLAYING` through a `playbackResumed`.](https://github.com/alexa/avs-device-sdk/issues/138)
2017-10-02 22:59:05 +00:00
auto future = queue.push([]() {});
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushFunctionPointerAndVerifyPopReturnsIt) {
auto future = queue.push(&exampleFunction);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushFunctorAndVerifyPopReturnsIt) {
ExampleFunctor exampleFunctor;
auto future = queue.push(exampleFunctor);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushFunctionWithPrimitiveReturnTypeNoArgsAndVerifyPopReturnsIt) {
int value = VALUE;
auto future = queue.push([=]() { return value; });
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
ASSERT_EQ(future.get(), value);
}
TEST_F(TaskQueueTest, pushFunctionWithObjectReturnTypeNoArgsAndVerifyPopReturnsIt) {
SimpleObject value(VALUE);
auto future = queue.push([=]() { return value; });
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
ASSERT_EQ(future.get().getValue(), value.getValue());
}
TEST_F(TaskQueueTest, pushFunctionWithNoReturnTypePrimitiveArgsAndVerifyPopReturnsIt) {
int value = VALUE;
Version 1.1.0 alexa-client-sdk - Changes in this update: - Better GStreamer error reporting. MediaPlayer used to only report `MEDIA_ERROR_UNKNOWN`, now reports more specific errors as defined in `ErrorType.h`. - Codebase has been formatted for easier reading. - `DirectiveRouter::removeDirectiveHandler()` signature changed and now returns a bool indicating if given handler should be successfully removed or not. - Cleanup of raw and shared pointers in the creation of `Transport` objects. - `HTTP2Stream`s now have IDs assigned as they are acquired as opposed to created, making associated logs easier to interpret. - `AlertsCapabilityAgent` has been refactored. - Alert management has been factored out into an `AlertScheduler` class. - Creation of Reminder (implements Alert) class. - Added new capability agent for `PlaybackController` with unit tests. - Added Settings interface with unit tests. - Return type of `getOffsetInMilliseconds()` changed from `int64_t` to `std::chronology::milliseconds`. - Added `AudioPlayer` unit tests. - Added teardown for all Integration tests except Alerts. - Implemented PlaylistParser. - Bug fixes: - AIP getting stuck in `LISTENING` or `THINKING` and refusing user input on network outage. - SampleApp crashing if running for 5 minutes after network disconnect. - Issue where on repeated user barge-ins, `AudioPlayer` would not pause. Specifically, the third attempt to “Play iHeartRadio” would not result in currently-playing music pausing. - Utterances being ignored after particularly long TTS. - GStreamer errors cropping up on SampleApp exit as a result of accessing the pipeline before it’s been setup. - Crashing when playing one URL after another. - Buffer overrun in Alerts Renderer. - [SampleApp crashing when issuing "Alexa skip" command with iHeartRadio.](https://github.com/alexa/avs-device-sdk/issues/153) - [`HTTP2Transport` network thread triggering a join on itself.](https://github.com/alexa/avs-device-sdk/issues/127) - [`HTTP2Stream` request handling truncating exception messages.](https://github.com/alexa/avs-device-sdk/issues/67) - [`AudioPlayer` was attempting an incorrect state transition from `STOPPED` to `PLAYING` through a `playbackResumed`.](https://github.com/alexa/avs-device-sdk/issues/138)
2017-10-02 22:59:05 +00:00
auto future = queue.push([](int number) {}, value);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushFunctionWithNoReturnTypeObjectArgsAndVerifyPopReturnsIt) {
SimpleObject arg(0);
Version 1.1.0 alexa-client-sdk - Changes in this update: - Better GStreamer error reporting. MediaPlayer used to only report `MEDIA_ERROR_UNKNOWN`, now reports more specific errors as defined in `ErrorType.h`. - Codebase has been formatted for easier reading. - `DirectiveRouter::removeDirectiveHandler()` signature changed and now returns a bool indicating if given handler should be successfully removed or not. - Cleanup of raw and shared pointers in the creation of `Transport` objects. - `HTTP2Stream`s now have IDs assigned as they are acquired as opposed to created, making associated logs easier to interpret. - `AlertsCapabilityAgent` has been refactored. - Alert management has been factored out into an `AlertScheduler` class. - Creation of Reminder (implements Alert) class. - Added new capability agent for `PlaybackController` with unit tests. - Added Settings interface with unit tests. - Return type of `getOffsetInMilliseconds()` changed from `int64_t` to `std::chronology::milliseconds`. - Added `AudioPlayer` unit tests. - Added teardown for all Integration tests except Alerts. - Implemented PlaylistParser. - Bug fixes: - AIP getting stuck in `LISTENING` or `THINKING` and refusing user input on network outage. - SampleApp crashing if running for 5 minutes after network disconnect. - Issue where on repeated user barge-ins, `AudioPlayer` would not pause. Specifically, the third attempt to “Play iHeartRadio” would not result in currently-playing music pausing. - Utterances being ignored after particularly long TTS. - GStreamer errors cropping up on SampleApp exit as a result of accessing the pipeline before it’s been setup. - Crashing when playing one URL after another. - Buffer overrun in Alerts Renderer. - [SampleApp crashing when issuing "Alexa skip" command with iHeartRadio.](https://github.com/alexa/avs-device-sdk/issues/153) - [`HTTP2Transport` network thread triggering a join on itself.](https://github.com/alexa/avs-device-sdk/issues/127) - [`HTTP2Stream` request handling truncating exception messages.](https://github.com/alexa/avs-device-sdk/issues/67) - [`AudioPlayer` was attempting an incorrect state transition from `STOPPED` to `PLAYING` through a `playbackResumed`.](https://github.com/alexa/avs-device-sdk/issues/138)
2017-10-02 22:59:05 +00:00
auto future = queue.push([](SimpleObject object) {}, arg);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushFunctionWithPrimitiveReturnTypeObjectArgsAndVerifyPopReturnsIt) {
int value = VALUE;
SimpleObject arg(0);
auto future = queue.push([=](SimpleObject object) { return value; }, arg);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
ASSERT_EQ(future.get(), value);
}
TEST_F(TaskQueueTest, pushFunctionWithObjectReturnTypePrimitiveArgsAndVerifyPopReturnsIt) {
int arg = 0;
SimpleObject value(VALUE);
auto future = queue.push([=](int primitive) { return value; }, arg);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
ASSERT_EQ(future.get().getValue(), value.getValue());
}
TEST_F(TaskQueueTest, pushFunctionWithPrimitiveReturnTypePrimitiveArgsAndVerifyPopReturnsIt) {
int arg = 0;
int value = VALUE;
auto future = queue.push([=](int number) { return value; }, arg);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
ASSERT_EQ(future.get(), value);
}
TEST_F(TaskQueueTest, pushFunctionWithObjectReturnTypeObjectArgsAndVerifyPopReturnsIt) {
SimpleObject value(VALUE);
SimpleObject arg(0);
auto future = queue.push([=](SimpleObject object) { return value; }, arg);
auto task = queue.pop();
task->operator()();
auto future_status = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(future_status, std::future_status::ready);
ASSERT_EQ(future.get().getValue(), value.getValue());
}
TEST_F(TaskQueueTest, verifyFirstInFirstOutOrderIsMaintained) {
int argOne = 1;
int argTwo = 2;
int argThree = 3;
int argFour = 4;
auto futureOne = queue.push(TASK, argOne);
auto futureTwo = queue.push(TASK, argTwo);
auto futureThree = queue.push(TASK, argThree);
auto futureFour = queue.push(TASK, argFour);
auto taskOne = queue.pop();
auto taskTwo = queue.pop();
auto taskThree = queue.pop();
auto taskFour = queue.pop();
taskOne->operator()();
taskTwo->operator()();
taskThree->operator()();
taskFour->operator()();
auto futureStatusOne = futureOne.wait_for(SHORT_TIMEOUT_MS);
auto futureStatusTwo = futureTwo.wait_for(SHORT_TIMEOUT_MS);
auto futureStatusThree = futureThree.wait_for(SHORT_TIMEOUT_MS);
auto futureStatusFour = futureFour.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(futureStatusOne, std::future_status::ready);
ASSERT_EQ(futureStatusTwo, std::future_status::ready);
ASSERT_EQ(futureStatusThree, std::future_status::ready);
ASSERT_EQ(futureStatusFour, std::future_status::ready);
ASSERT_EQ(futureOne.get(), argOne);
ASSERT_EQ(futureTwo.get(), argTwo);
ASSERT_EQ(futureThree.get(), argThree);
ASSERT_EQ(futureFour.get(), argFour);
}
TEST_F(TaskQueueTest, popBlocksOnInitiallyEmptyQueue) {
testQueueBlocksWhenEmpty();
}
TEST_F(TaskQueueTest, popBlocksOnEmptyQueueAfterAllTasksArePopped) {
// Put a task on the queue, and take it off to get back to empty
auto futureOne = queue.push(TASK, VALUE);
auto taskOne = queue.pop();
taskOne->operator()();
auto futureOneStatus = futureOne.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(futureOneStatus, std::future_status::ready);
ASSERT_EQ(futureOne.get(), VALUE);
testQueueBlocksWhenEmpty();
}
TEST_F(TaskQueueTest, isShutdownReturnsFalseWhenRunning) {
ASSERT_EQ(queue.isShutdown(), false);
}
TEST_F(TaskQueueTest, isShutdownReturnsTrueAfterShutdown) {
queue.shutdown();
ASSERT_EQ(queue.isShutdown(), true);
}
TEST_F(TaskQueueTest, shutdownUnblocksAnEmptyQueue) {
// Have another thread blocked on the queue
auto future = std::async(std::launch::async, [=]() {
auto t = queue.pop();
if (t) t->operator()();
});
// This is expected to timeout
auto failedStatus = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(failedStatus, std::future_status::timeout);
// Shutdown to unblock the queue
queue.shutdown();
// This is expected to succeed
auto successStatus = future.wait_for(SHORT_TIMEOUT_MS);
ASSERT_EQ(successStatus, std::future_status::ready);
}
TEST_F(TaskQueueTest, pushFailsToEnqueueANewTaskOnAShutdownQueue) {
// No tasks should be enqueued
queue.shutdown();
auto future = queue.push(TASK, VALUE);
ASSERT_EQ(future.valid(), false);
auto retrievedTask = queue.pop();
ASSERT_EQ(retrievedTask, nullptr);
}
Version 1.1.0 alexa-client-sdk - Changes in this update: - Better GStreamer error reporting. MediaPlayer used to only report `MEDIA_ERROR_UNKNOWN`, now reports more specific errors as defined in `ErrorType.h`. - Codebase has been formatted for easier reading. - `DirectiveRouter::removeDirectiveHandler()` signature changed and now returns a bool indicating if given handler should be successfully removed or not. - Cleanup of raw and shared pointers in the creation of `Transport` objects. - `HTTP2Stream`s now have IDs assigned as they are acquired as opposed to created, making associated logs easier to interpret. - `AlertsCapabilityAgent` has been refactored. - Alert management has been factored out into an `AlertScheduler` class. - Creation of Reminder (implements Alert) class. - Added new capability agent for `PlaybackController` with unit tests. - Added Settings interface with unit tests. - Return type of `getOffsetInMilliseconds()` changed from `int64_t` to `std::chronology::milliseconds`. - Added `AudioPlayer` unit tests. - Added teardown for all Integration tests except Alerts. - Implemented PlaylistParser. - Bug fixes: - AIP getting stuck in `LISTENING` or `THINKING` and refusing user input on network outage. - SampleApp crashing if running for 5 minutes after network disconnect. - Issue where on repeated user barge-ins, `AudioPlayer` would not pause. Specifically, the third attempt to “Play iHeartRadio” would not result in currently-playing music pausing. - Utterances being ignored after particularly long TTS. - GStreamer errors cropping up on SampleApp exit as a result of accessing the pipeline before it’s been setup. - Crashing when playing one URL after another. - Buffer overrun in Alerts Renderer. - [SampleApp crashing when issuing "Alexa skip" command with iHeartRadio.](https://github.com/alexa/avs-device-sdk/issues/153) - [`HTTP2Transport` network thread triggering a join on itself.](https://github.com/alexa/avs-device-sdk/issues/127) - [`HTTP2Stream` request handling truncating exception messages.](https://github.com/alexa/avs-device-sdk/issues/67) - [`AudioPlayer` was attempting an incorrect state transition from `STOPPED` to `PLAYING` through a `playbackResumed`.](https://github.com/alexa/avs-device-sdk/issues/138)
2017-10-02 22:59:05 +00:00
} // namespace test
} // namespace threading
} // namespace utils
} // namespace avsCommon
} // namespace alexaClientSDK