Return-Path: X-Original-To: apmail-hadoop-hdfs-commits-archive@minotaur.apache.org Delivered-To: apmail-hadoop-hdfs-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 1F40A114EE for ; Tue, 15 Apr 2014 18:09:30 +0000 (UTC) Received: (qmail 77952 invoked by uid 500); 15 Apr 2014 18:09:28 -0000 Delivered-To: apmail-hadoop-hdfs-commits-archive@hadoop.apache.org Received: (qmail 77850 invoked by uid 500); 15 Apr 2014 18:09:27 -0000 Mailing-List: contact hdfs-commits-help@hadoop.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: hdfs-dev@hadoop.apache.org Delivered-To: mailing list hdfs-commits@hadoop.apache.org Received: (qmail 77839 invoked by uid 99); 15 Apr 2014 18:09:26 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Apr 2014 18:09:26 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 15 Apr 2014 18:09:25 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 22C302388993; Tue, 15 Apr 2014 18:09:05 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1587660 - in /hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs: CHANGES.txt src/test/java/org/apache/hadoop/hdfs/web/TestByteRangeInputStream.java Date: Tue, 15 Apr 2014 18:09:04 -0000 To: hdfs-commits@hadoop.apache.org From: wheat9@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20140415180905.22C302388993@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: wheat9 Date: Tue Apr 15 18:09:04 2014 New Revision: 1587660 URL: http://svn.apache.org/r1587660 Log: HDFS-6194. Create new tests for ByteRangeInputStream. Contributed by Akira Ajisaka. Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestByteRangeInputStream.java Modified: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt Modified: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt?rev=1587660&r1=1587659&r2=1587660&view=diff ============================================================================== --- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt (original) +++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/CHANGES.txt Tue Apr 15 18:09:04 2014 @@ -296,6 +296,9 @@ Release 2.5.0 - UNRELEASED HDFS-6224. Add a unit test to TestAuditLogger for file permissions passed to logAuditEvent. (Charles Lamb via wang) + HDFS-6194. Create new tests for ByteRangeInputStream. + (Akira Ajisaka via wheat9) + OPTIMIZATIONS BUG FIXES Added: hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestByteRangeInputStream.java URL: http://svn.apache.org/viewvc/hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestByteRangeInputStream.java?rev=1587660&view=auto ============================================================================== --- hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestByteRangeInputStream.java (added) +++ hadoop/common/trunk/hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/web/TestByteRangeInputStream.java Tue Apr 15 18:09:04 2014 @@ -0,0 +1,210 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.hadoop.hdfs.web; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; +import static org.mockito.Matchers.anyBoolean; +import static org.mockito.Matchers.anyLong; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.net.HttpURLConnection; +import java.net.URL; + +import com.google.common.net.HttpHeaders; +import org.junit.Test; +import org.mockito.internal.util.reflection.Whitebox; + +public class TestByteRangeInputStream { + private class ByteRangeInputStreamImpl extends ByteRangeInputStream { + public ByteRangeInputStreamImpl(URLOpener o, URLOpener r) + throws IOException { + super(o, r); + } + + @Override + protected URL getResolvedUrl(HttpURLConnection connection) + throws IOException { + return new URL("http://resolvedurl/"); + } + } + + private ByteRangeInputStream.URLOpener getMockURLOpener(URL url) + throws IOException { + ByteRangeInputStream.URLOpener opener = + mock(ByteRangeInputStream.URLOpener.class, CALLS_REAL_METHODS); + opener.setURL(url); + doReturn(getMockConnection("65535")) + .when(opener).connect(anyLong(), anyBoolean()); + return opener; + } + + private HttpURLConnection getMockConnection(String length) + throws IOException { + HttpURLConnection mockConnection = mock(HttpURLConnection.class); + doReturn(new ByteArrayInputStream("asdf".getBytes())) + .when(mockConnection).getInputStream(); + doReturn(length).when(mockConnection) + .getHeaderField(HttpHeaders.CONTENT_LENGTH); + return mockConnection; + } + + @Test + public void testByteRange() throws IOException { + ByteRangeInputStream.URLOpener oMock = getMockURLOpener( + new URL("http://test")); + ByteRangeInputStream.URLOpener rMock = getMockURLOpener(null); + ByteRangeInputStream bris = new ByteRangeInputStreamImpl(oMock, rMock); + + bris.seek(0); + + assertEquals("getPos wrong", 0, bris.getPos()); + + bris.read(); + + assertEquals("Initial call made incorrectly (offset check)", + 0, bris.startPos); + assertEquals("getPos should return 1 after reading one byte", 1, + bris.getPos()); + verify(oMock, times(1)).connect(0, false); + + bris.read(); + + assertEquals("getPos should return 2 after reading two bytes", 2, + bris.getPos()); + // No additional connections should have been made (no seek) + verify(oMock, times(1)).connect(0, false); + + rMock.setURL(new URL("http://resolvedurl/")); + + bris.seek(100); + bris.read(); + + assertEquals("Seek to 100 bytes made incorrectly (offset Check)", + 100, bris.startPos); + assertEquals("getPos should return 101 after reading one byte", 101, + bris.getPos()); + verify(rMock, times(1)).connect(100, true); + + bris.seek(101); + bris.read(); + + // Seek to 101 should not result in another request + verify(rMock, times(1)).connect(100, true); + verify(rMock, times(0)).connect(101, true); + + bris.seek(2500); + bris.read(); + + assertEquals("Seek to 2500 bytes made incorrectly (offset Check)", + 2500, bris.startPos); + + doReturn(getMockConnection(null)) + .when(rMock).connect(anyLong(), anyBoolean()); + bris.seek(500); + try { + bris.read(); + fail("Exception should be thrown when content-length is not given"); + } catch (IOException e) { + assertTrue("Incorrect response message: " + e.getMessage(), + e.getMessage().startsWith(HttpHeaders.CONTENT_LENGTH + + " is missing: ")); + } + bris.close(); + } + + @Test + public void testPropagatedClose() throws IOException { + ByteRangeInputStream bris = + mock(ByteRangeInputStream.class, CALLS_REAL_METHODS); + InputStream mockStream = mock(InputStream.class); + doReturn(mockStream).when(bris).openInputStream(); + Whitebox.setInternalState(bris, "status", + ByteRangeInputStream.StreamStatus.SEEK); + + int brisOpens = 0; + int brisCloses = 0; + int isCloses = 0; + + // first open, shouldn't close underlying stream + bris.getInputStream(); + verify(bris, times(++brisOpens)).openInputStream(); + verify(bris, times(brisCloses)).close(); + verify(mockStream, times(isCloses)).close(); + + // stream is open, shouldn't close underlying stream + bris.getInputStream(); + verify(bris, times(brisOpens)).openInputStream(); + verify(bris, times(brisCloses)).close(); + verify(mockStream, times(isCloses)).close(); + + // seek forces a reopen, should close underlying stream + bris.seek(1); + bris.getInputStream(); + verify(bris, times(++brisOpens)).openInputStream(); + verify(bris, times(brisCloses)).close(); + verify(mockStream, times(++isCloses)).close(); + + // verify that the underlying stream isn't closed after a seek + // ie. the state was correctly updated + bris.getInputStream(); + verify(bris, times(brisOpens)).openInputStream(); + verify(bris, times(brisCloses)).close(); + verify(mockStream, times(isCloses)).close(); + + // seeking to same location should be a no-op + bris.seek(1); + bris.getInputStream(); + verify(bris, times(brisOpens)).openInputStream(); + verify(bris, times(brisCloses)).close(); + verify(mockStream, times(isCloses)).close(); + + // close should of course close + bris.close(); + verify(bris, times(++brisCloses)).close(); + verify(mockStream, times(++isCloses)).close(); + + // it's already closed, underlying stream should not close + bris.close(); + verify(bris, times(++brisCloses)).close(); + verify(mockStream, times(isCloses)).close(); + + // it's closed, don't reopen it + boolean errored = false; + try { + bris.getInputStream(); + } catch (IOException e) { + errored = true; + assertEquals("Stream closed", e.getMessage()); + } finally { + assertTrue("Read a closed steam", errored); + } + verify(bris, times(brisOpens)).openInputStream(); + verify(bris, times(brisCloses)).close(); + + verify(mockStream, times(isCloses)).close(); + } +}