示例#1
0
        /// <summary>
        /// Compute async all boards, one generation at a time.
        /// </summary>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        protected override async Task RunAsync(CancellationToken cancellationToken)
        {
            var boards = await StateManager.GetOrAddAsync <IReliableDictionary <Guid, GameBoard> >(BoardDictionaryKey);

            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();

                using (var tx = StateManager.CreateTransaction())
                {
                    var sessionEnumerator = (await boards.CreateEnumerableAsync(tx)).GetAsyncEnumerator();
                    while (await sessionEnumerator.MoveNextAsync(cancellationToken))
                    {
                        var session = sessionEnumerator.Current;
                        if (await _boardComputer.IsComputationFinished(session.Value))
                        {
                            continue;
                        }

                        await _boardComputer.ComputeGeneration(session.Value);

                        await boards.SetAsync(tx, session.Key, session.Value);

                        await tx.CommitAsync();
                    }
                }

                await Task.Delay(TimeSpan.FromSeconds(1), cancellationToken);
            }
        }
        public async Task ComputeGeneration_ForCellWithFewerThanTwoLiveNeighbours_CellDies()
        {
            var someBoard = new GameBoard
            {
                Board = new List <BoardCell>
                {
                    new BoardCell(0, 0, false),
                    new BoardCell(1, 0, true),
                    new BoardCell(2, 0, false),

                    new BoardCell(0, 1, false),
                    new BoardCell(1, 1, true),
                    new BoardCell(2, 1, false),

                    new BoardCell(0, 2, false),
                    new BoardCell(1, 2, false),
                    new BoardCell(2, 2, false),
                }
            };

            await _sut.ComputeGeneration(someBoard);

            Assert.IsFalse(someBoard.Board.Single(c => c.XPosition == 1 && c.YPosition == 1).IsAlive);
        }