@@ -779,10 +779,35 @@ def test_context_mgr_failure(self):
779779 self .assertEqual (list (batch .messages ), [MESSAGE1 , MESSAGE2 ])
780780 self .assertEqual (getattr (api , '_topic_published' , self ), self )
781781
782+ def test_batch_messages (self ):
783+ # Establish that a batch actually batches messsages in the expected
784+ # way.
785+ client = _Client (project = 'PROJECT' )
786+ topic = _Topic (name = 'TOPIC' )
787+
788+ # Track commits, but do not perform them.
789+ Batch = self ._get_target_class ()
790+ with mock .patch .object (Batch , 'commit' ) as commit :
791+ with self ._make_one (topic , client = client ) as batch :
792+ self .assertIsInstance (batch , Batch )
793+
794+ # Publish four messages and establish that the batch does
795+ # not commit.
796+ for i in range (0 , 4 ):
797+ batch .publish ('Batch message %d.' % (i ,))
798+ commit .assert_not_called ()
799+
800+ # Check the contents of the batch.
801+ self .assertEqual (batch .messages , [
802+ {'data' : 'Batch message 0.' , 'attributes' : {}},
803+ {'data' : 'Batch message 1.' , 'attributes' : {}},
804+ {'data' : 'Batch message 2.' , 'attributes' : {}},
805+ {'data' : 'Batch message 3.' , 'attributes' : {}},
806+ ])
807+
782808 def test_message_count_autocommit (self ):
783- """Establish that if the batch is assigned to take a maximum
784- number of messages, that it commits when it reaches that maximum.
785- """
809+ # Establish that if the batch is assigned to take a maximum
810+ # number of messages, that it commits when it reaches that maximum.
786811 client = _Client (project = 'PROJECT' )
787812 topic = _Topic (name = 'TOPIC' )
788813
@@ -795,17 +820,11 @@ def test_message_count_autocommit(self):
795820 # Publish four messages and establish that the batch does
796821 # not commit.
797822 for i in range (0 , 4 ):
798- batch .publish ({
799- 'attributes' : {},
800- 'data' : 'Batch message %d.' % (i ,),
801- })
823+ batch .publish ('Batch message %d.' % (i ,))
802824 commit .assert_not_called ()
803825
804826 # Publish a fifth message and observe the commit.
805- batch .publish ({
806- 'attributes' : {},
807- 'data' : 'The final call to trigger a commit!' ,
808- })
827+ batch .publish ('The final call to trigger a commit!' )
809828 commit .assert_called_once_with ()
810829
811830 # There should be a second commit after the context manager
@@ -814,9 +833,8 @@ def test_message_count_autocommit(self):
814833
815834 @mock .patch ('time.time' )
816835 def test_message_time_autocommit (self , mock_time ):
817- """Establish that if the batch is sufficiently old, that it commits
818- the next time it receives a publish.
819- """
836+ # Establish that if the batch is sufficiently old, that it commits
837+ # the next time it receives a publish.
820838 client = _Client (project = 'PROJECT' )
821839 topic = _Topic (name = 'TOPIC' )
822840
@@ -830,20 +848,40 @@ def test_message_time_autocommit(self, mock_time):
830848 # Publish some messages and establish that the batch does
831849 # not commit.
832850 for i in range (0 , 10 ):
833- batch .publish ({
834- 'attributes' : {},
835- 'data' : 'Batch message %d.' % (i ,),
836- })
851+ batch .publish ('Batch message %d.' % (i ,))
837852 commit .assert_not_called ()
838853
839854 # Move time ahead so that this batch is too old.
840855 mock_time .return_value = 10.0
841856
842857 # Publish another message and observe the commit.
843- batch .publish ({
844- 'attributes' : {},
845- 'data' : 'The final call to trigger a commit!' ,
846- })
858+ batch .publish ('The final call to trigger a commit!' )
859+ commit .assert_called_once_with ()
860+
861+ # There should be a second commit after the context manager
862+ # exits.
863+ self .assertEqual (commit .call_count , 2 )
864+
865+ def test_message_size_autocommit (self ):
866+ # Establish that if the batch is sufficiently large, that it
867+ # auto-commits.
868+ client = _Client (project = 'PROJECT' )
869+ topic = _Topic (name = 'TOPIC' )
870+
871+ # Track commits, but do not perform them.
872+ Batch = self ._get_target_class ()
873+ with mock .patch .object (Batch , 'commit' ) as commit :
874+ with self ._make_one (topic , client = client , max_size = 100 ) as batch :
875+ self .assertIsInstance (batch , Batch )
876+
877+ # Publish a short (< 100 bytes) message and establish that
878+ # the batch does not commit.
879+ batch .publish (b'foo' )
880+ commit .assert_not_called ()
881+
882+ # Publish another message and observe the commit.
883+ batch .publish (u'The final call to trigger a commit, because '
884+ u'this message is sufficiently long.' )
847885 commit .assert_called_once_with ()
848886
849887 # There should be a second commit after the context manager
0 commit comments