Commit 828f016a authored by Hiro's avatar Hiro 🏄
Browse files

Fix tests and checks

parent 36e8f916
Loading
Loading
Loading
Loading
+2 −4
Original line number Diff line number Diff line
@@ -55,8 +55,7 @@ public class Downloader {
    String message = huc.getResponseMessage();
    if (message.contains("Servers unavailable")) {
      throw new IOException("404 Servers unavailable");
    }
    else if (response == 200) {
    } else if (response == 200) {
      try (BufferedInputStream in = isDeflated
          ? new BufferedInputStream(new InflaterInputStream(
              huc.getInputStream()))
@@ -68,8 +67,7 @@ public class Downloader {
        }
      }
      return downloadedBytes.toByteArray();
    }
    else {
    } else {
      return null;
    }
  }
+11 −10
Original line number Diff line number Diff line
@@ -18,7 +18,6 @@ import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
@@ -32,6 +31,7 @@ import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

/**
 * Downloads relay descriptors from the directory authorities via HTTP.
@@ -712,8 +712,8 @@ public class RelayDescriptorDownloader {
                this.downloadResourceFromAuthority(authority,
                "/tor/status-vote/current/consensus-microdesc");
          } catch (IOException e) {
            logger.warn("Failed downloading consensus microdesc" +
                " from {}!", authority, e);
            logger.warn("Failed downloading consensus microdesc"
                + " from {}!", authority, e);
          }
        }
      }
@@ -737,8 +737,8 @@ public class RelayDescriptorDownloader {
                this.downloadResourceFromAuthority(authority,
                "/tor/status-vote/current/" + fingerprint);
          } catch (IOException e) {
            logger.warn("Failed downloading current vote" +
                " from {}!", authority, e);
            logger.warn("Failed downloading current vote"
                + " from {}!", authority, e);
          }
        }
      }
@@ -781,8 +781,8 @@ public class RelayDescriptorDownloader {
                  downloadedAllDescriptors;
            }
          } catch (IOException e) {
            logger.warn("Failed downloading all server or extra-info " +
                "descriptors files from {}!", authority, e);
            logger.warn("Failed downloading all server or extra-info "
                + "descriptors files from {}!", authority, e);
          }
        /* Download missing server descriptors, extra-info descriptors,
         * and microdescriptors if we're configured to do so. */
@@ -863,8 +863,8 @@ public class RelayDescriptorDownloader {
                break;
            }
          } catch (IOException e) {
            logger.warn("Failed downloading " +
                "descriptors files from {}!", authority, e);
            logger.warn("Failed downloading "
                + "descriptors files from {}!", authority, e);
          }
        }
      }
@@ -908,7 +908,8 @@ public class RelayDescriptorDownloader {
        for (int i = 0; i < 6; i++) {
          int wait = i * 5;
          logger.warn("Downloaded 0 bytes from {} "
              + "will wait {} SECONDS to avoid DOS protections.", authority, wait);
              + "will wait {} SECONDS to avoid DOS protections.",
              authority, wait);
          TimeUnit.SECONDS.sleep(wait);
          allData = Downloader.downloadFromHttpServer(url, isCompressed);
          logger.warn("Downloaded {} -> ({} bytes)", fullUrl,
+3 −4
Original line number Diff line number Diff line
@@ -5,7 +5,6 @@ package org.torproject.metrics.collector.downloader;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
@@ -84,9 +83,9 @@ public class DownloaderTest {
  @Test
  public void testExistingResource() throws Exception {
    URL requestedUrl = new URL("http://localhost/exists");
    byte[] expectedDownloadedBytes = "content".getBytes();
    HttpURLConnection urlConnection = mock(HttpURLConnection.class);
    httpUrlStreamHandler.addConnection(requestedUrl, urlConnection);
    byte[] expectedDownloadedBytes = "content".getBytes();
    given(urlConnection.getResponseCode()).willReturn(200);
    given(urlConnection.getResponseMessage()).willReturn("OK");
    given(urlConnection.getInputStream()).willReturn(
@@ -109,9 +108,9 @@ public class DownloaderTest {
  @Test
  public void testEmptyResource() throws Exception {
    URL requestedUrl = new URL("http://localhost/empty");
    byte[] expectedDownloadedBytes = new byte[0];
    HttpURLConnection urlConnection = mock(HttpURLConnection.class);
    httpUrlStreamHandler.addConnection(requestedUrl, urlConnection);
    byte[] expectedDownloadedBytes = new byte[0];
    given(urlConnection.getResponseCode()).willReturn(200);
    given(urlConnection.getResponseMessage()).willReturn("OK");
    given(urlConnection.getInputStream()).willReturn(
@@ -123,9 +122,9 @@ public class DownloaderTest {
  @Test(expected = SocketTimeoutException.class)
  public void testTimeout() throws Exception {
    URL requestedUrl = new URL("http://localhost/timeout");
    SocketTimeoutException expectedException = new SocketTimeoutException();
    HttpURLConnection urlConnection = mock(HttpURLConnection.class);
    httpUrlStreamHandler.addConnection(requestedUrl, urlConnection);
    SocketTimeoutException expectedException = new SocketTimeoutException();
    given(urlConnection.getResponseCode()).willReturn(200);
    given(urlConnection.getResponseMessage()).willReturn("OK");
    given(urlConnection.getInputStream()).willThrow(expectedException);