@Override public void close() throws Exception { Zookeeper.shutdownInprocessZookeeper(zookeeper); zkTmp.close(); }
public TmpPath() { this(localTempPath()); }
public void setUp() throws Exception { // setup ZK zookeeper = new TestingServer(true); // setup Broker kafkaDir = new TmpPath(Files.createTempDirectory("kafka-").toAbsolutePath().toString()); Properties brokerProps = new Properties(); brokerProps.setProperty("zookeeper.connect", zookeeper.getConnectString()); brokerProps.setProperty("broker.id", "0"); brokerProps.setProperty("log.dirs", kafkaDir.getPath()); brokerProps.setProperty("listeners", String.format("PLAINTEXT://%s:%d", KAFKA_HOST, KAFKA_PORT)); brokerProps.setProperty("offsets.topic.replication.factor", "1"); KafkaConfig config = new KafkaConfig(brokerProps); MockTime mock = new MockTime(); kafkaServer = TestUtils.createServer(config, mock); // setup default Producer createProducer(); kafkaAdminClient = AdminClient.create(Collections.singletonMap(AdminClientConfig.BOOTSTRAP_SERVERS_CONFIG, KAFKA_HOST + ":" + KAFKA_PORT)); }
public void tearDown() throws Exception { kafkaAdminClient.close(); closeProducer(); kafkaServer.shutdown(); kafkaDir.close(); zookeeper.close(); }
@Test public void testEmptyState() throws IOException { TmpPath tmp_dir = new TmpPath(); String dir = tmp_dir.getPath(); LocalState ls = new LocalState(dir, true); GlobalStreamId gs_a = new GlobalStreamId("a", "a"); FileOutputStream data = FileUtils.openOutputStream(new File(dir, "12345")); FileOutputStream version = FileUtils.openOutputStream(new File(dir, "12345.version")); Assert.assertNull(ls.get("c")); ls.put("a", gs_a); Assert.assertEquals(gs_a, ls.get("a")); } }
@Test public void testLocalState() throws Exception { try (TmpPath dir1_tmp = new TmpPath(); TmpPath dir2_tmp = new TmpPath()) { GlobalStreamId globalStreamId_a = new GlobalStreamId("a", "a"); GlobalStreamId globalStreamId_b = new GlobalStreamId("b", "b"); GlobalStreamId globalStreamId_c = new GlobalStreamId("c", "c"); GlobalStreamId globalStreamId_d = new GlobalStreamId("d", "d"); LocalState ls1 = new LocalState(dir1_tmp.getPath(), true); LocalState ls2 = new LocalState(dir2_tmp.getPath(), true); Assert.assertTrue(ls1.snapshot().isEmpty()); ls1.put("a", globalStreamId_a); ls1.put("b", globalStreamId_b); Map<String, GlobalStreamId> expected = new HashMap<>(); expected.put("a", globalStreamId_a); expected.put("b", globalStreamId_b); Assert.assertEquals(expected, ls1.snapshot()); Assert.assertEquals(expected, new LocalState(dir1_tmp.getPath(), true).snapshot()); Assert.assertTrue(ls2.snapshot().isEmpty()); ls2.put("b", globalStreamId_a); ls2.put("b", globalStreamId_b); ls2.put("b", globalStreamId_c); ls2.put("b", globalStreamId_d); Assert.assertEquals(globalStreamId_d, ls2.get("b")); } }