2017-06-23 23:26:34 +00:00
|
|
|
/*
|
2018-02-12 23:31:53 +00:00
|
|
|
* Copyright 2017-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2017-02-10 23:39:10 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2018-02-12 23:31:53 +00:00
|
|
|
|
2017-02-10 23:39:10 +00:00
|
|
|
#include "MessageRouterTest.h"
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
|
|
|
|
namespace alexaClientSDK {
|
|
|
|
namespace acl {
|
2017-06-09 23:23:31 +00:00
|
|
|
namespace test {
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-06-23 23:26:34 +00:00
|
|
|
using namespace alexaClientSDK::avsCommon::sdkInterfaces;
|
|
|
|
|
2017-02-10 23:39:10 +00:00
|
|
|
TEST_F(MessageRouterTest, getConnectionStatusReturnsDisconnectedBeforeConnect) {
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(m_router->getConnectionStatus().first, ConnectionStatusObserverInterface::Status::DISCONNECTED);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, getConnectionStatusReturnsPendingAfterConnectingStarts) {
|
|
|
|
setupStateToPending();
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(m_router->getConnectionStatus().first, ConnectionStatusObserverInterface::Status::PENDING);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, getConnectionStatusReturnsConnectedAfterConnectionEstablished) {
|
|
|
|
setupStateToConnected();
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(m_router->getConnectionStatus().first, ConnectionStatusObserverInterface::Status::CONNECTED);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, getConnectionStatusReturnsConnectedAfterDisconnected) {
|
2017-11-17 02:49:28 +00:00
|
|
|
m_router->onDisconnected(m_mockTransport, ConnectionStatusObserverInterface::ChangedReason::ACL_DISABLED);
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(m_router->getConnectionStatus().first, ConnectionStatusObserverInterface::Status::DISCONNECTED);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, ensureTheMessageRouterObserverIsInformedOfConnectionPendingAfterConnect) {
|
|
|
|
setupStateToPending();
|
|
|
|
|
2017-06-23 23:26:34 +00:00
|
|
|
// wait for the result to propagate by scheduling a task on the client executor
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionStatus(), ConnectionStatusObserverInterface::Status::PENDING);
|
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionChangedReason(),
|
|
|
|
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, ensureTheMessageRouterObserverIsInformedOfNewConnection) {
|
|
|
|
setupStateToConnected();
|
|
|
|
|
2017-06-23 23:26:34 +00:00
|
|
|
// wait for the result to propagate by scheduling a task on the client executor
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionStatus(), ConnectionStatusObserverInterface::Status::CONNECTED);
|
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionChangedReason(),
|
|
|
|
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, ensureTheMessageRouterObserverIsInformedOfTransportDisconnection) {
|
|
|
|
setupStateToConnected();
|
|
|
|
|
2017-06-09 23:23:31 +00:00
|
|
|
auto reason = ConnectionStatusObserverInterface::ChangedReason::ACL_DISABLED;
|
2017-02-10 23:39:10 +00:00
|
|
|
disconnectMockTransport(m_mockTransport.get());
|
2017-11-17 02:49:28 +00:00
|
|
|
m_router->onDisconnected(m_mockTransport, reason);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-06-23 23:26:34 +00:00
|
|
|
// wait for the result to propagate by scheduling a task on the client executor
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionStatus(), ConnectionStatusObserverInterface::Status::PENDING);
|
2017-02-10 23:39:10 +00:00
|
|
|
ASSERT_EQ(m_mockMessageRouterObserver->getLatestConnectionChangedReason(), reason);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, ensureTheMessageRouterObserverIsInformedOfRouterDisconnection) {
|
|
|
|
setupStateToConnected();
|
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->disable();
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-06-23 23:26:34 +00:00
|
|
|
// wait for the result to propagate by scheduling a task on the client executor
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionStatus(),
|
|
|
|
ConnectionStatusObserverInterface::Status::DISCONNECTED);
|
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionChangedReason(),
|
|
|
|
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, sendIsSuccessfulWhenConnected) {
|
|
|
|
setupStateToConnected();
|
|
|
|
|
|
|
|
auto messageRequest = createMessageRequest();
|
|
|
|
|
|
|
|
// Expect to have the message sent to the transport
|
|
|
|
EXPECT_CALL(*m_mockTransport, send(messageRequest)).Times(1);
|
|
|
|
|
2017-08-08 00:04:43 +00:00
|
|
|
// TODO: ACSDK-421: Revert this to use send().
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->sendMessage(messageRequest);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
// Since we connected we will be disconnected when the router is destroyed
|
|
|
|
EXPECT_CALL(*m_mockTransport, disconnect()).Times(AnyNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, sendFailsWhenDisconnected) {
|
|
|
|
auto messageRequest = createMessageRequest();
|
|
|
|
|
|
|
|
// Expect to have the message sent to the transport
|
|
|
|
EXPECT_CALL(*m_mockTransport, send(messageRequest)).Times(0);
|
|
|
|
|
2017-08-08 00:04:43 +00:00
|
|
|
// TODO: ACSDK-421: Revert this to use send().
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->sendMessage(messageRequest);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, sendFailsWhenPending) {
|
|
|
|
// Ensure a transport exists
|
|
|
|
initializeMockTransport(m_mockTransport.get());
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->enable();
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
auto messageRequest = createMessageRequest();
|
|
|
|
|
2017-03-10 00:01:46 +00:00
|
|
|
// Expect to have the message sent to the transport.
|
2017-06-23 23:26:34 +00:00
|
|
|
EXPECT_CALL(*m_mockTransport, send(messageRequest)).Times(1);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-08-08 00:04:43 +00:00
|
|
|
// TODO: ACSDK-421: Revert this to use send().
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->sendMessage(messageRequest);
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, sendMessageDoesNotSendAfterDisconnected) {
|
|
|
|
setupStateToConnected();
|
|
|
|
|
|
|
|
auto messageRequest = createMessageRequest();
|
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
EXPECT_CALL(*m_mockTransport, doShutdown()).Times(AtLeast(1));
|
|
|
|
m_router->disable();
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
// Expect to have the message sent to the transport
|
|
|
|
EXPECT_CALL(*m_mockTransport, send(messageRequest)).Times(0);
|
|
|
|
|
2017-08-08 00:04:43 +00:00
|
|
|
// TODO: ACSDK-421: Revert this to use send().
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->sendMessage(messageRequest);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, disconnectDisconnectsConnectedTransports) {
|
|
|
|
setupStateToConnected();
|
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
EXPECT_CALL(*m_mockTransport, doShutdown()).Times(1);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->disable();
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(MessageRouterTest, serverSideDisconnectCreatesANewTransport) {
|
|
|
|
/*
|
|
|
|
* This test is difficult to setup in a nice way. The idea is to replace the original
|
|
|
|
* transport with a new one, call onServerSideDisconnect to make it the new active
|
|
|
|
* transport, and then send a message. The message should be sent on the new transport.
|
|
|
|
*/
|
|
|
|
setupStateToConnected();
|
|
|
|
|
|
|
|
auto oldTransport = m_mockTransport;
|
|
|
|
|
|
|
|
auto newTransport = std::make_shared<NiceMock<MockTransport>>();
|
|
|
|
initializeMockTransport(newTransport.get());
|
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->setMockTransport(newTransport);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
// Reset the MessageRouterObserver, there should be no interactions with the observer
|
2017-11-17 02:49:28 +00:00
|
|
|
m_router->onServerSideDisconnect(oldTransport);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionStatus(), ConnectionStatusObserverInterface::Status::PENDING);
|
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionChangedReason(),
|
|
|
|
ConnectionStatusObserverInterface::ChangedReason::SERVER_SIDE_DISCONNECT);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
// mock the new transports connection
|
|
|
|
connectMockTransport(newTransport.get());
|
2017-11-17 02:49:28 +00:00
|
|
|
m_router->onConnected(newTransport);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-10-02 22:59:05 +00:00
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionStatus(), ConnectionStatusObserverInterface::Status::CONNECTED);
|
|
|
|
ASSERT_EQ(
|
|
|
|
m_mockMessageRouterObserver->getLatestConnectionChangedReason(),
|
|
|
|
ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
// mock the old transport disconnecting completely
|
|
|
|
disconnectMockTransport(oldTransport.get());
|
2017-11-17 02:49:28 +00:00
|
|
|
m_router->onDisconnected(oldTransport, ConnectionStatusObserverInterface::ChangedReason::ACL_CLIENT_REQUEST);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
|
|
|
auto messageRequest = createMessageRequest();
|
|
|
|
|
|
|
|
EXPECT_CALL(*oldTransport.get(), send(messageRequest)).Times(0);
|
|
|
|
|
|
|
|
EXPECT_CALL(*newTransport.get(), send(messageRequest)).Times(1);
|
|
|
|
|
2017-08-08 00:04:43 +00:00
|
|
|
// TODO: ACSDK-421: Revert this to use send().
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->sendMessage(messageRequest);
|
2017-02-10 23:39:10 +00:00
|
|
|
|
2017-07-18 22:25:37 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
2017-02-10 23:39:10 +00:00
|
|
|
}
|
|
|
|
|
2017-08-08 00:04:43 +00:00
|
|
|
/**
|
|
|
|
* This tests the calling of private method @c receive() for MessageRouterObserver from MessageRouter
|
|
|
|
*/
|
|
|
|
TEST_F(MessageRouterTest, onReceiveTest) {
|
|
|
|
m_mockMessageRouterObserver->reset();
|
2017-10-02 22:59:05 +00:00
|
|
|
m_router->consumeMessage(CONTEXT_ID, MESSAGE);
|
2017-08-08 00:04:43 +00:00
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
|
|
|
ASSERT_TRUE(m_mockMessageRouterObserver->wasNotifiedOfReceive());
|
|
|
|
ASSERT_EQ(CONTEXT_ID, m_mockMessageRouterObserver->getAttachmentContextId());
|
|
|
|
ASSERT_EQ(MESSAGE, m_mockMessageRouterObserver->getLatestMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This tests the calling of private method @c onConnectionStatusChanged()
|
|
|
|
* for MessageRouterObserver from MessageRouter
|
|
|
|
*/
|
|
|
|
TEST_F(MessageRouterTest, onConnectionStatusChangedTest) {
|
|
|
|
m_mockMessageRouterObserver->reset();
|
|
|
|
setupStateToConnected();
|
|
|
|
waitOnMessageRouter(SHORT_TIMEOUT_MS);
|
|
|
|
ASSERT_TRUE(m_mockMessageRouterObserver->wasNotifiedOfStatusChange());
|
|
|
|
}
|
2017-10-02 22:59:05 +00:00
|
|
|
} // namespace test
|
|
|
|
} // namespace acl
|
|
|
|
} // namespace alexaClientSDK
|