55using System ;
66using System . Collections . Generic ;
77using System . Linq ;
8+ using System . Threading ;
89using System . Threading . Tasks ;
910using Microsoft . Toolkit . Uwp ;
1011using Microsoft . VisualStudio . TestTools . UnitTesting ;
@@ -14,27 +15,45 @@ namespace UnitTests.UI
1415 [ TestClass ]
1516 public class Test_IncrementalLoadingCollection
1617 {
18+ private static readonly DataSource < int > . PageOperation [ ] FailPassSequence
19+ = new DataSource < int > . PageOperation [ ]
20+ {
21+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
22+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
23+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
24+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
25+ DataSource < int > . ThrowException , DataSource < int > . PassThrough ,
26+ } ;
27+
28+ [ DataRow ( 2500 , 1000 , 1000 , 1000 , 1000 ) ]
29+ [ DataRow ]
1730 [ TestMethod ]
18- public async Task SequentialRequests ( )
31+ public async Task Requests ( params int [ ] pageDelays )
1932 {
2033 const int pageSize = 20 ;
2134 const int pages = 5 ;
22- var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) ) , pageSize ) ;
35+
36+ var source = new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) , pageDelays . Select ( DataSource < int > . MakeDelayOp ) ) ;
37+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , pageSize ) ;
2338
2439 for ( int pageNum = 1 ; pageNum <= pages ; pageNum ++ )
2540 {
2641 var rez1 = await collection . LoadMoreItemsAsync ( 0 ) ;
2742 Assert . AreEqual ( ( uint ) pageSize , rez1 . Count ) ;
28- CollectionAssert . AreEquivalent ( Enumerable . Range ( 0 , pageSize * pageNum ) . ToArray ( ) , collection ) ;
43+ CollectionAssert . AreEqual ( Enumerable . Range ( 0 , pageSize * pageNum ) . ToArray ( ) , collection ) ;
2944 }
3045 }
3146
47+ [ DataRow ( 2500 , 1000 , 1000 , 1000 , 1000 ) ]
48+ [ DataRow ]
3249 [ TestMethod ]
33- public void ConcurentRequests ( )
50+ public async Task RequestsAsync ( params int [ ] pageDelays )
3451 {
35- const int pageSize = 20 ;
52+ const int pageSize = 20 ;
3653 const int pages = 5 ;
37- var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) ) , pageSize ) ;
54+
55+ var source = new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) , pageDelays . Select ( DataSource < int > . MakeDelayOp ) ) ;
56+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , pageSize ) ;
3857
3958 var requests = new List < Task > ( ) ;
4059
@@ -47,8 +66,92 @@ public void ConcurentRequests()
4766 } ) ) ;
4867 }
4968
50- Task . WaitAll ( requests . ToArray ( ) ) ;
69+ await Task . WhenAll ( requests ) ;
70+
71+ CollectionAssert . AreEqual ( Enumerable . Range ( 0 , pageSize * pages ) . ToArray ( ) , collection ) ;
72+ }
73+
74+ [ TestMethod ]
75+ public async Task FirstRequestFails ( )
76+ {
77+ const int pageSize = 20 ;
78+ const int pages = 5 ;
79+
80+ var source = new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) , DataSource < int > . ThrowException ) ;
81+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , pageSize ) ;
82+
83+ await Assert . ThrowsExceptionAsync < AggregateException > ( async ( ) => await collection . LoadMoreItemsAsync ( 0 ) ) ;
84+
85+ Assert . IsTrue ( ! collection . Any ( ) ) ;
86+
87+ var requests = new List < Task > ( ) ;
88+
89+ for ( int pageNum = 1 ; pageNum <= pages ; pageNum ++ )
90+ {
91+ requests . Add ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ( ) ) ;
92+ }
93+
94+ await Task . WhenAll ( requests ) ;
95+
96+ CollectionAssert . AreEqual ( Enumerable . Range ( 0 , pageSize * pages ) . ToArray ( ) , collection ) ;
97+ }
98+
99+ [ TestMethod ]
100+ public async Task EveryOtherRequestFails ( )
101+ {
102+ const int pageSize = 20 ;
103+ const int pages = 5 ;
104+
105+ var source = new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) , FailPassSequence ) ;
106+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , pageSize ) ;
107+
108+ var willFail = true ;
109+ for ( int submitedRequests = 0 ; submitedRequests < 10 ; submitedRequests ++ )
110+ {
111+ if ( willFail )
112+ {
113+ await collection . LoadMoreItemsAsync ( 0 ) . AsTask ( ) . ContinueWith ( t => Assert . AreEqual ( TaskStatus . Faulted , t . Status ) ) ;
114+ }
115+ else
116+ {
117+ await collection . LoadMoreItemsAsync ( 0 ) . AsTask ( ) . ContinueWith ( t => Assert . AreEqual ( TaskStatus . RanToCompletion , t . Status ) ) ;
118+ }
119+
120+ willFail = ! willFail ;
121+ }
122+
51123 CollectionAssert . AreEquivalent ( Enumerable . Range ( 0 , pageSize * pages ) . ToArray ( ) , collection ) ;
52124 }
125+
126+ [ TestMethod ]
127+ public async Task EveryOtherRequestFailsAsync ( )
128+ {
129+ const int pageSize = 20 ;
130+ const int pages = 5 ;
131+
132+ var source = new DataSource < int > ( Enumerable . Range ( 0 , pageSize * pages ) , FailPassSequence ) ;
133+ var collection = new IncrementalLoadingCollection < DataSource < int > , int > ( source , pageSize ) ;
134+
135+ var requests = new List < Task > ( ) ;
136+
137+ var willFail = true ;
138+ for ( int submitedRequests = 0 ; submitedRequests < 10 ; submitedRequests ++ )
139+ {
140+ if ( willFail )
141+ {
142+ requests . Add ( Assert . ThrowsExceptionAsync < AggregateException > ( ( ) => collection . LoadMoreItemsAsync ( 0 ) . AsTask ( ) ) ) ;
143+ }
144+ else
145+ {
146+ requests . Add ( collection . LoadMoreItemsAsync ( 0 ) . AsTask ( ) ) ;
147+ }
148+
149+ willFail = ! willFail ;
150+ }
151+
152+ await Task . WhenAll ( requests ) ;
153+
154+ CollectionAssert . AreEqual ( Enumerable . Range ( 0 , pageSize * pages ) . ToArray ( ) , collection ) ;
155+ }
53156 }
54157}
0 commit comments