use of org.apache.kafka.common.utils.Time in project cruise-control by linkedin.
the class SessionManagerTest method prepareRequests.
private TestContext prepareRequests(boolean expectSessionInvalidation, int numRequests) {
Time time = new MockTime();
List<HttpServletRequest> requests = new ArrayList<>(numRequests);
List<HttpSession> sessions = new ArrayList<>();
for (int i = 0; i < numRequests; i++) {
HttpServletRequest request = EasyMock.mock(HttpServletRequest.class);
HttpSession session = EasyMock.mock(HttpSession.class);
requests.add(request);
sessions.add(session);
EasyMock.expect(request.getSession()).andReturn(session).anyTimes();
EasyMock.expect(request.getSession(false)).andReturn(session).anyTimes();
EasyMock.expect(request.getMethod()).andReturn("GET").anyTimes();
EasyMock.expect(request.getRequestURI()).andReturn("/test").anyTimes();
EasyMock.expect(request.getParameterMap()).andReturn(Collections.emptyMap()).anyTimes();
EasyMock.expect(session.getLastAccessedTime()).andReturn(time.milliseconds()).anyTimes();
if (expectSessionInvalidation) {
session.invalidate();
EasyMock.expectLastCall().once();
}
}
EasyMock.replay(requests.toArray());
EasyMock.replay(sessions.toArray());
return new TestContext(requests, time);
}
use of org.apache.kafka.common.utils.Time in project cruise-control by linkedin.
the class BrokerFailureDetectorTest method testLoadFailedBrokersFromZK.
@Test
public void testLoadFailedBrokersFromZK() throws Exception {
Time mockTime = getMockTime();
Queue<Anomaly> anomalies = new ConcurrentLinkedQueue<>();
BrokerFailureDetector detector = createBrokerFailureDetector(anomalies, mockTime);
try {
detector.startDetection();
int brokerId = 0;
killBroker(brokerId);
long start = System.currentTimeMillis();
while (anomalies.isEmpty() && System.currentTimeMillis() < start + 30000) {
// Wait for the anomalies to be drained.
}
assertEquals(Collections.singletonMap(brokerId, 100L), detector.failedBrokers());
// shutdown, advance the clock and create a new detector.
detector.shutdown();
mockTime.sleep(100L);
detector = createBrokerFailureDetector(anomalies, mockTime);
// start the newly created detector and the broker down time should remain previous time.
detector.startDetection();
assertEquals(Collections.singletonMap(brokerId, 100L), detector.failedBrokers());
} finally {
detector.shutdown();
}
}
use of org.apache.kafka.common.utils.Time in project kafka-connect-cdc-mssql by jcustenborder.
the class QueryServiceIT method queryTable.
private void queryTable(ChangeKey input) throws SQLException, IOException {
List<Change> expectedChanges;
String fileName = String.format("%s.%s.json", input.schemaName, input.tableName);
String resourceName = String.format("query/table/%s/%s", input.databaseName, fileName);
long timestamp = 0L;
try (InputStream stream = this.getClass().getResourceAsStream(resourceName)) {
Preconditions.checkNotNull(stream, "Could not find resource %s.", resourceName);
log.info("Loading expected changes from {}", resourceName);
expectedChanges = loadChanges(stream);
for (Change change : expectedChanges) {
timestamp = change.timestamp();
break;
}
}
OffsetStorageReader offsetStorageReader = mock(OffsetStorageReader.class);
TableMetadataProvider tableMetadataProvider = new MsSqlTableMetadataProvider(config, offsetStorageReader);
Time time = mock(Time.class);
ChangeWriter changeWriter = mock(ChangeWriter.class);
List<Change> actualChanges = new ArrayList<>(1000);
doAnswer(invocationOnMock -> {
Change change = invocationOnMock.getArgument(0);
actualChanges.add(change);
return null;
}).when(changeWriter).addChange(any());
QueryService queryService = new QueryService(time, tableMetadataProvider, config, changeWriter);
when(time.milliseconds()).thenReturn(timestamp);
queryService.queryTable(changeWriter, input);
verify(offsetStorageReader, only()).offset(anyMap());
verify(time, atLeastOnce()).milliseconds();
if (log.isDebugEnabled()) {
log.trace("Found {} change(s).", actualChanges.size());
}
assertFalse(actualChanges.isEmpty(), "Changes should have been returned.");
assertEquals(expectedChanges.size(), actualChanges.size(), "The number of actualChanges returned is not the expect count.");
for (int i = 0; i < expectedChanges.size(); i++) {
Change expectedChange = expectedChanges.get(i);
Change actualChange = actualChanges.get(i);
assertChange(expectedChange, actualChange);
}
}
use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaProducerTest method testInitTransactionTimeout.
@Test(expected = TimeoutException.class)
public void testInitTransactionTimeout() {
Properties props = new Properties();
props.put(ProducerConfig.TRANSACTIONAL_ID_CONFIG, "bad-transaction");
props.put(ProducerConfig.MAX_BLOCK_MS_CONFIG, 5);
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9000");
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster("topic", 1);
Node node = cluster.nodes().get(0);
Metadata metadata = new Metadata(0, Long.MAX_VALUE, true);
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
Producer<String, String> producer = new KafkaProducer<>(new ProducerConfig(ProducerConfig.addSerializerToConfig(props, new StringSerializer(), new StringSerializer())), new StringSerializer(), new StringSerializer(), metadata, client);
try {
producer.initTransactions();
fail("initTransactions() should have raised TimeoutException");
} finally {
producer.close(0, TimeUnit.MILLISECONDS);
}
}
use of org.apache.kafka.common.utils.Time in project apache-kafka-on-k8s by banzaicloud.
the class KafkaProducerTest method shouldCloseProperlyAndThrowIfInterrupted.
@Test
public void shouldCloseProperlyAndThrowIfInterrupted() throws Exception {
Properties props = new Properties();
props.setProperty(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9999");
props.setProperty(ProducerConfig.PARTITIONER_CLASS_CONFIG, MockPartitioner.class.getName());
props.setProperty(ProducerConfig.BATCH_SIZE_CONFIG, "1");
Time time = new MockTime();
Cluster cluster = TestUtils.singletonCluster("topic", 1);
Node node = cluster.nodes().get(0);
Metadata metadata = new Metadata(0, Long.MAX_VALUE, true);
metadata.update(cluster, Collections.<String>emptySet(), time.milliseconds());
MockClient client = new MockClient(time, metadata);
client.setNode(node);
final Producer<String, String> producer = new KafkaProducer<>(new ProducerConfig(ProducerConfig.addSerializerToConfig(props, new StringSerializer(), new StringSerializer())), new StringSerializer(), new StringSerializer(), metadata, client);
ExecutorService executor = Executors.newSingleThreadExecutor();
final AtomicReference<Exception> closeException = new AtomicReference<>();
try {
Future<?> future = executor.submit(new Runnable() {
@Override
public void run() {
producer.send(new ProducerRecord<>("topic", "key", "value"));
try {
producer.close();
fail("Close should block and throw.");
} catch (Exception e) {
closeException.set(e);
}
}
});
// Close producer should not complete until send succeeds
try {
future.get(100, TimeUnit.MILLISECONDS);
fail("Close completed without waiting for send");
} catch (java.util.concurrent.TimeoutException expected) {
/* ignore */
}
// Ensure send has started
client.waitForRequests(1, 1000);
assertTrue("Close terminated prematurely", future.cancel(true));
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return closeException.get() != null;
}
}, "InterruptException did not occur within timeout.");
assertTrue("Expected exception not thrown " + closeException, closeException.get() instanceof InterruptException);
} finally {
executor.shutdownNow();
}
}
Aggregations