Search in sources :

Example 6 with MockTime

use of kafka.utils.MockTime in project Gaffer by gchq.

the class AddElementsFromKafkaHandlerIT method before.

@BeforeEach
public void before() throws Exception {
    bootstrapServers = "localhost:" + getOpenPort();
    // Create zookeeper server
    zkServer = new TestingServer(-1, createTemporaryDirectory("zkTmpDir"));
    zkServer.start();
    testFileSink = createTestFileSink();
    // Create kafka server
    kafkaServer = TestUtils.createServer(new KafkaConfig(serverProperties()), new MockTime());
    MapStore.resetStaticMap();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) MockTime(kafka.utils.MockTime) KafkaConfig(kafka.server.KafkaConfig) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 7 with MockTime

use of kafka.utils.MockTime in project incubator-gobblin by apache.

the class KafkaTestBase method startServer.

public static void startServer() throws RuntimeException {
    if (serverStarted && serverClosed) {
        throw new RuntimeException("Kafka test server has already been closed. Cannot generate Kafka server twice.");
    }
    if (!serverStarted) {
        serverStarted = true;
        zkConnect = TestZKUtils.zookeeperConnect();
        zkServer = new EmbeddedZookeeper(zkConnect);
        zkClient = new ZkClient(zkServer.connectString(), 30000, 30000, ZKStringSerializer$.MODULE$);
        kafkaPort = TestUtils.choosePort();
        Properties props = TestUtils.createBrokerConfig(brokerId, kafkaPort, true);
        KafkaConfig config = new KafkaConfig(props);
        Time mock = new MockTime();
        kafkaServer = TestUtils.createServer(config, mock);
    }
}
Also used : ZkClient(org.I0Itec.zkclient.ZkClient) EmbeddedZookeeper(kafka.zk.EmbeddedZookeeper) MockTime(kafka.utils.MockTime) Time(kafka.utils.Time) Properties(java.util.Properties) MockTime(kafka.utils.MockTime) KafkaConfig(kafka.server.KafkaConfig)

Example 8 with MockTime

use of kafka.utils.MockTime in project incubator-rya by apache.

the class EmbeddedKafkaInstance method startup.

/**
 * Starts the Embedded Kafka and Zookeeper Servers.
 * @throws Exception - If an exeption occurs during startup.
 */
protected void startup() throws Exception {
    // Setup the embedded zookeeper
    logger.info("Starting up Embedded Zookeeper...");
    zkServer = new EmbeddedZookeeper();
    zookeperConnect = ZKHOST + ":" + zkServer.port();
    logger.info("Embedded Zookeeper started at: {}", zookeperConnect);
    // setup Broker
    logger.info("Starting up Embedded Kafka...");
    brokerPort = Integer.toString(PortUtils.getRandomFreePort());
    final Properties brokerProps = new Properties();
    brokerProps.setProperty(KafkaConfig$.MODULE$.BrokerIdProp(), "0");
    brokerProps.setProperty(KafkaConfig$.MODULE$.HostNameProp(), BROKERHOST);
    brokerProps.setProperty(KafkaConfig$.MODULE$.PortProp(), brokerPort);
    brokerProps.setProperty(KafkaConfig$.MODULE$.ZkConnectProp(), zookeperConnect);
    brokerProps.setProperty(KafkaConfig$.MODULE$.LogDirsProp(), Files.createTempDirectory(getClass().getSimpleName() + "-").toAbsolutePath().toString());
    brokerProps.setProperty(KafkaConfig$.MODULE$.DeleteTopicEnableProp(), "true");
    final KafkaConfig config = new KafkaConfig(brokerProps);
    final Time mock = new MockTime();
    kafkaServer = TestUtils.createServer(config, mock);
    logger.info("Embedded Kafka Server started at: {}:{}", BROKERHOST, brokerPort);
}
Also used : EmbeddedZookeeper(kafka.zk.EmbeddedZookeeper) MockTime(kafka.utils.MockTime) Time(kafka.utils.Time) Properties(java.util.Properties) MockTime(kafka.utils.MockTime) KafkaConfig(kafka.server.KafkaConfig)

Example 9 with MockTime

use of kafka.utils.MockTime in project cruise-control by linkedin.

the class UserTaskManagerTest method testMaximumActiveTasks.

@Test
public void testMaximumActiveTasks() {
    HttpSession mockHttpSession1 = EasyMock.mock(HttpSession.class);
    EasyMock.expect(mockHttpSession1.getLastAccessedTime()).andReturn(100L).anyTimes();
    HttpServletRequest mockHttpServletRequest1 = prepareRequest(mockHttpSession1, null);
    OperationFuture future = new OperationFuture("future");
    UserTaskManager userTaskManager = new UserTaskManager(1000, 1, TimeUnit.HOURS.toMillis(6), 100, new MockTime());
    HttpServletResponse mockHttpServletResponse = EasyMock.mock(HttpServletResponse.class);
    mockHttpServletResponse.setHeader(EasyMock.anyString(), EasyMock.anyString());
    EasyMock.replay(mockHttpSession1, mockHttpServletResponse);
    // test-case: test max limitation active tasks
    OperationFuture future1 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
    Assert.assertEquals(future, future1);
    EasyMock.verify(mockHttpSession1, mockHttpServletRequest1, mockHttpServletResponse);
    HttpSession mockHttpSession2 = EasyMock.mock(HttpSession.class);
    EasyMock.expect(mockHttpSession2.getLastAccessedTime()).andReturn(100L).anyTimes();
    EasyMock.replay(mockHttpSession2);
    EasyMock.reset(mockHttpServletResponse);
    HttpServletRequest mockHttpServletRequest2 = prepareRequest(mockHttpSession2, null, "/test2", Collections.emptyMap());
    try {
        OperationFuture future2 = userTaskManager.getOrCreateUserTask(mockHttpServletRequest2, mockHttpServletResponse, uuid -> future, 0, true, null).get(0);
        Assert.assertEquals(future, future2);
        EasyMock.verify(mockHttpSession2, mockHttpServletRequest2, mockHttpServletResponse);
    } catch (RuntimeException e) {
        userTaskManager.close();
        return;
    }
    Assert.fail("Don't expect to be here!");
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpSession(javax.servlet.http.HttpSession) Capture(org.easymock.Capture) Time(org.apache.kafka.common.utils.Time) HttpServletResponse(javax.servlet.http.HttpServletResponse) HashMap(java.util.HashMap) Test(org.junit.Test) UUID(java.util.UUID) EasyMock(org.easymock.EasyMock) TimeUnit(java.util.concurrent.TimeUnit) MockTime(kafka.utils.MockTime) HttpServletRequest(javax.servlet.http.HttpServletRequest) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) Map(java.util.Map) GET_METHOD(com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD) Assert(org.junit.Assert) Collections(java.util.Collections) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) MockTime(kafka.utils.MockTime) Test(org.junit.Test)

Example 10 with MockTime

use of kafka.utils.MockTime in project cruise-control by linkedin.

the class UserTaskManagerTest method testSessionsShareUserTask.

@Test
public void testSessionsShareUserTask() {
    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);
    EasyMock.expect(mockHttpSession.getLastAccessedTime()).andReturn(100L).anyTimes();
    Map<String, String[]> requestParams1 = new HashMap<>();
    requestParams1.put("param", new String[] { "true" });
    HttpServletRequest mockHttpServletRequest1 = prepareRequest(mockHttpSession, null, "test", requestParams1);
    HttpServletResponse mockHttpServletResponse1 = EasyMock.mock(HttpServletResponse.class);
    Capture<String> userTaskHeader = Capture.newInstance();
    Capture<String> userTaskHeaderValue = Capture.newInstance();
    mockHttpServletResponse1.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
    Map<String, String[]> requestParams2 = new HashMap<>();
    requestParams2.put("param", new String[] { "true" });
    HttpServletRequest mockHttpServletRequest2 = prepareRequest(mockHttpSession, null, "test", requestParams2);
    HttpServletResponse mockHttpServletResponse2 = EasyMock.mock(HttpServletResponse.class);
    mockHttpServletResponse2.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
    Map<String, String[]> requestParams3 = new HashMap<>();
    requestParams3.put("param", new String[] { "true" });
    HttpServletRequest mockHttpServletRequest3 = prepareRequest(mockHttpSession, testUserTaskId.toString(), "test", requestParams3);
    HttpServletResponse mockHttpServletResponse3 = EasyMock.mock(HttpServletResponse.class);
    mockHttpServletResponse3.setHeader(EasyMock.capture(userTaskHeader), EasyMock.capture(userTaskHeaderValue));
    EasyMock.replay(mockUuidGenerator, mockHttpSession, mockHttpServletResponse1, mockHttpServletResponse2, mockHttpServletResponse3);
    OperationFuture future = new OperationFuture("future");
    UserTaskManager userTaskManager = new UserTaskManager(1000, 5, TimeUnit.HOURS.toMillis(6), 100, new MockTime(), mockUuidGenerator);
    userTaskManager.getOrCreateUserTask(mockHttpServletRequest1, mockHttpServletResponse1, uuid -> future, 0, true, null);
    userTaskManager.getOrCreateUserTask(mockHttpServletRequest2, mockHttpServletResponse2, uuid -> future, 0, true, null);
    // Test UserTaskManger can recognize the previous created task by taskId.
    userTaskManager.getOrCreateUserTask(mockHttpServletRequest3, mockHttpServletResponse3, uuid -> future, 0, true, null);
    // The 2nd request should reuse the UserTask created for the 1st request since they use the same session and send the same request.
    Assert.assertEquals(1, userTaskManager.numActiveSessionKeys());
    EasyMock.verify(mockUuidGenerator, mockHttpServletResponse1, mockHttpServletResponse2, mockHttpServletResponse3, mockHttpServletRequest1, mockHttpServletRequest2, mockHttpServletRequest3);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) HttpServletResponse(javax.servlet.http.HttpServletResponse) UUID(java.util.UUID) OperationFuture(com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture) MockTime(kafka.utils.MockTime) Test(org.junit.Test)

Aggregations

MockTime (kafka.utils.MockTime)25 Properties (java.util.Properties)16 KafkaConfig (kafka.server.KafkaConfig)14 TimeUnit (java.util.concurrent.TimeUnit)9 EmbeddedZookeeper (kafka.zk.EmbeddedZookeeper)9 ZkClient (org.I0Itec.zkclient.ZkClient)9 Test (org.junit.Test)9 Collections (java.util.Collections)8 Time (kafka.utils.Time)8 HashMap (java.util.HashMap)7 Map (java.util.Map)7 OperationFuture (com.linkedin.kafka.cruisecontrol.servlet.handler.async.runnable.OperationFuture)6 UUID (java.util.UUID)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 HttpSession (javax.servlet.http.HttpSession)6 Time (org.apache.kafka.common.utils.Time)6 Before (org.junit.Before)6 GET_METHOD (com.linkedin.kafka.cruisecontrol.servlet.KafkaCruiseControlServletUtils.GET_METHOD)5 IOException (java.io.IOException)5