Java tutorial
/** * Copyright (C) 2015 Greg Brandt (brandt.greg@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.github.brandtg.switchboard; import com.fasterxml.jackson.databind.ObjectMapper; import com.github.brandtg.switchboard.util.DropWizardApplicationRunner; import org.apache.commons.io.FileUtils; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClients; import org.apache.http.util.EntityUtils; import org.testng.annotations.AfterClass; import org.testng.annotations.BeforeClass; import org.testng.annotations.Test; import java.io.File; import java.net.URLEncoder; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class TestFileLogServer { private HttpClient httpClient; private long pollMillis; private long timeoutMillis; private File rootDir; private File testFile; private DropWizardApplicationRunner.DropWizardServer server; private HttpHost serverAddress; @BeforeClass public void beforeClass() throws Exception { httpClient = HttpClients.createDefault(); pollMillis = 1000; timeoutMillis = 30000; rootDir = new File(System.getProperty("java.io.tmpdir"), TestFileLogServer.class.getSimpleName()); testFile = new File(rootDir, "" + System.currentTimeMillis()); serverAddress = new HttpHost("localhost", 8080); if (rootDir.exists()) { FileUtils.forceDelete(rootDir); } FileLogServerConfig config = new FileLogServerConfig(); config.setRootDir(rootDir.getAbsolutePath()); server = DropWizardApplicationRunner.createServer(config, FileLogServer.class); server.start(); } @AfterClass public void afterClass() throws Exception { server.stop(); FileUtils.forceDelete(rootDir); } @Test public void testCreateAndUpdate() throws Exception { String logName = URLEncoder.encode(testFile.getAbsolutePath(), "UTF-8"); FileUtils.write(testFile, "Hello"); pollAndCheck(serverAddress, String.format("/log/%s/0", logName), 1, 1); FileUtils.write(testFile, "World"); pollAndCheck(serverAddress, String.format("/log/%s/0", logName), 2, 2); } private void pollAndCheck(HttpHost host, String uri, long count, long highWaterMark) throws Exception { long startTime = System.currentTimeMillis(); long currentTime; do { // Query server HttpGet req = new HttpGet(uri); HttpResponse res = httpClient.execute(host, req); try { if (res.getStatusLine().getStatusCode() == 200) { ObjectMapper mapper = new ObjectMapper(); LogRegionResponse data = mapper.readValue(res.getEntity().getContent(), LogRegionResponse.class); // Get all sorted indexes present in response List<Long> indexes = new ArrayList<>(); for (LogRegion logRegion : data.getLogRegions()) { indexes.add(logRegion.getIndex()); } Collections.sort(indexes); // Check that we've have expected count and reached high watermark if (indexes.size() >= count && indexes.get(indexes.size() - 1) >= highWaterMark) { return; } } } finally { if (res != null) { EntityUtils.consume(res.getEntity()); } } // Wait until next time Thread.sleep(pollMillis); currentTime = System.currentTimeMillis(); } while (currentTime - startTime < timeoutMillis); // Exited, so timed out throw new IllegalStateException("Timed out while waiting for " + uri); } }