@Override public void commitTransaction() throws ProducerFencedException { verifyProducerState(); verifyTransactionsInitialized(); verifyNoTransactionInFlight(); flush(); this.sent.addAll(this.uncommittedSends); if (!this.uncommittedConsumerGroupOffsets.isEmpty()) this.consumerGroupOffsets.add(this.uncommittedConsumerGroupOffsets); this.uncommittedSends.clear(); this.uncommittedConsumerGroupOffsets = new HashMap<>(); this.transactionCommitted = true; this.transactionAborted = false; this.transactionInFlight = false; ++this.commitCount; }
@Test public void shouldNotBeFlushedAfterFlush() { buildMockProducer(false); producer.send(record1); producer.flush(); assertTrue(producer.flushed()); }
@Override public void abortTransaction() throws ProducerFencedException { verifyProducerState(); verifyTransactionsInitialized(); verifyNoTransactionInFlight(); flush(); this.uncommittedSends.clear(); this.uncommittedConsumerGroupOffsets.clear(); this.transactionCommitted = false; this.transactionAborted = true; this.transactionInFlight = false; }
@Test public void shouldThrowOnFlushProducerIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.flush(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void testManualCompletion() throws Exception { buildMockProducer(false); Future<RecordMetadata> md1 = producer.send(record1); assertFalse("Send shouldn't have completed", md1.isDone()); Future<RecordMetadata> md2 = producer.send(record2); assertFalse("Send shouldn't have completed", md2.isDone()); assertTrue("Complete the first request", producer.completeNext()); assertFalse("Requst should be successful", isError(md1)); assertFalse("Second request still incomplete", md2.isDone()); IllegalArgumentException e = new IllegalArgumentException("blah"); assertTrue("Complete the second request with an error", producer.errorNext(e)); try { md2.get(); fail("Expected error to be thrown"); } catch (ExecutionException err) { assertEquals(e, err.getCause()); } assertFalse("No more requests to complete", producer.completeNext()); Future<RecordMetadata> md3 = producer.send(record1); Future<RecordMetadata> md4 = producer.send(record2); assertTrue("Requests should not be completed.", !md3.isDone() && !md4.isDone()); producer.flush(); assertTrue("Requests should be completed.", md3.isDone() && md4.isDone()); }