@@ -63,6 +63,26 @@ public function testGetSetBackgroundBatching()
6363 $ this ->assertTrue ($ adapter ->isBackgroundBatchingEnabled ());
6464 }
6565
66+ public function testGetSetReturnImmediately ()
67+ {
68+ $ client = Mockery::mock (PubSubClient::class);
69+ $ adapter = new GoogleCloudPubSubAdapter ($ client );
70+ $ this ->assertFalse ($ adapter ->getReturnImmediately ());
71+
72+ $ adapter ->setReturnImmediately (true );
73+ $ this ->assertTrue ($ adapter ->getReturnImmediately ());
74+ }
75+
76+ public function testGetSetReturnImmediatelyPause ()
77+ {
78+ $ client = Mockery::mock (PubSubClient::class);
79+ $ adapter = new GoogleCloudPubSubAdapter ($ client );
80+ $ this ->assertEquals (500000 , $ adapter ->getReturnImmediatelyPause ());
81+
82+ $ adapter ->setReturnImmediatelyPause (1000000 );
83+ $ this ->assertEquals (1000000 , $ adapter ->getReturnImmediatelyPause ());
84+ }
85+
6686 public function testPublishWhenTopicMustBeCreated ()
6787 {
6888 $ topic = Mockery::mock (Topic::class);
@@ -320,6 +340,7 @@ public function testSubscribeWhenSubscriptionMustBeCreated()
320340 'timeoutMillis ' => null ,
321341 ],
322342 'maxMessages ' => 1000 ,
343+ 'returnImmediately ' => false
323344 ])
324345 ->once ()
325346 ->andReturn ($ messageBatch1 );
@@ -336,6 +357,7 @@ public function testSubscribeWhenSubscriptionMustBeCreated()
336357 'timeoutMillis ' => null ,
337358 ],
338359 'maxMessages ' => 1000 ,
360+ 'returnImmediately ' => false
339361 ])
340362 ->once ()
341363 ->andReturn ($ messageBatch2 );
@@ -397,6 +419,7 @@ public function testSubscribeWhenSubscriptionExists()
397419 'timeoutMillis ' => null ,
398420 ],
399421 'maxMessages ' => 1000 ,
422+ 'returnImmediately ' => false
400423 ])
401424 ->once ()
402425 ->andReturn ($ messageBatch1 );
@@ -412,6 +435,7 @@ public function testSubscribeWhenSubscriptionExists()
412435 'timeoutMillis ' => null ,
413436 ],
414437 'maxMessages ' => 1000 ,
438+ 'returnImmediately ' => false
415439 ])
416440 ->once ()
417441 ->andReturn ($ messageBatch2 );
@@ -471,6 +495,7 @@ public function testSubscribeWhenAutoTopicCreationIsDisabled()
471495 'timeoutMillis ' => null ,
472496 ],
473497 'maxMessages ' => 1000 ,
498+ 'returnImmediately ' => false
474499 ])
475500 ->once ()
476501 ->andReturn ($ messageBatch1 );
@@ -486,6 +511,7 @@ public function testSubscribeWhenAutoTopicCreationIsDisabled()
486511 'timeoutMillis ' => null ,
487512 ],
488513 'maxMessages ' => 1000 ,
514+ 'returnImmediately ' => false
489515 ])
490516 ->once ()
491517 ->andReturn ($ messageBatch2 );
@@ -521,4 +547,81 @@ public function testSubscribeWhenAutoTopicCreationIsDisabled()
521547
522548 $ adapter ->subscribe ('channel_name ' , [$ handler1 , 'handle ' ]);
523549 }
550+
551+ public function testSubscribeWhenReturnImmediatelyIsEnabled ()
552+ {
553+ $ message1 = new Message (['data ' => '{"hello":"world"} ' ], ['ackId ' => 1 ]);
554+ $ message2 = new Message (['data ' => '"this is a string" ' ], ['ackId ' => 2 ]);
555+ $ message3 = new Message (['data ' => '"unsubscribe" ' ], ['ackId ' => 3 ]);
556+
557+ $ messageBatch1 = [
558+ $ message1 ,
559+ $ message2 ,
560+ ];
561+
562+ $ messageBatch2 = [
563+ $ message3 ,
564+ ];
565+
566+ $ subscription = Mockery::mock (Subscription::class);
567+ $ subscription ->shouldReceive ('exists ' )
568+ ->once ()
569+ ->andReturn (true );
570+ $ subscription ->shouldNotHaveReceived ('create ' );
571+
572+ $ expectedPullOptions = [
573+ 'grpcOptions ' => [
574+ 'timeoutMillis ' => null ,
575+ ],
576+ 'maxMessages ' => 1000 ,
577+ 'returnImmediately ' => true
578+ ];
579+
580+ $ subscription ->shouldReceive ('pull ' )
581+ ->with ($ expectedPullOptions )
582+ ->once ()
583+ ->andReturn ($ messageBatch1 );
584+ $ subscription ->shouldReceive ('acknowledge ' )
585+ ->with ($ message1 )
586+ ->once ();
587+ $ subscription ->shouldReceive ('acknowledge ' )
588+ ->with ($ message2 )
589+ ->once ();
590+
591+ $ subscription ->shouldReceive ('pull ' )
592+ ->with ($ expectedPullOptions )
593+ ->once ()
594+ ->andReturn ($ messageBatch2 );
595+ $ subscription ->shouldReceive ('acknowledge ' )
596+ ->with ($ message3 )
597+ ->once ();
598+
599+ $ topic = Mockery::mock (Topic::class);
600+ $ topic ->shouldReceive ('exists ' )
601+ ->once ()
602+ ->andReturn (true );
603+ $ topic ->shouldNotHaveReceived ('create ' );
604+ $ topic ->shouldReceive ('subscription ' )
605+ ->with ('default.channel_name ' )
606+ ->once ()
607+ ->andReturn ($ subscription );
608+
609+ $ client = Mockery::mock (PubSubClient::class);
610+ $ client ->shouldReceive ('topic ' )
611+ ->with ('channel_name ' )
612+ ->once ()
613+ ->andReturn ($ topic );
614+
615+ $ handler1 = Mockery::mock (\stdClass::class);
616+ $ handler1 ->shouldReceive ('handle ' )
617+ ->with (['hello ' => 'world ' ])
618+ ->once ();
619+ $ handler1 ->shouldReceive ('handle ' )
620+ ->with ('this is a string ' )
621+ ->once ();
622+
623+ $ adapter = new GoogleCloudPubSubAdapter ($ client );
624+ $ adapter ->setReturnImmediately (true );
625+ $ adapter ->subscribe ('channel_name ' , [$ handler1 , 'handle ' ]);
626+ }
524627}
0 commit comments