@Override public void close() { close(Duration.ofMillis(0)); }
@After public void cleanup() { if (this.producer != null && !this.producer.closed()) this.producer.close(); }
@Test public void shouldThrowSendOffsetsToTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.sendOffsetsToTransaction(null, null); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@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 shouldThrowOnCommitTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.commitTransaction(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldThrowOnAbortTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.abortTransaction(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldThrowOnInitTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.initTransactions(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldThrowOnBeginTransactionIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.beginTransaction(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@Test public void shouldThrowOnFenceProducerIfProducerIsClosed() { buildMockProducer(true); producer.close(); try { producer.fenceProducer(); fail("Should have thrown as producer is already closed"); } catch (IllegalStateException e) { } }
@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 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(); }
@Override public void close() { MOCK_PRODUCER_MAP.remove(producerKey); try { if (closedMethod == null || !((Boolean) closedMethod.invoke(mockProducer))) { mockProducer.close(); } } catch (Exception e) { // Not expected. throw new RuntimeException(e); } } }