1+ // -----------------------------------------------------------------------
2+ // <copyright file="LimitedBatchSpec.cs" company="Akka.NET Project">
3+ // Copyright (C) 2009-2022 Lightbend Inc. <http://www.lightbend.com>
4+ // Copyright (C) 2013-2022 .NET Foundation <https://github.com/akkadotnet/akka.net>
5+ // </copyright>
6+ // -----------------------------------------------------------------------
7+
8+ using System . Collections . Generic ;
9+ using System . Linq ;
10+ using System . Threading ;
11+ using System . Threading . Tasks ;
12+ using Akka . Persistence . Azure . Tests . Helper ;
13+ using Azure . Data . Tables ;
14+ using FluentAssertions ;
15+ using FluentAssertions . Extensions ;
16+ using Xunit ;
17+ using Xunit . Abstractions ;
18+
19+ namespace Akka . Persistence . Azure . Tests
20+ {
21+ public class LimitedBatchSpec : IAsyncLifetime
22+ {
23+ private readonly TableClient _tableClient ;
24+
25+ public LimitedBatchSpec ( ITestOutputHelper output )
26+ {
27+ _tableClient = new TableClient ( "UseDevelopmentStorage=true" , "testtable" ) ;
28+ }
29+
30+ public async Task InitializeAsync ( )
31+ {
32+ await DbUtils . CleanupCloudTable ( "UseDevelopmentStorage=true" ) ;
33+ await _tableClient . CreateAsync ( ) ;
34+ }
35+
36+ public Task DisposeAsync ( )
37+ {
38+ return Task . CompletedTask ;
39+ }
40+
41+ [ Fact ( DisplayName = "Limited batch with 0 entries should return empty list" ) ]
42+ public async Task ZeroEntriesTest ( )
43+ {
44+ using var cts = new CancellationTokenSource ( 3 . Seconds ( ) ) ;
45+ var result = await _tableClient . ExecuteBatchAsLimitedBatches ( new List < TableTransactionAction > ( ) , cts . Token ) ;
46+ result . Count . Should ( ) . Be ( 0 ) ;
47+
48+ var entities = await _tableClient . QueryAsync < TableEntity > ( "PartitionKey eq 'test'" , null , null , cts . Token )
49+ . ToListAsync ( cts . Token ) ;
50+ entities . Count . Should ( ) . Be ( 0 ) ;
51+ }
52+
53+ [ Fact ( DisplayName = "Limited batch with less than 100 entries should work" ) ]
54+ public async Task FewEntriesTest ( )
55+ {
56+ var entries = Enumerable . Range ( 1 , 50 )
57+ . Select ( i => new TableTransactionAction ( TableTransactionActionType . Add , new TableEntity
58+ {
59+ PartitionKey = "test" ,
60+ RowKey = i . ToString ( "D8" )
61+ } ) ) . ToList ( ) ;
62+
63+ using var cts = new CancellationTokenSource ( 3 . Seconds ( ) ) ;
64+ var result = await _tableClient . ExecuteBatchAsLimitedBatches ( entries , cts . Token ) ;
65+ result . Count . Should ( ) . Be ( 50 ) ;
66+
67+ var entities = await _tableClient . QueryAsync < TableEntity > ( "PartitionKey eq 'test'" , null , null , cts . Token )
68+ . ToListAsync ( cts . Token ) ;
69+ entities . Count . Should ( ) . Be ( 50 ) ;
70+ entities . Select ( e => int . Parse ( e . RowKey . TrimStart ( '0' ) ) ) . Should ( ) . BeEquivalentTo ( Enumerable . Range ( 1 , 50 ) ) ;
71+ }
72+
73+ [ Fact ( DisplayName = "Limited batch with more than 100 entries should work" ) ]
74+ public async Task LotsEntriesTest ( )
75+ {
76+ var entries = Enumerable . Range ( 1 , 505 )
77+ . Select ( i => new TableTransactionAction ( TableTransactionActionType . Add , new TableEntity
78+ {
79+ PartitionKey = "test" ,
80+ RowKey = i . ToString ( "D8" )
81+ } ) ) . ToList ( ) ;
82+
83+ using var cts = new CancellationTokenSource ( 3 . Seconds ( ) ) ;
84+ var result = await _tableClient . ExecuteBatchAsLimitedBatches ( entries , cts . Token ) ;
85+ result . Count . Should ( ) . Be ( 505 ) ;
86+
87+ var entities = await _tableClient . QueryAsync < TableEntity > ( "PartitionKey eq 'test'" , null , null , cts . Token )
88+ . ToListAsync ( cts . Token ) ;
89+ entities . Count . Should ( ) . Be ( 505 ) ;
90+ entities . Select ( e => int . Parse ( e . RowKey . TrimStart ( '0' ) ) ) . Should ( ) . BeEquivalentTo ( Enumerable . Range ( 1 , 505 ) ) ;
91+ }
92+ }
93+ }
0 commit comments