/** * Adds the record to the list of sent records. The {@link RecordMetadata} returned will be immediately satisfied. * * @see #history() */ @Override public synchronized Future<RecordMetadata> send(ProducerRecord<K, V> record) { return send(record, null); }
@Test public void shouldPublishMessagesOnlyAfterCommitIfTransactionsAreEnabled() { buildMockProducer(true); producer.initTransactions(); producer.beginTransaction(); producer.send(record1); producer.send(record2); assertTrue(producer.history().isEmpty()); producer.commitTransaction(); List<ProducerRecord<byte[], byte[]>> expectedResult = new ArrayList<>(); expectedResult.add(record1); expectedResult.add(record2); assertThat(producer.history(), equalTo(expectedResult)); }
@Test public void shouldPreserveCommittedMessagesOnAbortIfTransactionsAreEnabled() { buildMockProducer(true); producer.initTransactions(); producer.beginTransaction(); producer.send(record1); producer.send(record2); producer.commitTransaction(); producer.beginTransaction(); producer.abortTransaction(); List<ProducerRecord<byte[], byte[]>> expectedResult = new ArrayList<>(); expectedResult.add(record1); expectedResult.add(record2); assertThat(producer.history(), equalTo(expectedResult)); }
@Test public void shouldFlushOnCommitForNonAutoCompleteIfTransactionsAreEnabled() { buildMockProducer(false); producer.initTransactions(); producer.beginTransaction(); Future<RecordMetadata> md1 = producer.send(record1); Future<RecordMetadata> md2 = producer.send(record2); assertFalse(md1.isDone()); assertFalse(md2.isDone()); producer.commitTransaction(); assertTrue(md1.isDone()); assertTrue(md2.isDone()); }
@Test public void shouldThrowOnAbortForNonAutoCompleteIfTransactionsAreEnabled() throws Exception { buildMockProducer(false); producer.initTransactions(); producer.beginTransaction(); Future<RecordMetadata> md1 = producer.send(record1); assertFalse(md1.isDone()); producer.abortTransaction(); assertTrue(md1.isDone()); }
@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()); }
@Test public void shouldNotBeFlushedWithNoAutoCompleteIfBufferedRecords() { buildMockProducer(false); producer.send(record1); assertFalse(producer.flushed()); }
@Test public void shouldThrowOnSendIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.send(null); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldBeFlushedWithAutoCompleteIfBufferedRecords() { buildMockProducer(true); producer.send(record1); assertTrue(producer.flushed()); }
@Test public void shouldDropMessagesOnAbortIfTransactionsAreEnabled() { buildMockProducer(true); producer.initTransactions(); producer.beginTransaction(); producer.send(record1); producer.send(record2); producer.abortTransaction(); assertTrue(producer.history().isEmpty()); producer.beginTransaction(); producer.commitTransaction(); assertTrue(producer.history().isEmpty()); }
@Test public void testPartitioner() throws Exception { PartitionInfo partitionInfo0 = new PartitionInfo(topic, 0, null, null, null); PartitionInfo partitionInfo1 = new PartitionInfo(topic, 1, null, null, null); Cluster cluster = new Cluster(null, new ArrayList<Node>(0), asList(partitionInfo0, partitionInfo1), Collections.<String>emptySet(), Collections.<String>emptySet()); MockProducer<String, String> producer = new MockProducer<>(cluster, true, new DefaultPartitioner(), new StringSerializer(), new StringSerializer()); ProducerRecord<String, String> record = new ProducerRecord<>(topic, "key", "value"); Future<RecordMetadata> metadata = producer.send(record); assertEquals("Partition should be correct", 1, metadata.get().partition()); producer.clear(); assertEquals("Clear should erase our history", 0, producer.history().size()); producer.close(); }
@Test public void testAutoCompleteMock() throws Exception { buildMockProducer(true); Future<RecordMetadata> metadata = producer.send(record1); assertTrue("Send should be immediately complete", metadata.isDone()); assertFalse("Send should be successful", isError(metadata)); assertEquals("Offset should be 0", 0L, metadata.get().offset()); assertEquals(topic, metadata.get().topic()); assertEquals("We should have the record in our history", singletonList(record1), producer.history()); producer.clear(); assertEquals("Clear should erase our history", 0, producer.history().size()); }
@Test public void shouldNotBeFlushedAfterFlush() { buildMockProducer(false); producer.send(record1); producer.flush(); assertTrue(producer.flushed()); }
@Test public void shouldThrowOnSendIfProducerGotFenced() { buildMockProducer(true); producer.initTransactions(); producer.fenceProducer(); try { producer.send(null); fail("Should have thrown as producer is fenced off"); } catch (KafkaException e) { assertTrue("The root cause of the exception should be ProducerFenced", e.getCause() instanceof ProducerFencedException); } }
/** * Adds the record to the list of sent records. The {@link RecordMetadata} returned will be immediately satisfied. * * @see #history() */ @Override public synchronized Future<RecordMetadata> send(ProducerRecord<byte[], byte[]> record) { return send(record, null); }
@Override public synchronized Future<RecordMetadata> send(ProducerRecord<String, String> record, Callback callback) { callback.onCompletion(null, new SimulatedWriteException()); return super.send(record, callback); } }
@Override public synchronized Future<RecordMetadata> send(ProducerRecord<String, String> record, Callback callback) { callback.onCompletion(null, new SimulatedWriteException()); return super.send(record, callback); } }