@@ -921,236 +921,10 @@ def send_one_message(val)
921921 end
922922
923923 describe "#each_batch" do
924- let ( :message_payload ) { 'a' * 10 }
925-
926- before do
927- @topic = SecureRandom . base64 ( 10 ) . tr ( '+=/' , '' )
928- end
929-
930- after do
931- @topic = nil
932- end
933-
934- def topic_name
935- @topic
936- end
937-
938- def produce_n ( n )
939- handles = [ ]
940- n . times do |i |
941- handles << producer . produce (
942- topic : topic_name ,
943- payload : i % 10 == 0 ? nil : Time . new . to_f . to_s ,
944- key : i . to_s ,
945- partition : 0
946- )
947- end
948- handles . each ( &:wait )
949- end
950-
951- def new_message
952- instance_double ( "Rdkafka::Consumer::Message" ) . tap do |message |
953- allow ( message ) . to receive ( :payload ) . and_return ( message_payload )
954- end
955- end
956-
957- it "retrieves messages produced into a topic" do
958- # This is the only each_batch test that actually produces real messages
959- # into a topic in the real kafka of the container.
960- #
961- # The other tests stub 'poll' which makes them faster and more reliable,
962- # but it makes sense to keep a single test with a fully integrated flow.
963- # This will help to catch breaking changes in the behavior of 'poll',
964- # libdrkafka, or Kafka.
965- #
966- # This is, in effect, an integration test and the subsequent specs are
967- # unit tests.
968- admin = rdkafka_config . admin
969- create_topic_handle = admin . create_topic ( topic_name , 1 , 1 )
970- create_topic_handle . wait ( max_wait_timeout : 15.0 )
971- consumer . subscribe ( topic_name )
972- produce_n 42
973- all_yields = [ ]
974- consumer . each_batch ( max_items : 10 ) do |batch |
975- all_yields << batch
976- break if all_yields . flatten . size >= 42
977- end
978- expect ( all_yields . flatten . first ) . to be_a Rdkafka ::Consumer ::Message
979- expect ( all_yields . flatten . size ) . to eq 42
980- expect ( all_yields . size ) . to be > 4
981- expect ( all_yields . flatten . map ( &:key ) ) . to eq ( 0 ..41 ) . map { |x | x . to_s }
982- admin . close
983- end
984-
985- it "should batch poll results and yield arrays of messages" do
986- consumer . subscribe ( topic_name )
987- all_yields = [ ]
988- expect ( consumer )
989- . to receive ( :poll )
990- . exactly ( 10 ) . times
991- . and_return ( new_message )
992- consumer . each_batch ( max_items : 10 ) do |batch |
993- all_yields << batch
994- break if all_yields . flatten . size >= 10
995- end
996- expect ( all_yields . first ) . to be_instance_of ( Array )
997- expect ( all_yields . flatten . size ) . to eq 10
998- non_empty_yields = all_yields . reject { |batch | batch . empty? }
999- expect ( non_empty_yields . size ) . to be < 10
1000- end
1001-
1002- it "should yield a partial batch if the timeout is hit with some messages" do
1003- consumer . subscribe ( topic_name )
1004- poll_count = 0
1005- expect ( consumer )
1006- . to receive ( :poll )
1007- . at_least ( 3 ) . times do
1008- poll_count = poll_count + 1
1009- if poll_count > 2
1010- sleep 0.1
1011- nil
1012- else
1013- new_message
1014- end
1015- end
1016- all_yields = [ ]
1017- consumer . each_batch ( max_items : 10 ) do |batch |
1018- all_yields << batch
1019- break if all_yields . flatten . size >= 2
1020- end
1021- expect ( all_yields . flatten . size ) . to eq 2
1022- end
1023-
1024- it "should yield [] if nothing is received before the timeout" do
1025- admin = rdkafka_config . admin
1026- create_topic_handle = admin . create_topic ( topic_name , 1 , 1 )
1027- create_topic_handle . wait ( max_wait_timeout : 15.0 )
1028- consumer . subscribe ( topic_name )
1029- consumer . each_batch do |batch |
1030- expect ( batch ) . to eq ( [ ] )
1031- break
1032- end
1033- admin . close
1034- end
1035-
1036- it "should yield batchs of max_items in size if messages are already fetched" do
1037- yielded_batches = [ ]
1038- expect ( consumer )
1039- . to receive ( :poll )
1040- . with ( anything )
1041- . exactly ( 20 ) . times
1042- . and_return ( new_message )
1043-
1044- consumer . each_batch ( max_items : 10 , timeout_ms : 500 ) do |batch |
1045- yielded_batches << batch
1046- break if yielded_batches . flatten . size >= 20
1047- break if yielded_batches . size >= 20 # so failure doesn't hang
1048- end
1049- expect ( yielded_batches . size ) . to eq 2
1050- expect ( yielded_batches . map ( &:size ) ) . to eq 2 . times . map { 10 }
1051- end
1052-
1053- it "should yield batchs as soon as bytes_threshold is hit" do
1054- yielded_batches = [ ]
1055- expect ( consumer )
1056- . to receive ( :poll )
1057- . with ( anything )
1058- . exactly ( 20 ) . times
1059- . and_return ( new_message )
1060-
1061- consumer . each_batch ( bytes_threshold : message_payload . size * 4 , timeout_ms : 500 ) do |batch |
1062- yielded_batches << batch
1063- break if yielded_batches . flatten . size >= 20
1064- break if yielded_batches . size >= 20 # so failure doesn't hang
1065- end
1066- expect ( yielded_batches . size ) . to eq 5
1067- expect ( yielded_batches . map ( &:size ) ) . to eq 5 . times . map { 4 }
1068- end
1069-
1070- context "error raised from poll and yield_on_error is true" do
1071- it "should yield buffered exceptions on rebalance, then break" do
1072- config = rdkafka_consumer_config (
1073- {
1074- :"enable.auto.commit" => false ,
1075- :"enable.auto.offset.store" => false
1076- }
1077- )
1078- consumer = config . consumer
1079- consumer . subscribe ( topic_name )
1080- batches_yielded = [ ]
1081- exceptions_yielded = [ ]
1082- each_batch_iterations = 0
1083- poll_count = 0
1084- expect ( consumer )
1085- . to receive ( :poll )
1086- . with ( anything )
1087- . exactly ( 3 ) . times
1088- . and_wrap_original do |method , *args |
1089- poll_count = poll_count + 1
1090- if poll_count == 3
1091- raise Rdkafka ::RdkafkaError . new ( 27 ,
1092- "partitions ... too ... heavy ... must ... rebalance" )
1093- else
1094- new_message
1095- end
1096- end
1097- expect {
1098- consumer . each_batch ( max_items : 30 , yield_on_error : true ) do |batch , pending_error |
1099- batches_yielded << batch
1100- exceptions_yielded << pending_error
1101- each_batch_iterations = each_batch_iterations + 1
1102- end
1103- } . to raise_error ( Rdkafka ::RdkafkaError )
1104- expect ( poll_count ) . to eq 3
1105- expect ( each_batch_iterations ) . to eq 1
1106- expect ( batches_yielded . size ) . to eq 1
1107- expect ( batches_yielded . first . size ) . to eq 2
1108- expect ( exceptions_yielded . flatten . size ) . to eq 1
1109- expect ( exceptions_yielded . flatten . first ) . to be_instance_of ( Rdkafka ::RdkafkaError )
1110- consumer . close
1111- end
1112- end
1113-
1114- context "error raised from poll and yield_on_error is false" do
1115- it "should yield buffered exceptions on rebalance, then break" do
1116- config = rdkafka_consumer_config (
1117- {
1118- :"enable.auto.commit" => false ,
1119- :"enable.auto.offset.store" => false
1120- }
1121- )
1122- consumer = config . consumer
1123- consumer . subscribe ( topic_name )
1124- batches_yielded = [ ]
1125- exceptions_yielded = [ ]
1126- each_batch_iterations = 0
1127- poll_count = 0
1128- expect ( consumer )
1129- . to receive ( :poll )
1130- . with ( anything )
1131- . exactly ( 3 ) . times
1132- . and_wrap_original do |method , *args |
1133- poll_count = poll_count + 1
1134- if poll_count == 3
1135- raise Rdkafka ::RdkafkaError . new ( 27 ,
1136- "partitions ... too ... heavy ... must ... rebalance" )
1137- else
1138- new_message
1139- end
1140- end
1141- expect {
1142- consumer . each_batch ( max_items : 30 , yield_on_error : false ) do |batch , pending_error |
1143- batches_yielded << batch
1144- exceptions_yielded << pending_error
1145- each_batch_iterations = each_batch_iterations + 1
1146- end
1147- } . to raise_error ( Rdkafka ::RdkafkaError )
1148- expect ( poll_count ) . to eq 3
1149- expect ( each_batch_iterations ) . to eq 0
1150- expect ( batches_yielded . size ) . to eq 0
1151- expect ( exceptions_yielded . size ) . to eq 0
1152- consumer . close
1153- end
924+ it 'expect to raise an error' do
925+ expect do
926+ consumer . each_batch { }
927+ end . to raise_error ( NotImplementedError )
1154928 end
1155929 end
1156930
@@ -1317,7 +1091,6 @@ def on_partitions_revoked(list)
13171091 {
13181092 :subscribe => [ nil ] ,
13191093 :unsubscribe => nil ,
1320- :each_batch => nil ,
13211094 :pause => [ nil ] ,
13221095 :resume => [ nil ] ,
13231096 :subscription => nil ,
0 commit comments