use of io.debezium.util.Testing in project debezium by debezium.
the class AbstractConnectorTest method start.
/**
* Start the connector using the supplied connector configuration.
*
* @param connectorClass the connector class; may not be null
* @param connectorConfig the configuration for the connector; may not be null
* @param isStopRecord the function that will be called to determine if the connector should be stopped before processing
* this record; may be null if not needed
* @param callback the function that will be called when the engine fails to start the connector or when the connector
* stops running after completing successfully or due to an error; may be null
*/
protected void start(Class<? extends SourceConnector> connectorClass, Configuration connectorConfig, CompletionCallback callback, Predicate<SourceRecord> isStopRecord) {
Configuration config = Configuration.copy(connectorConfig).with(EmbeddedEngine.ENGINE_NAME, "testing-connector").with(EmbeddedEngine.CONNECTOR_CLASS, connectorClass.getName()).with(StandaloneConfig.OFFSET_STORAGE_FILE_FILENAME_CONFIG, OFFSET_STORE_PATH).with(EmbeddedEngine.OFFSET_FLUSH_INTERVAL_MS, 0).build();
latch = new CountDownLatch(1);
CompletionCallback wrapperCallback = (success, msg, error) -> {
try {
if (callback != null)
callback.handle(success, msg, error);
} finally {
if (!success) {
// we only unblock if there was an error; in all other cases we're unblocking when a task has been started
latch.countDown();
}
}
Testing.debug("Stopped connector");
};
ConnectorCallback connectorCallback = new ConnectorCallback() {
@Override
public void taskStarted() {
// if this is called, it means a task has been started successfully so we can continue
latch.countDown();
}
};
// Create the connector ...
engine = EmbeddedEngine.create().using(config).notifying((record) -> {
if (isStopRecord != null && isStopRecord.test(record)) {
logger.error("Stopping connector after record as requested");
throw new ConnectException("Stopping connector after record as requested");
}
try {
consumedLines.put(record);
} catch (InterruptedException e) {
Thread.interrupted();
}
}).using(this.getClass().getClassLoader()).using(wrapperCallback).using(connectorCallback).build();
// Submit the connector for asynchronous execution ...
assertThat(executor).isNull();
executor = Executors.newFixedThreadPool(1);
executor.execute(() -> {
LoggingContext.forConnector(getClass().getSimpleName(), "", "engine");
engine.run();
});
try {
if (!latch.await(10, TimeUnit.SECONDS)) {
// maybe it takes more time to start up, so just log a warning and continue
logger.warn("The connector did not finish starting its task(s) or complete in the expected amount of time");
}
} catch (InterruptedException e) {
if (Thread.interrupted()) {
fail("Interrupted while waiting for engine startup");
}
}
}
Aggregations