コード例 #1
0
        public async Task Test(ChunkTestCase testCase)
        {
            var stream = new MockReadRowsStream(
                new ReadRowsResponse
            {
                Chunks = { testCase.Chunks.Select(json => CellChunk.Parser.ParseJson(json)) }
            });

            List <Row> responses;

            if (testCase.ExpectsError)
            {
                // Do not use ToList() here. We want to get all results before the first failure.
                responses = new List <Row>();
                await Assert.ThrowsAsync <InvalidOperationException>(
                    () => stream.AsAsyncEnumerable().ForEachAsync(row => responses.Add(row)));
            }
            else
            {
                responses = await stream.AsAsyncEnumerable().ToList();
            }

            var results = from row in responses
                          from family in row.Families
                          from column in family.Columns
                          from cell in column.Cells
                          select new TestResult
            {
                RowKey     = row.Key.ToStringUtf8(),
                FamilyName = family.Name,
                Qualifier  = column.Qualifier.ToStringUtf8(),
                Timestamp  = cell.TimestampMicros,
                Value      = cell.Value.ToStringUtf8(),
                Label      = cell.Labels.FirstOrDefault() ?? "",
                Error      = false
            };
            var expectedResults = testCase.GetNonExceptionResults();

            Assert.True(expectedResults.SequenceEqual(results), testCase.Name);
        }
コード例 #2
0
 public Enumerator(MockReadRowsStream owner, IAsyncEnumerator <ReadRowsResponse> underlyingStream)
 {
     _owner            = owner;
     _underlyingStream = underlyingStream;
 }
コード例 #3
0
        public static BigtableClient CreateReadRowsMockClient(
            ReadRowsRequest initialRequest,
            ReadRowsResponse[] initialStreamResponse,
            ReadRowsResponse[][] responsesForRetryStreams = null,
            bool errorAtEndOfLastStream = false)
        {
            MockReadRowsStream lastStream = null;
            var result = CreateMockClientForStreamingRpc(
                initialRequest,
                c => c.ReadRows(
                    It.Is <ReadRowsRequest>(r => ReferenceEquals(r, initialRequest)),
                    It.IsAny <CallSettings>()),
                c => c.ReadRows(
                    It.IsAny <ReadRowsRequest>(),
                    It.IsAny <CallSettings>()),
                initialStreamResponse,
                responsesForRetryStreams,
                itemsToStream: entries =>
                lastStream = new MockReadRowsStream(entries)
            {
                ShouldErrorAtEnd = true
            },
                validator: (request, response) =>
            {
                // Make sure the request is properly formulated for the mock stream being returned.
                // Each response chunk should be a continuation of a previous chunk or from a row
                // that was requested.
                if (!response.Responses.SelectMany(r => r.Chunks).All(
                        c => c.RowKey.IsEmpty || IsRequested(request, c.RowKey)))
                {
                    throw new InvalidOperationException("The specified request is invalid for the mock stream about to be returned.");
                }
            });

            // All but the last stream should end with an RpcException which permits retrying with the
            // default RetrySettings so the higher level ReadRowsStream keeps retrying
            // (see `ShouldErrorAtEnd = true` above). The last stream should end normally, unless
            // errorAtEndOfLastStream is true, in which case it should end with an error as well.
            lastStream.ShouldErrorAtEnd = errorAtEndOfLastStream;

            return(result);

            bool IsRequested(ReadRowsRequest request, BigtableByteString rowKey)
            {
                return
                    (request.Rows.RowRanges.Any(IsInRange) ||
                     request.Rows.RowKeys.Contains((ByteString)rowKey));

                bool IsInRange(RowRange range)
                {
                    switch (range.StartKeyCase)
                    {
                    case RowRange.StartKeyOneofCase.StartKeyClosed:
                        if (rowKey < range.StartKeyClosed)
                        {
                            return(false);
                        }
                        break;

                    case RowRange.StartKeyOneofCase.StartKeyOpen:
                        if (rowKey <= range.StartKeyOpen)
                        {
                            return(false);
                        }
                        break;
                    }
                    if (!range.EndKeyClosed.IsEmpty)
                    {
                        switch (range.EndKeyCase)
                        {
                        case RowRange.EndKeyOneofCase.EndKeyClosed:
                            if (range.EndKeyClosed < rowKey)
                            {
                                return(false);
                            }
                            break;

                        case RowRange.EndKeyOneofCase.EndKeyOpen:
                            if (range.EndKeyOpen <= rowKey)
                            {
                                return(false);
                            }
                            break;
                        }
                    }
                    return(true);
                }
            }
        }