4
4
let! ( :logger ) { LOGGER }
5
5
let! ( :cluster ) { double ( :cluster ) }
6
6
let! ( :transaction_coordinator ) { double ( :broker ) }
7
+ let! ( :group_coordinator ) { double ( :broker ) }
7
8
8
9
let! ( :manager ) do
9
10
described_class . new ( logger : logger , cluster : cluster )
13
14
allow ( cluster ) . to receive ( :get_transaction_coordinator ) . and_return (
14
15
transaction_coordinator
15
16
)
17
+ allow ( cluster ) . to receive ( :get_group_coordinator ) . and_return (
18
+ group_coordinator
19
+ )
16
20
allow ( transaction_coordinator ) . to receive ( :init_producer_id ) . and_return (
17
21
Kafka ::Protocol ::InitProducerIDResponse . new (
18
22
error_code : 0 ,
586
590
error_code : 0
587
591
)
588
592
)
589
- allow ( transaction_coordinator ) . to receive ( :txn_offset_commit ) . and_return (
590
- Kafka ::Protocol ::TxnOffsetCommitResponse . new (
591
- error_code : 0
593
+ allow ( group_coordinator ) . to receive ( :txn_offset_commit ) . and_return (
594
+ txn_offset_commit_response (
595
+ 'hello' => [ 1 ] ,
596
+ 'world' => [ 2 ]
592
597
)
593
598
)
594
599
end
595
600
596
601
it 'notifies transaction coordinator' do
597
602
manager . send_offsets_to_txn ( offsets : [ 1 , 2 ] , group_id : 1 )
598
603
expect ( transaction_coordinator ) . to have_received ( :add_offsets_to_txn )
599
- expect ( transaction_coordinator ) . to have_received ( :txn_offset_commit )
604
+ expect ( group_coordinator ) . to have_received ( :txn_offset_commit )
605
+ end
606
+ end
607
+
608
+ context 'transaction coordinator returns error' do
609
+ before do
610
+ manager . init_transactions
611
+ manager . begin_transaction
612
+ allow ( transaction_coordinator ) . to receive ( :add_offsets_to_txn ) . and_return (
613
+ Kafka ::Protocol ::AddOffsetsToTxnResponse . new (
614
+ error_code : 47
615
+ )
616
+ )
617
+ end
618
+
619
+ it 'raises exception' do
620
+ expect do
621
+ manager . send_offsets_to_txn ( offsets : [ 1 , 2 ] , group_id : 1 )
622
+ end . to raise_error ( Kafka ::InvalidProducerEpochError )
623
+ end
624
+
625
+ it 'changes state to error' do
626
+ begin
627
+ manager . send_offsets_to_txn ( offsets : [ 1 , 2 ] , group_id : 1 )
628
+ rescue ; end
629
+ expect ( manager . error? ) . to eql ( true )
630
+ end
631
+ end
632
+
633
+ context 'group coordinator returns error' do
634
+ before do
635
+ manager . init_transactions
636
+ manager . begin_transaction
637
+ allow ( transaction_coordinator ) . to receive ( :add_offsets_to_txn ) . and_return (
638
+ Kafka ::Protocol ::AddOffsetsToTxnResponse . new (
639
+ error_code : 0
640
+ )
641
+ )
642
+ allow ( group_coordinator ) . to receive ( :txn_offset_commit ) . and_return (
643
+ txn_offset_commit_response (
644
+ { 'hello' => [ 1 ] , 'world' => [ 2 ] } ,
645
+ error_code : 47
646
+ )
647
+ )
648
+ end
649
+
650
+ it 'raises exception' do
651
+ expect do
652
+ manager . send_offsets_to_txn ( offsets : [ 1 , 2 ] , group_id : 1 )
653
+ end . to raise_error ( Kafka ::InvalidProducerEpochError )
654
+ end
655
+
656
+ it 'changes state to error' do
657
+ begin
658
+ manager . send_offsets_to_txn ( offsets : [ 1 , 2 ] , group_id : 1 )
659
+ rescue ; end
660
+ expect ( manager . error? ) . to eql ( true )
600
661
end
601
662
end
602
663
end
@@ -617,3 +678,19 @@ def success_add_partitions_to_txn_response(topics)
617
678
end
618
679
)
619
680
end
681
+
682
+ def txn_offset_commit_response ( topics , error_code : 0 )
683
+ Kafka ::Protocol ::TxnOffsetCommitResponse . new (
684
+ errors : topics . map do |topic , partitions |
685
+ Kafka ::Protocol ::TxnOffsetCommitResponse ::TopicPartitionsError . new (
686
+ topic : topic ,
687
+ partitions : partitions . map do |partition |
688
+ Kafka ::Protocol ::TxnOffsetCommitResponse ::PartitionError . new (
689
+ partition : partition ,
690
+ error_code : error_code
691
+ )
692
+ end
693
+ )
694
+ end
695
+ )
696
+ end
0 commit comments