77import org .junit .Test ;
88
99import java .util .ArrayList ;
10+ import java .util .Collections ;
1011import java .util .List ;
1112import java .util .stream .IntStream ;
1213
@@ -85,6 +86,31 @@ public void testAddSeveralCompleteRecord() {
8586 .isEqualTo (new OffsetAndMetadata (1002L ));
8687 }
8788
89+ @ Test
90+ public void testClearAll () {
91+ final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
92+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
93+ records .add (new ConsumerRecord <>(testingTopic , 102 , 1002 , defaultKey , defaultMsg ));
94+ records .add (new ConsumerRecord <>(testingTopic , 103 , 1003 , defaultKey , defaultMsg ));
95+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1004 , defaultKey , defaultMsg ));
96+
97+ for (ConsumerRecord <Object , Object > record : records ) {
98+ progress .markPendingRecord (record );
99+ progress .markCompletedRecord (record );
100+ }
101+
102+ assertThat (progress .noCompletedRecords ()).isFalse ();
103+ assertThat (progress .noPendingRecords ()).isFalse ();
104+ progress .clearAll ();
105+ assertThat (progress .noCompletedRecords ()).isTrue ();
106+ assertThat (progress .noPendingRecords ()).isTrue ();
107+ }
108+
109+ @ Test
110+ public void testCompletedOffsetsToCommitOnEmptyCompletedRecords () {
111+ assertThat (progress .completedOffsetsToCommit ()).isEmpty ();
112+ }
113+
88114 @ Test
89115 public void testRevokePartitions () {
90116 final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
@@ -106,6 +132,98 @@ public void testRevokePartitions() {
106132 .containsValue (new OffsetAndMetadata (1004L ));
107133 }
108134
135+ @ Test
136+ public void testNoOffsetsToCommit () {
137+ assertThat (progress .noOffsetsToCommit ()).isTrue ();
138+ }
139+
140+ @ Test
141+ public void testNoOffsetsToCommit2 () {
142+ final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
143+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
144+ records .add (new ConsumerRecord <>(testingTopic , 102 , 1002 , defaultKey , defaultMsg ));
145+ records .add (new ConsumerRecord <>(testingTopic , 103 , 1003 , defaultKey , defaultMsg ));
146+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1004 , defaultKey , defaultMsg ));
147+
148+ for (ConsumerRecord <Object , Object > record : records ) {
149+ progress .markPendingRecord (record );
150+ }
151+
152+ assertThat (progress .noOffsetsToCommit ()).isTrue ();
153+ }
154+
155+ @ Test
156+ public void testNoOffsetsToCommit3 () {
157+ final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
158+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
159+ records .add (new ConsumerRecord <>(testingTopic , 102 , 1002 , defaultKey , defaultMsg ));
160+ records .add (new ConsumerRecord <>(testingTopic , 103 , 1003 , defaultKey , defaultMsg ));
161+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1004 , defaultKey , defaultMsg ));
162+
163+ for (ConsumerRecord <Object , Object > record : records ) {
164+ progress .markPendingRecord (record );
165+ }
166+
167+ progress .markCompletedRecord (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
168+ assertThat (progress .noOffsetsToCommit ()).isFalse ();
169+ }
170+
171+ @ Test
172+ public void testUpdateCommittedOffsets () {
173+ final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
174+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
175+ records .add (new ConsumerRecord <>(testingTopic , 102 , 1002 , defaultKey , defaultMsg ));
176+ records .add (new ConsumerRecord <>(testingTopic , 103 , 1003 , defaultKey , defaultMsg ));
177+
178+ for (ConsumerRecord <Object , Object > record : records ) {
179+ progress .markPendingRecord (record );
180+ progress .markCompletedRecord (record );
181+ }
182+
183+ assertThat (progress .completedOffsetsToCommit ()).hasSize (3 );
184+ progress .updateCommittedOffsets (buildCommitOffsets (records ));
185+ assertThat (progress .completedOffsetsToCommit ()).isEmpty ();
186+ }
187+
188+ @ Test
189+ public void testUpdateCommittedOffsetsWithoutProgress () {
190+ final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
191+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
192+ records .add (new ConsumerRecord <>(testingTopic , 102 , 1002 , defaultKey , defaultMsg ));
193+ records .add (new ConsumerRecord <>(testingTopic , 103 , 1003 , defaultKey , defaultMsg ));
194+
195+ for (ConsumerRecord <Object , Object > record : records ) {
196+ progress .markPendingRecord (record );
197+ progress .markCompletedRecord (record );
198+ }
199+
200+ progress .clearFor (Collections .singletonList (new TopicPartition (testingTopic , 101 )));
201+ assertThat (progress .completedOffsetsToCommit ()).hasSize (2 );
202+ progress .updateCommittedOffsets (buildCommitOffsets (records ));
203+ assertThat (progress .completedOffsetsToCommit ()).isEmpty ();
204+ }
205+
206+ @ Test
207+ public void testCompletedPartitionsWithoutProgress () {
208+ final List <ConsumerRecord <Object , Object >> records = new ArrayList <>();
209+ records .add (new ConsumerRecord <>(testingTopic , 101 , 1001 , defaultKey , defaultMsg ));
210+ records .add (new ConsumerRecord <>(testingTopic , 102 , 1002 , defaultKey , defaultMsg ));
211+ records .add (new ConsumerRecord <>(testingTopic , 103 , 1003 , defaultKey , defaultMsg ));
212+
213+ for (ConsumerRecord <Object , Object > record : records ) {
214+ progress .markPendingRecord (record );
215+ progress .markCompletedRecord (record );
216+ }
217+
218+ progress .markPendingRecord (new ConsumerRecord <>(testingTopic , 102 , 1003 , defaultKey , defaultMsg ));
219+
220+ progress .clearFor (Collections .singletonList (new TopicPartition (testingTopic , 101 )));
221+ assertThat (progress .completedPartitions (buildCommitOffsets (records )))
222+ .hasSize (2 )
223+ .contains (new TopicPartition (testingTopic , 101 ),
224+ new TopicPartition (testingTopic , 103 ));
225+ }
226+
109227 private TopicPartition partition (int partition ) {
110228 return new TopicPartition (testingTopic , partition );
111229 }
0 commit comments