AWSGameLiftClientLocalTicketTrackerTest.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/std/smart_ptr/make_shared.h>
  9. #include <AWSGameLiftClientFixture.h>
  10. #include <AWSGameLiftClientLocalTicketTracker.h>
  11. #include <AWSGameLiftClientMocks.h>
  12. #include <Request/IAWSGameLiftInternalRequests.h>
  13. using namespace AWSGameLift;
  14. static constexpr const uint64_t TEST_RACKER_POLLING_PERIOD_MS = 1000;
  15. static constexpr const uint64_t TEST_WAIT_BUFFER_TIME_MS = 10;
  16. static constexpr const uint64_t TEST_WAIT_MAXIMUM_TIME_MS = 10000;
  17. class TestAWSGameLiftClientLocalTicketTracker
  18. : public AWSGameLiftClientLocalTicketTracker
  19. {
  20. public:
  21. TestAWSGameLiftClientLocalTicketTracker() = default;
  22. virtual ~TestAWSGameLiftClientLocalTicketTracker() = default;
  23. void SetUp()
  24. {
  25. ActivateTracker();
  26. m_pollingPeriodInMS = TEST_RACKER_POLLING_PERIOD_MS;
  27. }
  28. void TearDown()
  29. {
  30. DeactivateTracker();
  31. }
  32. bool IsTrackerIdle()
  33. {
  34. return m_status == TicketTrackerStatus::Idle;
  35. }
  36. };
  37. class AWSGameLiftClientLocalTicketTrackerTest
  38. : public AWSGameLiftClientFixture
  39. , public IAWSGameLiftInternalRequests
  40. {
  41. protected:
  42. void SetUp() override
  43. {
  44. AWSGameLiftClientFixture::SetUp();
  45. AZ::Interface<IAWSGameLiftInternalRequests>::Register(this);
  46. m_gameliftClientMockPtr = AZStd::make_shared<GameLiftClientMock>();
  47. m_gameliftClientTicketTracker = AZStd::make_unique<TestAWSGameLiftClientLocalTicketTracker>();
  48. m_gameliftClientTicketTracker->SetUp();
  49. }
  50. void TearDown() override
  51. {
  52. m_gameliftClientTicketTracker->TearDown();
  53. m_gameliftClientTicketTracker.reset();
  54. m_gameliftClientMockPtr.reset();
  55. AZ::Interface<IAWSGameLiftInternalRequests>::Unregister(this);
  56. AWSGameLiftClientFixture::TearDown();
  57. }
  58. AZStd::shared_ptr<Aws::GameLift::GameLiftClient> GetGameLiftClient() const
  59. {
  60. return m_gameliftClientMockPtr;
  61. }
  62. void SetGameLiftClient(AZStd::shared_ptr<Aws::GameLift::GameLiftClient> gameliftClient)
  63. {
  64. AZ_UNUSED(gameliftClient);
  65. m_gameliftClientMockPtr.reset();
  66. }
  67. void WaitForProcessFinish(AZStd::function<bool()> processFinishCondition)
  68. {
  69. int processingTime = 0;
  70. while (processingTime < TEST_WAIT_MAXIMUM_TIME_MS)
  71. {
  72. if (processFinishCondition())
  73. {
  74. return;
  75. }
  76. AZStd::this_thread::sleep_for(AZStd::chrono::milliseconds(TEST_WAIT_BUFFER_TIME_MS));
  77. processingTime += TEST_WAIT_BUFFER_TIME_MS;
  78. }
  79. }
  80. public:
  81. AZStd::unique_ptr<TestAWSGameLiftClientLocalTicketTracker> m_gameliftClientTicketTracker;
  82. AZStd::shared_ptr<GameLiftClientMock> m_gameliftClientMockPtr;
  83. };
  84. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallWithoutClientSetup_GetExpectedErrors)
  85. {
  86. AZ::Interface<IAWSGameLiftInternalRequests>::Get()->SetGameLiftClient(nullptr);
  87. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  88. AZ_TEST_START_TRACE_SUPPRESSION;
  89. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  90. WaitForProcessFinish([&](){ return matchmakingHandlerMock.m_numMatchError == 1; });
  91. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  92. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchError == 1);
  93. ASSERT_FALSE(m_gameliftClientTicketTracker->IsTrackerIdle());
  94. }
  95. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_MultipleCallsWithoutClientSetup_GetExpectedErrors)
  96. {
  97. AZ::Interface<IAWSGameLiftInternalRequests>::Get()->SetGameLiftClient(nullptr);
  98. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  99. AZ_TEST_START_TRACE_SUPPRESSION;
  100. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  101. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  102. WaitForProcessFinish([&](){ return matchmakingHandlerMock.m_numMatchError == 1; });
  103. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  104. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchError == 1);
  105. ASSERT_FALSE(m_gameliftClientTicketTracker->IsTrackerIdle());
  106. }
  107. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallButWithFailedOutcome_GetExpectedErrors)
  108. {
  109. Aws::Client::AWSError<Aws::GameLift::GameLiftErrors> error;
  110. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(error);
  111. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  112. .Times(1)
  113. .WillOnce(::testing::Return(outcome));
  114. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  115. AZ_TEST_START_TRACE_SUPPRESSION;
  116. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  117. WaitForProcessFinish([&](){ return matchmakingHandlerMock.m_numMatchError == 1; });
  118. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  119. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchError == 1);
  120. ASSERT_FALSE(m_gameliftClientTicketTracker->IsTrackerIdle());
  121. }
  122. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallWithMoreThanOneTicket_GetExpectedErrors)
  123. {
  124. Aws::GameLift::Model::DescribeMatchmakingResult result;
  125. result.AddTicketList(Aws::GameLift::Model::MatchmakingTicket());
  126. result.AddTicketList(Aws::GameLift::Model::MatchmakingTicket());
  127. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  128. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  129. .Times(1)
  130. .WillOnce(::testing::Return(outcome));
  131. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  132. AZ_TEST_START_TRACE_SUPPRESSION;
  133. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  134. WaitForProcessFinish([&](){ return matchmakingHandlerMock.m_numMatchError == 1; });
  135. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  136. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchError == 1);
  137. ASSERT_FALSE(m_gameliftClientTicketTracker->IsTrackerIdle());
  138. }
  139. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallWithCompleteStatus_ProcessStopsAndGetExpectedResult)
  140. {
  141. Aws::GameLift::Model::GameSessionConnectionInfo connectionInfo;
  142. connectionInfo.SetIpAddress("DummyIpAddress");
  143. connectionInfo.SetPort(123);
  144. connectionInfo.AddMatchedPlayerSessions(
  145. Aws::GameLift::Model::MatchedPlayerSession()
  146. .WithPlayerId("player1")
  147. .WithPlayerSessionId("playersession1"));
  148. Aws::GameLift::Model::MatchmakingTicket ticket;
  149. ticket.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::COMPLETED);
  150. ticket.SetGameSessionConnectionInfo(connectionInfo);
  151. Aws::GameLift::Model::DescribeMatchmakingResult result;
  152. result.AddTicketList(ticket);
  153. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  154. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  155. .Times(1)
  156. .WillOnce(::testing::Return(outcome));
  157. SessionHandlingClientRequestsMock sessionHandlerMock;
  158. EXPECT_CALL(sessionHandlerMock, RequestPlayerJoinSession(::testing::_))
  159. .Times(1)
  160. .WillOnce(::testing::Return(true));
  161. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  162. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  163. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  164. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchComplete == 1);
  165. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  166. }
  167. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallButNoPlayerSession_ProcessStopsAndGetExpectedError)
  168. {
  169. Aws::GameLift::Model::GameSessionConnectionInfo connectionInfo;
  170. connectionInfo.SetIpAddress("DummyIpAddress");
  171. connectionInfo.SetPort(123);
  172. Aws::GameLift::Model::MatchmakingTicket ticket;
  173. ticket.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::COMPLETED);
  174. ticket.SetGameSessionConnectionInfo(connectionInfo);
  175. Aws::GameLift::Model::DescribeMatchmakingResult result;
  176. result.AddTicketList(ticket);
  177. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  178. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  179. .Times(1)
  180. .WillOnce(::testing::Return(outcome));
  181. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  182. AZ_TEST_START_TRACE_SUPPRESSION;
  183. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  184. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  185. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  186. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchComplete == 1);
  187. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  188. }
  189. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallButFailedToJoinMatch_ProcessStopsAndGetExpectedError)
  190. {
  191. Aws::GameLift::Model::GameSessionConnectionInfo connectionInfo;
  192. connectionInfo.SetIpAddress("DummyIpAddress");
  193. connectionInfo.SetPort(123);
  194. connectionInfo.AddMatchedPlayerSessions(
  195. Aws::GameLift::Model::MatchedPlayerSession()
  196. .WithPlayerId("player1")
  197. .WithPlayerSessionId("playersession1"));
  198. Aws::GameLift::Model::MatchmakingTicket ticket;
  199. ticket.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::COMPLETED);
  200. ticket.SetGameSessionConnectionInfo(connectionInfo);
  201. Aws::GameLift::Model::DescribeMatchmakingResult result;
  202. result.AddTicketList(ticket);
  203. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  204. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  205. .Times(1)
  206. .WillOnce(::testing::Return(outcome));
  207. SessionHandlingClientRequestsMock sessionHandlerMock;
  208. EXPECT_CALL(sessionHandlerMock, RequestPlayerJoinSession(::testing::_))
  209. .Times(1)
  210. .WillOnce(::testing::Return(false));
  211. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  212. AZ_TEST_START_TRACE_SUPPRESSION;
  213. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  214. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  215. AZ_TEST_STOP_TRACE_SUPPRESSION(1);
  216. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchComplete == 1);
  217. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  218. }
  219. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallButTicketTimeOut_ProcessStopsAndGetExpectedError)
  220. {
  221. Aws::GameLift::Model::MatchmakingTicket ticket;
  222. ticket.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::TIMED_OUT);
  223. Aws::GameLift::Model::DescribeMatchmakingResult result;
  224. result.AddTicketList(ticket);
  225. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  226. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  227. .Times(1)
  228. .WillOnce(::testing::Return(outcome));
  229. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  230. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  231. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  232. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchFailure == 1);
  233. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  234. }
  235. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallButTicketFailed_ProcessStopsAndGetExpectedError)
  236. {
  237. Aws::GameLift::Model::MatchmakingTicket ticket;
  238. ticket.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::FAILED);
  239. Aws::GameLift::Model::DescribeMatchmakingResult result;
  240. result.AddTicketList(ticket);
  241. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  242. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  243. .Times(1)
  244. .WillOnce(::testing::Return(outcome));
  245. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  246. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  247. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  248. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchFailure == 1);
  249. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  250. }
  251. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallButTicketCancelled_ProcessStopsAndGetExpectedError)
  252. {
  253. Aws::GameLift::Model::MatchmakingTicket ticket;
  254. ticket.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::CANCELLED);
  255. Aws::GameLift::Model::DescribeMatchmakingResult result;
  256. result.AddTicketList(ticket);
  257. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome(result);
  258. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  259. .Times(1)
  260. .WillOnce(::testing::Return(outcome));
  261. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  262. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  263. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  264. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchFailure == 1);
  265. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  266. }
  267. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_CallAndTicketCompleteAtLast_ProcessContinuesAndStop)
  268. {
  269. Aws::GameLift::Model::MatchmakingTicket ticket1;
  270. ticket1.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::QUEUED);
  271. Aws::GameLift::Model::DescribeMatchmakingResult result1;
  272. result1.AddTicketList(ticket1);
  273. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome1(result1);
  274. Aws::GameLift::Model::GameSessionConnectionInfo connectionInfo;
  275. connectionInfo.SetIpAddress("DummyIpAddress");
  276. connectionInfo.SetPort(123);
  277. connectionInfo.AddMatchedPlayerSessions(
  278. Aws::GameLift::Model::MatchedPlayerSession()
  279. .WithPlayerId("player1")
  280. .WithPlayerSessionId("playersession1"));
  281. Aws::GameLift::Model::MatchmakingTicket ticket2;
  282. ticket2.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::COMPLETED);
  283. ticket2.SetGameSessionConnectionInfo(connectionInfo);
  284. Aws::GameLift::Model::DescribeMatchmakingResult result2;
  285. result2.AddTicketList(ticket2);
  286. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome2(result2);
  287. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  288. .WillOnce(::testing::Return(outcome1))
  289. .WillOnce(::testing::Return(outcome2));
  290. SessionHandlingClientRequestsMock sessionHandlerMock;
  291. EXPECT_CALL(sessionHandlerMock, RequestPlayerJoinSession(::testing::_))
  292. .Times(1)
  293. .WillOnce(::testing::Return(true));
  294. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  295. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  296. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  297. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchComplete == 1);
  298. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  299. }
  300. TEST_F(AWSGameLiftClientLocalTicketTrackerTest, StartPolling_RequiresAcceptanceAndTicketCompleteAtLast_ProcessContinuesAndStop)
  301. {
  302. Aws::GameLift::Model::MatchmakingTicket ticket1;
  303. ticket1.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::REQUIRES_ACCEPTANCE);
  304. Aws::GameLift::Model::DescribeMatchmakingResult result1;
  305. result1.AddTicketList(ticket1);
  306. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome1(result1);
  307. Aws::GameLift::Model::GameSessionConnectionInfo connectionInfo;
  308. connectionInfo.SetIpAddress("DummyIpAddress");
  309. connectionInfo.SetPort(123);
  310. connectionInfo.AddMatchedPlayerSessions(
  311. Aws::GameLift::Model::MatchedPlayerSession()
  312. .WithPlayerId("player1")
  313. .WithPlayerSessionId("playersession1"));
  314. Aws::GameLift::Model::MatchmakingTicket ticket2;
  315. ticket2.SetStatus(Aws::GameLift::Model::MatchmakingConfigurationStatus::COMPLETED);
  316. ticket2.SetGameSessionConnectionInfo(connectionInfo);
  317. Aws::GameLift::Model::DescribeMatchmakingResult result2;
  318. result2.AddTicketList(ticket2);
  319. Aws::GameLift::Model::DescribeMatchmakingOutcome outcome2(result2);
  320. EXPECT_CALL(*m_gameliftClientMockPtr, DescribeMatchmaking(::testing::_))
  321. .WillOnce(::testing::Return(outcome1))
  322. .WillOnce(::testing::Return(outcome2));
  323. SessionHandlingClientRequestsMock sessionHandlerMock;
  324. EXPECT_CALL(sessionHandlerMock, RequestPlayerJoinSession(::testing::_))
  325. .Times(1)
  326. .WillOnce(::testing::Return(true));
  327. MatchmakingNotificationsHandlerMock matchmakingHandlerMock;
  328. m_gameliftClientTicketTracker->StartPolling("ticket1", "player1");
  329. WaitForProcessFinish([this](){ return m_gameliftClientTicketTracker->IsTrackerIdle(); });
  330. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchAcceptance == 1);
  331. ASSERT_TRUE(matchmakingHandlerMock.m_numMatchComplete == 1);
  332. ASSERT_TRUE(m_gameliftClientTicketTracker->IsTrackerIdle());
  333. }