Search in sources :

Example 16 with Callback

use of org.apache.kafka.connect.util.Callback in project kafka by apache.

the class ConnectorsResourceTest method testListConnectorsNotSynced.

@Test(expected = ConnectException.class)
public void testListConnectorsNotSynced() throws Throwable {
    final Capture<Callback<Collection<String>>> cb = Capture.newInstance();
    herder.connectors(EasyMock.capture(cb));
    expectAndCallbackException(cb, new ConnectException("not synced"));
    PowerMock.replayAll();
    // throws
    connectorsResource.listConnectors(FORWARD);
}
Also used : Callback(org.apache.kafka.connect.util.Callback) ConnectException(org.apache.kafka.connect.errors.ConnectException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 17 with Callback

use of org.apache.kafka.connect.util.Callback in project kafka by apache.

the class ConnectorsResourceTest method testGetConnectorTaskConfigsConnectorNotFound.

@Test(expected = NotFoundException.class)
public void testGetConnectorTaskConfigsConnectorNotFound() throws Throwable {
    final Capture<Callback<List<TaskInfo>>> cb = Capture.newInstance();
    herder.taskConfigs(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));
    expectAndCallbackException(cb, new NotFoundException("connector not found"));
    PowerMock.replayAll();
    connectorsResource.getTaskConfigs(CONNECTOR_NAME, FORWARD);
    PowerMock.verifyAll();
}
Also used : TaskInfo(org.apache.kafka.connect.runtime.rest.entities.TaskInfo) Callback(org.apache.kafka.connect.util.Callback) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 18 with Callback

use of org.apache.kafka.connect.util.Callback in project kafka by apache.

the class ConnectorsResourceTest method testGetConnectorConfigConnectorNotFound.

@Test(expected = NotFoundException.class)
public void testGetConnectorConfigConnectorNotFound() throws Throwable {
    final Capture<Callback<Map<String, String>>> cb = Capture.newInstance();
    herder.connectorConfig(EasyMock.eq(CONNECTOR_NAME), EasyMock.capture(cb));
    expectAndCallbackException(cb, new NotFoundException("not found"));
    PowerMock.replayAll();
    connectorsResource.getConnectorConfig(CONNECTOR_NAME, FORWARD);
    PowerMock.verifyAll();
}
Also used : Callback(org.apache.kafka.connect.util.Callback) NotFoundException(org.apache.kafka.connect.errors.NotFoundException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 19 with Callback

use of org.apache.kafka.connect.util.Callback in project kafka by apache.

the class KafkaOffsetBackingStoreTest method testSetFailure.

@Test
public void testSetFailure() throws Exception {
    expectConfigure();
    expectStart(Collections.EMPTY_LIST);
    expectStop();
    // Set offsets
    Capture<org.apache.kafka.clients.producer.Callback> callback0 = EasyMock.newCapture();
    storeLog.send(EasyMock.aryEq(TP0_KEY.array()), EasyMock.aryEq(TP0_VALUE.array()), EasyMock.capture(callback0));
    PowerMock.expectLastCall();
    Capture<org.apache.kafka.clients.producer.Callback> callback1 = EasyMock.newCapture();
    storeLog.send(EasyMock.aryEq(TP1_KEY.array()), EasyMock.aryEq(TP1_VALUE.array()), EasyMock.capture(callback1));
    PowerMock.expectLastCall();
    Capture<org.apache.kafka.clients.producer.Callback> callback2 = EasyMock.newCapture();
    storeLog.send(EasyMock.aryEq(TP2_KEY.array()), EasyMock.aryEq(TP2_VALUE.array()), EasyMock.capture(callback2));
    PowerMock.expectLastCall();
    PowerMock.replayAll();
    store.configure(DEFAULT_DISTRIBUTED_CONFIG);
    store.start();
    // Set some offsets
    Map<ByteBuffer, ByteBuffer> toSet = new HashMap<>();
    toSet.put(TP0_KEY, TP0_VALUE);
    toSet.put(TP1_KEY, TP1_VALUE);
    toSet.put(TP2_KEY, TP2_VALUE);
    final AtomicBoolean invoked = new AtomicBoolean(false);
    final AtomicBoolean invokedFailure = new AtomicBoolean(false);
    Future<Void> setFuture = store.set(toSet, new Callback<Void>() {

        @Override
        public void onCompletion(Throwable error, Void result) {
            invoked.set(true);
            if (error != null)
                invokedFailure.set(true);
        }
    });
    assertFalse(setFuture.isDone());
    // Out of order callbacks shouldn't matter, should still require all to be invoked before invoking the callback
    // for the store's set callback
    callback1.getValue().onCompletion(null, null);
    assertFalse(invoked.get());
    callback2.getValue().onCompletion(null, new KafkaException("bogus error"));
    assertTrue(invoked.get());
    assertTrue(invokedFailure.get());
    callback0.getValue().onCompletion(null, null);
    try {
        setFuture.get(10000, TimeUnit.MILLISECONDS);
        fail("Should have seen KafkaException thrown when waiting on KafkaOffsetBackingStore.set() future");
    } catch (ExecutionException e) {
        // expected
        assertNotNull(e.getCause());
        assertTrue(e.getCause() instanceof KafkaException);
    }
    store.stop();
    PowerMock.verifyAll();
}
Also used : HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.apache.kafka.connect.util.Callback) KafkaException(org.apache.kafka.common.KafkaException) ExecutionException(java.util.concurrent.ExecutionException) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 20 with Callback

use of org.apache.kafka.connect.util.Callback in project kafka by apache.

the class KafkaOffsetBackingStoreTest method testGetSetNull.

@Test
public void testGetSetNull() throws Exception {
    expectConfigure();
    expectStart(Collections.EMPTY_LIST);
    // Set offsets
    Capture<org.apache.kafka.clients.producer.Callback> callback0 = EasyMock.newCapture();
    storeLog.send(EasyMock.isNull(byte[].class), EasyMock.aryEq(TP0_VALUE.array()), EasyMock.capture(callback0));
    PowerMock.expectLastCall();
    Capture<org.apache.kafka.clients.producer.Callback> callback1 = EasyMock.newCapture();
    storeLog.send(EasyMock.aryEq(TP1_KEY.array()), EasyMock.isNull(byte[].class), EasyMock.capture(callback1));
    PowerMock.expectLastCall();
    // Second get() should get the produced data and return the new values
    final Capture<Callback<Void>> secondGetReadToEndCallback = EasyMock.newCapture();
    storeLog.readToEnd(EasyMock.capture(secondGetReadToEndCallback));
    PowerMock.expectLastCall().andAnswer(new IAnswer<Object>() {

        @Override
        public Object answer() throws Throwable {
            capturedConsumedCallback.getValue().onCompletion(null, new ConsumerRecord<>(TOPIC, 0, 0, 0L, TimestampType.CREATE_TIME, 0L, 0, 0, (byte[]) null, TP0_VALUE.array()));
            capturedConsumedCallback.getValue().onCompletion(null, new ConsumerRecord<>(TOPIC, 1, 0, 0L, TimestampType.CREATE_TIME, 0L, 0, 0, TP1_KEY.array(), (byte[]) null));
            secondGetReadToEndCallback.getValue().onCompletion(null, null);
            return null;
        }
    });
    expectStop();
    PowerMock.replayAll();
    store.configure(DEFAULT_DISTRIBUTED_CONFIG);
    store.start();
    // Set offsets using null keys and values
    Map<ByteBuffer, ByteBuffer> toSet = new HashMap<>();
    toSet.put(null, TP0_VALUE);
    toSet.put(TP1_KEY, null);
    final AtomicBoolean invoked = new AtomicBoolean(false);
    Future<Void> setFuture = store.set(toSet, new Callback<Void>() {

        @Override
        public void onCompletion(Throwable error, Void result) {
            invoked.set(true);
        }
    });
    assertFalse(setFuture.isDone());
    // Out of order callbacks shouldn't matter, should still require all to be invoked before invoking the callback
    // for the store's set callback
    callback1.getValue().onCompletion(null, null);
    assertFalse(invoked.get());
    callback0.getValue().onCompletion(null, null);
    setFuture.get(10000, TimeUnit.MILLISECONDS);
    assertTrue(invoked.get());
    // Getting data should read to end of our published data and return it
    final AtomicBoolean secondGetInvokedAndPassed = new AtomicBoolean(false);
    store.get(Arrays.asList(null, TP1_KEY), new Callback<Map<ByteBuffer, ByteBuffer>>() {

        @Override
        public void onCompletion(Throwable error, Map<ByteBuffer, ByteBuffer> result) {
            assertEquals(TP0_VALUE, result.get(null));
            assertNull(result.get(TP1_KEY));
            secondGetInvokedAndPassed.set(true);
        }
    }).get(10000, TimeUnit.MILLISECONDS);
    assertTrue(secondGetInvokedAndPassed.get());
    store.stop();
    PowerMock.verifyAll();
}
Also used : HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Callback(org.apache.kafka.connect.util.Callback) HashMap(java.util.HashMap) Map(java.util.Map) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

Callback (org.apache.kafka.connect.util.Callback)20 Test (org.junit.Test)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)18 HashMap (java.util.HashMap)8 NotFoundException (org.apache.kafka.connect.errors.NotFoundException)6 ConnectorInfo (org.apache.kafka.connect.runtime.rest.entities.ConnectorInfo)6 Herder (org.apache.kafka.connect.runtime.Herder)5 RestServer (org.apache.kafka.connect.runtime.rest.RestServer)4 CreateConnectorRequest (org.apache.kafka.connect.runtime.rest.entities.CreateConnectorRequest)4 ByteBuffer (java.nio.ByteBuffer)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 ConsumerRecord (org.apache.kafka.clients.consumer.ConsumerRecord)3 ConnectorTaskId (org.apache.kafka.connect.util.ConnectorTaskId)3 Map (java.util.Map)2 NotAssignedException (org.apache.kafka.connect.runtime.distributed.NotAssignedException)2 ExecutionException (java.util.concurrent.ExecutionException)1 Response (javax.ws.rs.core.Response)1 KafkaException (org.apache.kafka.common.KafkaException)1 ConfigException (org.apache.kafka.common.config.ConfigException)1 ByteArrayDeserializer (org.apache.kafka.common.serialization.ByteArrayDeserializer)1