use of kafka.utils.MockTime in project hazelcast-jet by hazelcast.
the class KafkaTestSupport method createKafkaCluster.
public final String createKafkaCluster() throws IOException {
System.setProperty("zookeeper.preAllocSize", Integer.toString(128));
zkServer = new EmbeddedZookeeper();
String zkConnect = ZK_HOST + ':' + zkServer.port();
ZkClient zkClient = new ZkClient(zkConnect, SESSION_TIMEOUT, CONNECTION_TIMEOUT, ZKStringSerializer$.MODULE$);
zkUtils = ZkUtils.apply(zkClient, false);
brokerPort = getRandomPort();
Properties brokerProps = new Properties();
brokerProps.setProperty("zookeeper.connect", zkConnect);
brokerProps.setProperty("broker.id", "0");
brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKER_HOST + ':' + brokerPort);
brokerProps.setProperty("offsets.topic.replication.factor", "1");
brokerProps.setProperty("offsets.topic.num.partitions", "1");
// we need this due to avoid OOME while running tests, see https://issues.apache.org/jira/browse/KAFKA-3872
brokerProps.setProperty("log.cleaner.dedupe.buffer.size", Long.toString(2 * 1024 * 1024L));
KafkaConfig config = new KafkaConfig(brokerProps);
Time mock = new MockTime();
kafkaServer = TestUtils.createServer(config, mock);
return BROKER_HOST + ':' + brokerPort;
}
use of kafka.utils.MockTime in project kafkastreams-cep by fhussonnois.
the class CEPStockKStreamsIntegrationTest method test.
@Test
public void test() throws ExecutionException, InterruptedException {
final Collection<KeyValue<String, String>> batch1 = Arrays.asList(new KeyValue<>(null, "{\"name\":\"e1\",\"price\":100,\"volume\":1010}"), new KeyValue<>(null, "{\"name\":\"e2\",\"price\":120,\"volume\":990}"), new KeyValue<>(null, "{\"name\":\"e3\",\"price\":120,\"volume\":1005}"), new KeyValue<>(null, "{\"name\":\"e4\",\"price\":121,\"volume\":999}"), new KeyValue<>(null, "{\"name\":\"e5\",\"price\":120,\"volume\":999}"), new KeyValue<>(null, "{\"name\":\"e6\",\"price\":125,\"volume\":750}"), new KeyValue<>(null, "{\"name\":\"e7\",\"price\":120,\"volume\":950}"), new KeyValue<>(null, "{\"name\":\"e8\",\"price\":120,\"volume\":700}"));
IntegrationTestUtils.produceKeyValuesSynchronously(INPUT_STREAM, batch1, TestUtils.producerConfig(CLUSTER.bootstrapServers(), StringSerializer.class, StringSerializer.class, new Properties()), mockTime);
// build query
ComplexStreamsBuilder builder = new ComplexStreamsBuilder();
CEPStream<String, StockEvent> stream = builder.stream(INPUT_STREAM);
KStream<String, Sequence<String, StockEvent>> stocks = stream.query("Stocks", Patterns.STOCKS);
stocks.mapValues(seq -> {
JSONObject json = new JSONObject();
seq.asMap().forEach((k, v) -> {
JSONArray events = new JSONArray();
json.put(k, events);
List<String> collect = v.stream().map(e -> e.value.name).collect(Collectors.toList());
Collections.reverse(collect);
collect.forEach(events::add);
});
return json.toJSONString();
}).through(OUTPUT_STREAM, Produced.with(null, Serdes.String())).print(Printed.toSysOut());
Topology topology = builder.build();
kafkaStreams = new KafkaStreams(topology, streamsConfiguration);
kafkaStreams.start();
final Properties consumerConfig = TestUtils.consumerConfig(CLUSTER.bootstrapServers(), StringDeserializer.class, StringDeserializer.class);
List<KeyValue<String, String>> result = IntegrationTestUtils.readKeyValues(OUTPUT_STREAM, consumerConfig, TimeUnit.SECONDS.toMillis(10), 4);
Assert.assertEquals(4, result.size());
Assert.assertEquals("{\"0\":[\"e1\"],\"1\":[\"e2\",\"e3\",\"e4\",\"e5\"],\"2\":[\"e6\"]}", result.get(0).value);
Assert.assertEquals("{\"0\":[\"e3\"],\"1\":[\"e4\"],\"2\":[\"e6\"]}", result.get(1).value);
Assert.assertEquals("{\"0\":[\"e1\"],\"1\":[\"e2\",\"e3\",\"e4\",\"e5\",\"e6\",\"e7\"],\"2\":[\"e8\"]}", result.get(2).value);
Assert.assertEquals("{\"0\":[\"e3\"],\"1\":[\"e4\",\"e6\"],\"2\":[\"e8\"]}", result.get(3).value);
}
use of kafka.utils.MockTime in project incubator-rya by apache.
the class KafkaExportITBase method setupKafka.
/**
* setup mini kafka and call the super to setup mini fluo
*/
@Before
public void setupKafka() throws Exception {
// Install an instance of Rya on the Accumulo cluster.
installRyaInstance();
// Setup Kafka.
zkServer = new EmbeddedZookeeper();
final String zkConnect = ZKHOST + ":" + zkServer.port();
zkClient = new ZkClient(zkConnect, 30000, 30000, ZKStringSerializer$.MODULE$);
zkUtils = ZkUtils.apply(zkClient, false);
// setup Broker
final Properties brokerProps = new Properties();
brokerProps.setProperty("zookeeper.connect", zkConnect);
brokerProps.setProperty("broker.id", "0");
brokerProps.setProperty("log.dirs", Files.createTempDirectory("kafka-").toAbsolutePath().toString());
brokerProps.setProperty("listeners", "PLAINTEXT://" + BROKERHOST + ":" + BROKERPORT);
final KafkaConfig config = new KafkaConfig(brokerProps);
final Time mock = new MockTime();
kafkaServer = TestUtils.createServer(config, mock);
}
use of kafka.utils.MockTime in project cruise-control by linkedin.
the class UserTaskManagerTest method testCompletedTasks.
@Test
public void testCompletedTasks() throws Exception {
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
mockHttpSession.invalidate();
HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(UUID.randomUUID()).anyTimes();
OperationFuture future = new OperationFuture("future");
UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
Capture<String> userTaskHeader = Capture.newInstance();
Capture<String> userTaskHeaderValue = Capture.newInstance();
mockHttpServletResponse.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
// test-case: verify if the background cleaner task removes tasks that are completed
OperationFuture future1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
Assert.assertEquals(future, future1);
future1.cancel(true);
Thread.sleep(TimeUnit.SECONDS.toMillis(UserTaskManager.USER_TASK_SCANNER_PERIOD_SECONDS * 4));
Assert.assertTrue(future.isDone());
Assert.assertTrue(future.isCancelled());
EasyMock.verify(mockHttpSession, mockUuidGenerator, mockHttpServletResponse);
userTaskManager.close();
}
use of kafka.utils.MockTime in project cruise-control by linkedin.
the class UserTaskManagerTest method testAddStepsFutures.
@Test
public void testAddStepsFutures() {
UUID testUserTaskId = UUID.randomUUID();
UserTaskManager.UuidGenerator mockUuidGenerator = EasyMock.mock(UserTaskManager.UuidGenerator.class);
EasyMock.expect(mockUuidGenerator.randomUUID()).andReturn(testUserTaskId).anyTimes();
HttpSession mockHttpSession = EasyMock.mock(HttpSession.class);
// Change mock session's last access time to always return current time to avoid unintended recycling of session.
EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(System.currentTimeMillis()).anyTimes();
HttpServletRequest mockHttpServletRequest = prepareRequest(mockHttpSession, null);
HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
OperationFuture testFuture1 = new OperationFuture("testFuture1");
OperationFuture testFuture2 = new OperationFuture("testFuture2");
OperationFuture insertedFuture1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> testFuture1, 0, true, null).get(0);
Assert.assertEquals(testFuture1, insertedFuture1);
EasyMock.verify(mockUuidGenerator, mockHttpSession, mockHttpServletResponse);
EasyMock.reset(mockHttpServletResponse);
OperationFuture insertedFuture2 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest, mockHttpServletResponse, uuid -> testFuture2, 1, true, null).get(1);
Assert.assertEquals(testFuture2, insertedFuture2);
Assert.assertEquals(userTaskManager.getUserTaskByUserTaskId(testUserTaskId, mockHttpServletRequest).futures().size(), 2);
userTaskManager.close();
}
Aggregations