5
5
using System ;
6
6
using System . Collections . Generic ;
7
7
using System . Linq ;
8
+ using System . Threading ;
8
9
using System . Threading . Tasks ;
9
10
using Microsoft . Toolkit . Uwp ;
10
11
using Microsoft . VisualStudio . TestTools . UnitTesting ;
@@ -14,27 +15,45 @@ namespace UnitTests.UI
14
15
[ TestClass ]
15
16
public class Test_IncrementalLoadingCollection
16
17
{
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 ]
17
30
[ TestMethod ]
18
- public async Task SequentialRequests ( )
31
+ public async Task Requests ( params int [ ] pageDelays )
19
32
{
20
33
const int pageSize = 20 ;
21
34
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 ) ;
23
38
24
39
for ( int pageNum = 1 ; pageNum <= pages ; pageNum ++ )
25
40
{
26
41
var rez1 = await collection . LoadMoreItemsAsync ( 0 ) ;
27
42
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 ) ;
29
44
}
30
45
}
31
46
47
+ [ DataRow ( 2500 , 1000 , 1000 , 1000 , 1000 ) ]
48
+ [ DataRow ]
32
49
[ TestMethod ]
33
- public void ConcurentRequests ( )
50
+ public async Task RequestsAsync ( params int [ ] pageDelays )
34
51
{
35
- const int pageSize = 20 ;
52
+ const int pageSize = 20 ;
36
53
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 ) ;
38
57
39
58
var requests = new List < Task > ( ) ;
40
59
@@ -47,8 +66,92 @@ public void ConcurentRequests()
47
66
} ) ) ;
48
67
}
49
68
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
+
51
123
CollectionAssert . AreEquivalent ( Enumerable . Range ( 0 , pageSize * pages ) . ToArray ( ) , collection ) ;
52
124
}
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
+ }
53
156
}
54
157
}
0 commit comments