23
23
import static org .junit .Assert .assertNull ;
24
24
import static org .junit .Assert .assertSame ;
25
25
import static org .junit .Assert .assertTrue ;
26
+ import static org .junit .Assert .fail ;
26
27
27
28
import com .google .cloud .AsyncPage ;
28
29
import com .google .cloud .Page ;
29
30
import com .google .cloud .RetryParams ;
30
31
import com .google .cloud .pubsub .PubSub .ListOption ;
31
32
import com .google .cloud .pubsub .spi .PubSubRpc ;
33
+ import com .google .cloud .pubsub .spi .PubSubRpc .PullCallback ;
34
+ import com .google .cloud .pubsub .spi .PubSubRpc .PullFuture ;
32
35
import com .google .cloud .pubsub .spi .PubSubRpcFactory ;
33
36
import com .google .common .base .Function ;
34
37
import com .google .common .collect .ImmutableList ;
55
58
import com .google .pubsub .v1 .PullRequest ;
56
59
import com .google .pubsub .v1 .PullResponse ;
57
60
61
+ import org .easymock .Capture ;
58
62
import org .easymock .EasyMock ;
59
63
import org .junit .After ;
60
64
import org .junit .Before ;
61
65
import org .junit .Rule ;
62
66
import org .junit .Test ;
63
67
import org .junit .rules .ExpectedException ;
64
68
69
+ import java .io .IOException ;
65
70
import java .util .Iterator ;
66
71
import java .util .List ;
67
72
import java .util .concurrent .ExecutionException ;
@@ -1229,7 +1234,7 @@ public void testListTopicSubscriptionsAsyncWithOptions()
1229
1234
}
1230
1235
1231
1236
@ Test
1232
- public void testPullMessages () {
1237
+ public void testPullMessages () throws ExecutionException , InterruptedException {
1233
1238
pubsub = new PubSubImpl (options , renewerMock );
1234
1239
PullRequest request = PullRequest .newBuilder ()
1235
1240
.setSubscription (SUBSCRIPTION_NAME_PB )
@@ -1243,10 +1248,16 @@ public void testPullMessages() {
1243
1248
.addReceivedMessages (MESSAGE_PB1 )
1244
1249
.addReceivedMessages (MESSAGE_PB2 )
1245
1250
.build ();
1246
- EasyMock .expect (pubsubRpcMock .pull (request )).andReturn (Futures .immediateFuture (response ));
1251
+ Capture <PullCallback > callback = Capture .newInstance ();
1252
+ PullFuture futureMock = EasyMock .createStrictMock (PullFuture .class );
1253
+ futureMock .addCallback (EasyMock .capture (callback ));
1254
+ EasyMock .expectLastCall ();
1255
+ EasyMock .expect (futureMock .get ()).andReturn (response );
1256
+ EasyMock .expect (pubsubRpcMock .pull (request )).andReturn (futureMock );
1247
1257
renewerMock .add (SUBSCRIPTION , ImmutableList .of ("ackId1" , "ackId2" ));
1248
- EasyMock .replay (pubsubRpcMock , renewerMock );
1258
+ EasyMock .replay (pubsubRpcMock , renewerMock , futureMock );
1249
1259
Iterator <ReceivedMessage > messageIterator = pubsub .pull (SUBSCRIPTION , 42 );
1260
+ callback .getValue ().success (response );
1250
1261
EasyMock .reset (renewerMock );
1251
1262
for (ReceivedMessage message : messageList ) {
1252
1263
renewerMock .remove (SUBSCRIPTION , message .ackId ());
@@ -1256,6 +1267,7 @@ public void testPullMessages() {
1256
1267
while (messageIterator .hasNext ()) {
1257
1268
messageIterator .next ();
1258
1269
}
1270
+ EasyMock .verify (futureMock );
1259
1271
}
1260
1272
1261
1273
@ Test
@@ -1273,10 +1285,16 @@ public void testPullMessagesAsync() throws ExecutionException, InterruptedExcept
1273
1285
.addReceivedMessages (MESSAGE_PB1 )
1274
1286
.addReceivedMessages (MESSAGE_PB2 )
1275
1287
.build ();
1276
- EasyMock .expect (pubsubRpcMock .pull (request )).andReturn (Futures .immediateFuture (response ));
1288
+ Capture <PullCallback > callback = Capture .newInstance ();
1289
+ PullFuture futureMock = EasyMock .createStrictMock (PullFuture .class );
1290
+ futureMock .addCallback (EasyMock .capture (callback ));
1291
+ EasyMock .expectLastCall ();
1292
+ EasyMock .expect (futureMock .get ()).andReturn (response );
1293
+ EasyMock .expect (pubsubRpcMock .pull (request )).andReturn (futureMock );
1277
1294
renewerMock .add (SUBSCRIPTION , ImmutableList .of ("ackId1" , "ackId2" ));
1278
- EasyMock .replay (pubsubRpcMock , renewerMock );
1295
+ EasyMock .replay (pubsubRpcMock , renewerMock , futureMock );
1279
1296
Iterator <ReceivedMessage > messageIterator = pubsub .pullAsync (SUBSCRIPTION , 42 ).get ();
1297
+ callback .getValue ().success (response );
1280
1298
EasyMock .reset (renewerMock );
1281
1299
for (ReceivedMessage message : messageList ) {
1282
1300
renewerMock .remove (SUBSCRIPTION , message .ackId ());
@@ -1286,6 +1304,55 @@ public void testPullMessagesAsync() throws ExecutionException, InterruptedExcept
1286
1304
while (messageIterator .hasNext ()) {
1287
1305
messageIterator .next ();
1288
1306
}
1307
+ EasyMock .verify (futureMock );
1308
+ }
1309
+
1310
+ @ Test
1311
+ public void testPullMessagesError () throws ExecutionException , InterruptedException {
1312
+ pubsub = new PubSubImpl (options , renewerMock );
1313
+ PullRequest request = PullRequest .newBuilder ()
1314
+ .setSubscription (SUBSCRIPTION_NAME_PB )
1315
+ .setMaxMessages (42 )
1316
+ .setReturnImmediately (true )
1317
+ .build ();
1318
+ PubSubException exception = new PubSubException (new IOException (), false );
1319
+ PullFuture futureMock = EasyMock .createStrictMock (PullFuture .class );
1320
+ futureMock .addCallback (EasyMock .anyObject (PullCallback .class ));
1321
+ EasyMock .expectLastCall ();
1322
+ EasyMock .expect (futureMock .get ()).andThrow (new ExecutionException (exception ));
1323
+ EasyMock .expect (pubsubRpcMock .pull (request )).andReturn (futureMock );
1324
+ EasyMock .replay (pubsubRpcMock , renewerMock , futureMock );
1325
+ try {
1326
+ pubsub .pull (SUBSCRIPTION , 42 );
1327
+ fail ("Expected PubSubException" );
1328
+ } catch (PubSubException ex ) {
1329
+ assertSame (exception , ex );
1330
+ }
1331
+ EasyMock .verify (futureMock );
1332
+ }
1333
+
1334
+ @ Test
1335
+ public void testPullMessagesAsyncError () throws ExecutionException , InterruptedException {
1336
+ pubsub = new PubSubImpl (options , renewerMock );
1337
+ PullRequest request = PullRequest .newBuilder ()
1338
+ .setSubscription (SUBSCRIPTION_NAME_PB )
1339
+ .setMaxMessages (42 )
1340
+ .setReturnImmediately (true )
1341
+ .build ();
1342
+ PubSubException exception = new PubSubException (new IOException (), false );
1343
+ PullFuture futureMock = EasyMock .createStrictMock (PullFuture .class );
1344
+ futureMock .addCallback (EasyMock .anyObject (PullCallback .class ));
1345
+ EasyMock .expectLastCall ();
1346
+ EasyMock .expect (futureMock .get ()).andThrow (new ExecutionException (exception ));
1347
+ EasyMock .expect (pubsubRpcMock .pull (request )).andReturn (futureMock );
1348
+ EasyMock .replay (pubsubRpcMock , renewerMock , futureMock );
1349
+ try {
1350
+ pubsub .pullAsync (SUBSCRIPTION , 42 ).get ();
1351
+ fail ("Expected ExecutionException" );
1352
+ } catch (ExecutionException ex ) {
1353
+ assertSame (exception , ex .getCause ());
1354
+ }
1355
+ EasyMock .verify (futureMock );
1289
1356
}
1290
1357
1291
1358
@ Test
0 commit comments