3
3
using System ;
4
4
using System . Collections . Generic ;
5
5
using System . Linq ;
6
+ using System . Threading ;
6
7
using System . Threading . Tasks ;
7
8
using Xunit ;
8
9
@@ -15,6 +16,7 @@ public BulkOperationsTests(RedisSetup setup)
15
16
{
16
17
_connection = setup . Connection ;
17
18
}
19
+
18
20
private IRedisConnection _connection = null ;
19
21
20
22
[ Fact ]
@@ -27,7 +29,7 @@ public async Task Test_Bulk_InsertAsync()
27
29
new Person ( ) { Name = "Jeeva" , Age = 22 , NickNames = new [ ] { "Jee" , "Jeev" , "J" } } ,
28
30
new Person ( ) { Name = "Martin" , Age = 60 , NickNames = new [ ] { "Mart" , "Mat" , "tin" } }
29
31
} ;
30
- var keys = await collection . Insert ( persons ) ;
32
+ var keys = await collection . InsertAsync ( persons ) ;
31
33
32
34
var people = collection . Where ( x => x . NickNames . Contains ( "Bob" ) || x . NickNames . Contains ( "Alie" ) ) . ToList ( ) ;
33
35
Assert . Contains ( people , x => x . Name == persons . First ( ) . Name ) ;
@@ -43,7 +45,7 @@ public async Task Test_Inserts_TwiceWith_SaveDataWith_ExactFields()
43
45
new Person ( ) { Name = "Jeeva" , Age = 22 , NickNames = new [ ] { "Jee" , "Jeev" , "J" } } ,
44
46
new Person ( ) { Name = "Martin" , Age = 61 , NickNames = new [ ] { "Mart" , "Mat" , "tin" } }
45
47
} ;
46
- var keys = await collection . Insert ( persons ) ; //performs JSON.SET create keys and emit the list of keys.
48
+ var keys = await collection . InsertAsync ( persons ) ; //performs JSON.SET create keys and emit the list of keys.
47
49
48
50
var persons2 = new List < Person > ( ) {
49
51
new Person ( ) { Name = "Alice" , Age = 14 , NickNames = new [ ] { "Ally" , "Alie" , "Al" } , IsEngineer = true } ,
@@ -52,9 +54,9 @@ public async Task Test_Inserts_TwiceWith_SaveDataWith_ExactFields()
52
54
new Person ( ) { Name = "Martin" , Age = 61 , NickNames = new [ ] { "Mart" , "Mat" , "tin" } , TagField = "Martin" }
53
55
} ;
54
56
55
- var keys2 = await collection . Insert ( persons2 ) ; //create keys and emit the list of keys.
57
+ var keys2 = await collection . InsertAsync ( persons2 ) ; //create keys and emit the list of keys.
56
58
57
- var people = collection . Where ( x => x . Age >= 20 && x . Age <= 30 ) . ToList ( ) ;
59
+ var people = collection . Where ( x => x . Age >= 20 && x . Age <= 30 ) . ToList ( ) ;
58
60
Assert . NotEqual ( keys , keys2 ) ; //not performs any re-indexing because keys are not same.
59
61
}
60
62
@@ -68,7 +70,7 @@ public async Task Test_BulkInsert_WithSameIds()
68
70
new Person ( ) { Name = "Jeeva" , Age = 22 , NickNames = new [ ] { "Jee" , "Jeev" , "J" } , } ,
69
71
new Person ( ) { Name = "Martin" , Age = 60 , NickNames = new [ ] { "Mart" , "Mat" , "tin" } , }
70
72
} ;
71
- await collection . Insert ( persons ) ;
73
+ await collection . InsertAsync ( persons ) ;
72
74
var people = collection . Where ( x => x . NickNames . Contains ( "Jeeva" ) || x . NickNames . Contains ( "Alie" ) ) . ToList ( ) ;
73
75
Assert . False ( people . First ( ) . Name == persons . First ( ) . Name ) ; // this fails because the Name field of people doesn't contains the Name value Alice
74
76
}
@@ -82,7 +84,7 @@ public async Task Test_BulkInsert_HashesWith_Expiration()
82
84
new HashPerson ( ) { Name = "Phineas" , Age = 14 , IsEngineer = true , TagField = "SummerVacation" }
83
85
} ;
84
86
85
- await collection . Insert ( PhineasFerb , TimeSpan . FromMilliseconds ( 8000 ) ) ;
87
+ await collection . InsertAsync ( PhineasFerb , TimeSpan . FromMilliseconds ( 8000 ) ) ;
86
88
var ttl = ( long ) _connection . Execute ( "PTTL" , PhineasFerb [ 0 ] . GetKey ( ) ) ;
87
89
Assert . True ( ttl <= 8000 ) ;
88
90
Assert . True ( ttl >= 1000 ) ;
@@ -97,7 +99,7 @@ public async Task Test_BulkInsert_WithExpiration()
97
99
new Person ( ) { Name = "Phineas" , Age = 14 , IsEngineer = true , TagField = "SummerVacation" , NickNames = new [ ] { "Phineas" , "Triangle Head" , "Phine" } }
98
100
} ;
99
101
100
- await collection . Insert ( PhineasFerb , TimeSpan . FromSeconds ( 8 ) ) ;
102
+ await collection . InsertAsync ( PhineasFerb , TimeSpan . FromSeconds ( 8 ) ) ;
101
103
var ttl = ( long ) _connection . Execute ( "PTTL" , PhineasFerb [ 0 ] . GetKey ( ) ) ;
102
104
Assert . True ( ttl <= 8000 ) ;
103
105
Assert . True ( ttl >= 1000 ) ;
@@ -114,13 +116,33 @@ public async Task Test_Bulk_Insert_Del()
114
116
new Person ( ) { Name = "Perry" , Age = 5 , IsEngineer = false , TagField = "Agent" , Address = new Address { State = "Tri-State Area " } }
115
117
} ;
116
118
117
- await collection . Insert ( PhineasFerbShow ) ;
119
+ await collection . InsertAsync ( PhineasFerbShow ) ;
118
120
var searchByState = collection . Where ( x => x . Address . State == "Tri-State Area" ) . ToList ( ) ;
119
121
await collection . DeleteAsync ( searchByState ) ;
120
122
var searchByTag = collection . FindById ( searchByState [ 0 ] . GetKey ( ) ) ;
121
123
Assert . Null ( searchByTag ) ;
122
124
}
123
125
126
+ [ Fact ]
127
+ public async Task Test_Bulk_Insert_WithWhen ( )
128
+ {
129
+ var collection = new RedisCollection < Person > ( _connection ) ;
130
+ var PhineasFerbShow = new List < Person > ( ) {
131
+ new Person ( ) { Id = "1" , Name = "Ferb" , Age = 14 , IsEngineer = true , TagField = "SummerVacation" , Address = new Address { State = "Tri-State Area" } } ,
132
+ new Person ( ) { Id = "2" , Name = "Phineas" , Age = 14 , IsEngineer = true , TagField = "SummerVacation" , Address = new Address { State = "Tri-State Area" } } ,
133
+ new Person ( ) { Id = "3" , Name = "Dr.Doofenshmirtz" , Age = 38 , IsEngineer = true , TagField = "Villain" , Address = new Address { State = "Tri-State Area" } } ,
134
+ new Person ( ) { Id = "4" , Name = "Perry" , Age = 5 , IsEngineer = false , TagField = "Agent" , Address = new Address { State = "Tri-State Area " } }
135
+ } ;
136
+
137
+ var res = await collection . InsertAsync ( PhineasFerbShow , WhenKey . NotExists , TimeSpan . FromMilliseconds ( 10000 ) ) ;
138
+ Assert . True ( res . All ( x => x != null ) ) ;
139
+ Thread . Sleep ( 1100 ) ;
140
+ res = await collection . InsertAsync ( PhineasFerbShow , WhenKey . NotExists , TimeSpan . FromMilliseconds ( 5000 ) ) ;
141
+ Assert . True ( res . All ( x => x == null ) ) ;
142
+ res = await collection . InsertAsync ( PhineasFerbShow , WhenKey . Always , TimeSpan . FromMilliseconds ( 6000 ) ) ;
143
+ Assert . True ( res . All ( x => x != null ) ) ;
144
+ }
145
+
124
146
[ Fact ]
125
147
public async Task Test_Bulk_InsertAsync_DelAsync_ForHashes ( )
126
148
{
@@ -132,7 +154,7 @@ public async Task Test_Bulk_InsertAsync_DelAsync_ForHashes()
132
154
new HashPerson ( ) { Name = "Perry" , Age = 5 , IsEngineer = false , TagField = "Agent" , Address = new Address { State = "Tri-State Area " } }
133
155
} ;
134
156
135
- await collection . Insert ( PhineasFerbShow ) ;
157
+ await collection . InsertAsync ( PhineasFerbShow ) ;
136
158
var searchByName = await collection . Where ( x => x . Name == "Dr.Doofenshmirtz" || x . Name == "Perry" ) . ToListAsync ( ) ;
137
159
await collection . DeleteAsync ( searchByName ) ;
138
160
var searchByTag = await collection . FindByIdAsync ( searchByName [ 0 ] . GetKey ( ) ) ;
@@ -149,7 +171,7 @@ public async Task Test_Bulk_UpdateAsync()
149
171
new Person ( ) { Name = "Monkey D. Garp" , Age = 70 , NickNames = new [ ] { "Garp" , "Garps" , "Hero of the Navy" } , TagField = "Navy" } ,
150
172
new Person ( ) { Name = "Shanks" , Age = 50 , NickNames = new [ ] { "Shanks" , "Red-Hair" } , TagField = "Red-Haired Pirates" }
151
173
} ;
152
- var keys = await collection . Insert ( onepiece ) ;
174
+ var keys = await collection . InsertAsync ( onepiece ) ;
153
175
var people = collection . Where ( x => x . NickNames . Contains ( "Luffy" ) || x . NickNames . Contains ( "Shanks" ) ) . ToList ( ) ;
154
176
Assert . Equal ( onepiece [ 0 ] . Age , people [ 0 ] . Age ) ;
155
177
people [ 0 ] . Age = 25 ;
@@ -168,7 +190,7 @@ public async Task Test_Bulk_UpdateSync_WithHashesNumeric()
168
190
new HashPerson ( ) { Name = "Monkey D. Garp" , Age = 70 , NickNames = new List < string > { "Garp" , "Garps" , "Hero of the Navy" } , TagField = "Navy" } ,
169
191
new HashPerson ( ) { Name = "Shanks" , Age = 50 , NickNames = new List < string > { "Shanks" , "Red-Hair" } , TagField = "Red-Haired Pirates" }
170
192
} ;
171
- var keys = collection . Insert ( onepiece ) ;
193
+ var keys = collection . InsertAsync ( onepiece ) ;
172
194
var people = collection . Where ( x => x . Name . Contains ( "Luffy" ) || x . Name . Contains ( "Shanks" ) ) . ToList ( ) ;
173
195
Assert . Equal ( onepiece [ 0 ] . Age , people [ 0 ] . Age ) ;
174
196
people [ 0 ] . Height = 20.2 ;
@@ -178,7 +200,6 @@ public async Task Test_Bulk_UpdateSync_WithHashesNumeric()
178
200
Assert . NotEqual ( onepiece [ 0 ] . Age , people [ 0 ] . Age ) ;
179
201
}
180
202
181
-
182
203
[ Fact ]
183
204
public async Task Test_BulkUpdate_WithEmbbedObject ( )
184
205
{
@@ -189,11 +210,11 @@ public async Task Test_BulkUpdate_WithEmbbedObject()
189
210
new Person ( ) { Name = "Monkey D. Garp" , Age = 70 , NickNames = new [ ] { "Garp" , "Garps" , "Hero of the Navy" } , TagField = "Navy" } ,
190
211
new Person ( ) { Name = "Shanks" , Age = 50 , NickNames = new [ ] { "Shanks" , "Red-Hair" } , TagField = "Red-Haired Pirates" }
191
212
} ;
192
- var keys = collection . Insert ( onepiece ) ;
213
+ var keys = collection . InsertAsync ( onepiece ) ;
193
214
var people = collection . Where ( x => x . NickNames . Contains ( "Luffy" ) || x . NickNames . Contains ( "Shanks" ) ) . ToList ( ) ;
194
215
people [ 0 ] . Address = new Address { City = "Goa Kingdom" } ;
195
216
people [ 1 ] . Address = new Address { City = "Goa Kingdom" } ;
196
- await collection . UpdateAsync ( people ) ;
217
+ await collection . UpdateAsync ( people ) ;
197
218
Assert . Contains ( people , x => x . Name == onepiece . First ( ) . Name ) ;
198
219
}
199
220
@@ -218,7 +239,7 @@ public async Task Test_Bulk50_Records_Insert_Update_Del_Async()
218
239
}
219
240
) ;
220
241
}
221
- var keys = await collection . Insert ( people ) ; // 1000 records in an avg of 200ms.
242
+ var keys = await collection . InsertAsync ( people ) ; // 1000 records in an avg of 200ms.
222
243
var listofPeople = ( await collection . FindByIdsAsync ( keys ) ) . Values . ToList ( ) ;
223
244
for ( int i = 0 ; i < keys . Count ; i ++ )
224
245
{
@@ -248,14 +269,14 @@ public async Task TestBulk_Insert_Update_Del_Async_WithHashes()
248
269
new HashPerson ( ) { Name = "Perry" , Age = 5 , IsEngineer = false , TagField = "Agent" , Address = new Address { State = "Tri-State Area " } }
249
270
} ;
250
271
251
- await collection . Insert ( PhineasFerbShow ) ;
252
- var searchByName = await collection . Where ( x => x . Name == "Dr.Doofenshmirtz" || x . Name == "Perry" ) . ToListAsync ( ) ;
272
+ await collection . InsertAsync ( PhineasFerbShow ) ;
273
+ var searchByName = await collection . Where ( x => x . Name == "Dr.Doofenshmirtz" || x . Name == "Perry" ) . ToListAsync ( ) ;
253
274
searchByName [ 0 ] . TagField = "Vacation" ;
254
275
searchByName [ 1 ] . DepartmentNumber = 2 ;
255
276
await collection . UpdateAsync ( searchByName ) ;
256
277
await collection . DeleteAsync ( searchByName ) ;
257
278
var searchByTag = await collection . FindByIdAsync ( searchByName [ 0 ] . GetKey ( ) ) ;
258
279
Assert . Null ( searchByTag ) ;
259
280
}
260
- }
261
- }
281
+ }
282
+ }
0 commit comments