@@ -110,6 +110,86 @@ public void GCM_Batched_Single_ShouldSucceed()
110
110
TestNotifications ( false , 1 , 1 , 0 ) ;
111
111
}
112
112
113
+ [ Test ]
114
+ public void GCM_Subscription_ShouldBeExpired ( )
115
+ {
116
+ int msgIdOn = 1 ;
117
+
118
+ int pushFailCount = 0 ;
119
+ int pushSuccessCount = 0 ;
120
+ int subChangedCount = 0 ;
121
+ int subExpiredCount = 0 ;
122
+
123
+ var notifications = new List < GcmNotification > ( ) {
124
+ new GcmNotification ( ) . ForDeviceRegistrationId ( "NOTREGISTERED" ) . WithJson ( @"{""key"":""value""}" )
125
+ } ;
126
+
127
+ TestNotifications ( notifications ,
128
+ new List < GcmMessageResponseFilter > ( ) {
129
+ new GcmMessageResponseFilter ( )
130
+ {
131
+ IsMatch = ( request , s ) => {
132
+ return s . Equals ( "NOTREGISTERED" , StringComparison . InvariantCultureIgnoreCase ) ;
133
+ } ,
134
+ Status = new GcmMessageResult ( ) {
135
+ ResponseStatus = GcmMessageTransportResponseStatus . NotRegistered ,
136
+ MessageId = "1:" + msgIdOn ++
137
+ }
138
+ }
139
+ } ,
140
+ ( sender , notification ) => pushSuccessCount ++ , //Success
141
+ ( sender , notification , error ) => pushFailCount ++ , //Failed
142
+ ( sender , oldId , newId , notification ) => subChangedCount ++ ,
143
+ ( sender , id , expiryDate , notification ) => subExpiredCount ++
144
+ ) ;
145
+
146
+ Assert . AreEqual ( 0 , pushFailCount , "Client - Failed Count" ) ;
147
+ Assert . AreEqual ( 0 , pushSuccessCount , "Client - Success Count" ) ;
148
+ Assert . AreEqual ( 0 , subChangedCount , "Client - SubscriptionId Changed Count" ) ;
149
+ Assert . AreEqual ( notifications . Count , subExpiredCount , "Client - SubscriptionId Expired Count" ) ;
150
+ }
151
+
152
+
153
+ [ Test ]
154
+ public void GCM_Subscription_ShouldBeChanged ( )
155
+ {
156
+ int msgIdOn = 1 ;
157
+
158
+ int pushFailCount = 0 ;
159
+ int pushSuccessCount = 0 ;
160
+ int subChangedCount = 0 ;
161
+ int subExpiredCount = 0 ;
162
+
163
+ var notifications = new List < GcmNotification > ( ) {
164
+ new GcmNotification ( ) . ForDeviceRegistrationId ( "NOTREGISTERED" ) . WithJson ( @"{""key"":""value""}" )
165
+ } ;
166
+
167
+ TestNotifications ( notifications ,
168
+ new List < GcmMessageResponseFilter > ( ) {
169
+ new GcmMessageResponseFilter ( )
170
+ {
171
+ IsMatch = ( request , s ) => {
172
+ return s . Equals ( "NOTREGISTERED" , StringComparison . InvariantCultureIgnoreCase ) ;
173
+ } ,
174
+ Status = new GcmMessageResult ( ) {
175
+ ResponseStatus = GcmMessageTransportResponseStatus . NotRegistered ,
176
+ CanonicalRegistrationId = "NEWID" ,
177
+ MessageId = "1:" + msgIdOn ++
178
+ }
179
+ }
180
+ } ,
181
+ ( sender , notification ) => pushSuccessCount ++ , //Success
182
+ ( sender , notification , error ) => pushFailCount ++ , //Failed
183
+ ( sender , oldId , newId , notification ) => subChangedCount ++ ,
184
+ ( sender , id , expiryDate , notification ) => subExpiredCount ++
185
+ ) ;
186
+
187
+ Assert . AreEqual ( 0 , pushFailCount , "Client - Failed Count" ) ;
188
+ Assert . AreEqual ( 0 , pushSuccessCount , "Client - Success Count" ) ;
189
+ Assert . AreEqual ( notifications . Count , subChangedCount , "Client - SubscriptionId Changed Count" ) ;
190
+ Assert . AreEqual ( 0 , subExpiredCount , "Client - SubscriptionId Expired Count" ) ;
191
+ }
192
+
113
193
114
194
public void TestNotifications ( bool shouldBatch , int toQueue , int expectSuccessful , int expectFailed , int [ ] indexesToFail = null )
115
195
{
@@ -139,6 +219,16 @@ public void TestNotifications(bool shouldBatch, int toQueue, int expectSuccessfu
139
219
}
140
220
} ) ;
141
221
222
+ server . MessageResponseFilters . Add ( new GcmMessageResponseFilter ( )
223
+ {
224
+ IsMatch = ( request , s ) => {
225
+ return s . Equals ( "NOTREGISTERED" , StringComparison . InvariantCultureIgnoreCase ) ;
226
+ } ,
227
+ Status = new GcmMessageResult ( ) {
228
+ ResponseStatus = GcmMessageTransportResponseStatus . NotRegistered ,
229
+ MessageId = "1:" + msgIdOn ++
230
+ }
231
+ } ) ;
142
232
//var waitServerFinished = new ManualResetEvent(false);
143
233
144
234
server . Start ( testPort , response =>
@@ -197,5 +287,65 @@ public void TestNotifications(bool shouldBatch, int toQueue, int expectSuccessfu
197
287
Assert . AreEqual ( expectFailed , pushFailCount , "Client - Failed Count" ) ;
198
288
Assert . AreEqual ( expectSuccessful , pushSuccessCount , "Client - Success Count" ) ;
199
289
}
290
+
291
+
292
+
293
+
294
+ public void TestNotifications ( List < GcmNotification > notifications ,
295
+ List < GcmMessageResponseFilter > responseFilters ,
296
+ Action < object , INotification > sentCallback ,
297
+ Action < object , INotification , Exception > failedCallback ,
298
+ Action < object , string , string , INotification > subscriptionChangedCallback ,
299
+ Action < object , string , DateTime , INotification > subscriptionExpiredCallback )
300
+ {
301
+ testPort ++ ;
302
+
303
+ int pushFailCount = 0 ;
304
+ int pushSuccessCount = 0 ;
305
+
306
+ int serverReceivedCount = 0 ;
307
+ int serverReceivedFailCount = 0 ;
308
+ int serverReceivedSuccessCount = 0 ;
309
+
310
+
311
+ var server = new TestServers . GcmTestServer ( ) ;
312
+
313
+ server . MessageResponseFilters . AddRange ( responseFilters ) ;
314
+
315
+ server . Start ( testPort , response => {
316
+ serverReceivedCount += ( int ) response . NumberOfCanonicalIds ;
317
+ serverReceivedSuccessCount += ( int ) response . NumberOfSuccesses ;
318
+ serverReceivedFailCount += ( int ) response . NumberOfFailures ;
319
+ } ) ;
320
+
321
+
322
+
323
+ var settings = new GcmPushChannelSettings ( "SENDERAUTHTOKEN" ) ;
324
+ settings . OverrideUrl ( "http://localhost:" + ( testPort ) + "/" ) ;
325
+
326
+ var push = new GcmPushService ( settings ) ;
327
+ push . OnNotificationSent += ( sender , notification1 ) => {
328
+ pushSuccessCount ++ ;
329
+ sentCallback ( sender , notification1 ) ;
330
+ } ;
331
+ push . OnNotificationFailed += ( sender , notification1 , error ) => {
332
+ pushFailCount ++ ;
333
+ failedCallback ( sender , notification1 , error ) ;
334
+ } ;
335
+ push . OnDeviceSubscriptionChanged += ( sender , oldSubscriptionId , newSubscriptionId , notification ) => subscriptionChangedCallback ( sender , oldSubscriptionId , newSubscriptionId , notification ) ;
336
+ push . OnDeviceSubscriptionExpired += ( sender , expiredSubscriptionId , expirationDateUtc , notification ) => subscriptionExpiredCallback ( sender , expiredSubscriptionId , expirationDateUtc , notification ) ;
337
+
338
+
339
+ foreach ( var n in notifications )
340
+ push . QueueNotification ( n ) ;
341
+
342
+ push . Stop ( ) ;
343
+ push . Dispose ( ) ;
344
+
345
+ server . Dispose ( ) ;
346
+ //waitServerFinished.WaitOne();
347
+
348
+ Console . WriteLine ( "TEST-> DISPOSE." ) ;
349
+ }
200
350
}
201
351
}
0 commit comments