Esempio n. 1
0
        public async Task <bool> ReservePlayerColour(Guid sessionId, String sessionEtag, PlayerColour colour)
        {
            ISessionData sessionData = await GetSession(sessionId);

            SessionTableEntry session = sessionData as SessionTableEntry;

            if (!session.IsColourUsed(colour))
            {
                if (session.ETag == sessionEtag)
                {
                    try
                    {
                        session.AddUsedColour(colour);

                        // Write entry back (fails on write conflict)
                        TableOperation insertOperation = TableOperation.Replace(session);
                        await SessionTable.ExecuteAsync(insertOperation);

                        return(true);
                    }
                    catch (StorageException exception)
                    {
                        if (exception.RequestInformation.HttpStatusCode == (int)HttpStatusCode.PreconditionFailed)
                        {
                            throw new ConcurrencyException();
                        }
                        else
                        {
                            throw exception;
                        }
                    }
                }
                else
                {
                    throw new ConcurrencyException();
                }
            }
            else
            {
                return(false);
            }
        }
        public async Task IntegrationTestReservePlayerColour()
        {
            // Arrange
            SessionRepository repository = new SessionRepository(DevelopmentStorageAccountConnectionString);
            Guid         validGuid       = new Guid("E5894BE3-6074-4516-93FB-BC851C1E4246");
            ISessionData sessionData     = await repository.SetupSession(validGuid, "CreatingUser");

            // Act
            bool isReserved = await repository.ReservePlayerColour(validGuid, sessionData.CurrentEtag, PlayerColour.Blue);

            // Assert
            Assert.AreEqual(true, isReserved);
            TableOperation operation = TableOperation.Retrieve <SessionTableEntry>(validGuid.ToString(), "CreatingUser");
            TableResult    result    = await SessionTable.ExecuteAsync(operation);

            Assert.IsNotNull(result.Result);
            Assert.IsInstanceOfType(result.Result, typeof(SessionTableEntry));
            SessionTableEntry resultStronglyTyped = result.Result as SessionTableEntry;

            Assert.IsTrue(resultStronglyTyped.IsColourUsed(PlayerColour.Blue));
            Assert.AreNotEqual(resultStronglyTyped.CurrentEtag, sessionData.CurrentEtag);
        }
        public async Task IntegrationTestCreateSession()
        {
            // Arrange
            SessionRepository repository  = new SessionRepository(DevelopmentStorageAccountConnectionString);
            String            dummyUserId = "DummyUserId";

            // Act
            Guid newSessionGuid = await repository.CreateSession(dummyUserId, PlayerColour.Black);

            // Assert
            Assert.IsNotNull(newSessionGuid);

            var            dataTable = repository.GetTableForSessionData(newSessionGuid);
            TableOperation operation = TableOperation.Retrieve <SessionTableEntry>(newSessionGuid.ToString(), dummyUserId);
            TableResult    result    = await SessionTable.ExecuteAsync(operation);

            Assert.IsNotNull(result.Result);
            Assert.IsInstanceOfType(result.Result, typeof(SessionTableEntry));
            SessionTableEntry resultStronglyTyped = result.Result as SessionTableEntry;

            Assert.AreEqual(dummyUserId, resultStronglyTyped.OwnerId);
            Assert.AreEqual(Guid.Empty, resultStronglyTyped.PhaseId);
            Assert.AreEqual(SessionPhase.NotStarted, resultStronglyTyped.PhaseType);
            Assert.IsTrue(resultStronglyTyped.IsColourUsed(PlayerColour.Black));

            operation = TableOperation.Retrieve <NationTableEntry>(newSessionGuid.ToString(), "Nation_" + dummyUserId);
            result    = await dataTable.ExecuteAsync(operation);

            Assert.IsNotNull(result.Result);
            Assert.IsInstanceOfType(result.Result, typeof(NationTableEntry));
            NationTableEntry resultPlayerStronglyTyped = result.Result as NationTableEntry;

            Assert.AreEqual(newSessionGuid, resultPlayerStronglyTyped.SessionId);
            Assert.AreEqual(dummyUserId, resultPlayerStronglyTyped.UserId);
            Assert.AreEqual(Guid.Empty, resultPlayerStronglyTyped.CompletedPhase);
        }