/* * 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 tachyon.client; import java.io.IOException; import java.util.HashSet; import java.util.Set; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import tachyon.TestUtils; import tachyon.master.LocalTachyonCluster; /** * Unit tests for <code>tachyon.client.LocalBlockInStream</code>. */ public class LocalBlockInStreamTest { private final int MIN_LEN = 0; private final int MAX_LEN = 255; private final int DELTA = 33; private LocalTachyonCluster mLocalTachyonCluster = null; private TachyonFS mTfs = null; private Set<WriteType> mWriteCacheType; @After public final void after() throws Exception { mLocalTachyonCluster.stop(); System.clearProperty("tachyon.user.quota.unit.bytes"); } @Before public final void before() throws IOException { System.setProperty("tachyon.user.quota.unit.bytes", "1000"); mLocalTachyonCluster = new LocalTachyonCluster(10000); mLocalTachyonCluster.start(); mTfs = mLocalTachyonCluster.getClient(); mWriteCacheType = new HashSet<WriteType>(); mWriteCacheType.add(WriteType.MUST_CACHE); mWriteCacheType.add(WriteType.CACHE_THROUGH); } /** * Test <code>void read()</code>. */ @Test public void readTest1() throws IOException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : mWriteCacheType) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = file.getInStream(ReadType.NO_CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } byte[] ret = new byte[k]; int value = is.read(); int cnt = 0; while (value != -1) { Assert.assertTrue(value >= 0); Assert.assertTrue(value < 256); ret[cnt ++] = (byte) value; value = is.read(); } Assert.assertEquals(cnt, k); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); Assert.assertTrue(file.isInMemory()); is = file.getInStream(ReadType.CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } ret = new byte[k]; value = is.read(); cnt = 0; while (value != -1) { Assert.assertTrue(value >= 0); Assert.assertTrue(value < 256); ret[cnt ++] = (byte) value; value = is.read(); } Assert.assertEquals(cnt, k); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); Assert.assertTrue(file.isInMemory()); } } } /** * Test <code>void read(byte b[])</code>. */ @Test public void readTest2() throws IOException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : mWriteCacheType) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = file.getInStream(ReadType.NO_CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } byte[] ret = new byte[k]; Assert.assertEquals(k, is.read(ret)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); Assert.assertTrue(file.isInMemory()); is = file.getInStream(ReadType.CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } ret = new byte[k]; Assert.assertEquals(k, is.read(ret)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); Assert.assertTrue(file.isInMemory()); } } } /** * Test <code>void read(byte[] b, int off, int len)</code>. */ @Test public void readTest3() throws IOException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : mWriteCacheType) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = file.getInStream(ReadType.NO_CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } byte[] ret = new byte[k / 2]; Assert.assertEquals(k / 2, is.read(ret, 0, k / 2)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k / 2, ret)); is.close(); Assert.assertTrue(file.isInMemory()); is = file.getInStream(ReadType.CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } ret = new byte[k]; Assert.assertEquals(k, is.read(ret, 0, k)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(k, ret)); is.close(); Assert.assertTrue(file.isInMemory()); } } } /** * Test <code>void seek(long pos)</code>. * * @throws IOException */ @Test public void seekExceptionTest() throws IOException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : mWriteCacheType) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = file.getInStream(ReadType.NO_CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } try { is.seek(-1); } catch (IOException e) { // This is expected continue; } is.close(); throw new IOException("Except seek IOException"); } } } /** * Test <code>void seek(long pos)</code>. * * @throws IOException */ @Test public void seekTest() throws IOException { for (int k = MIN_LEN + DELTA; k <= MAX_LEN; k += DELTA) { for (WriteType op : mWriteCacheType) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = file.getInStream(ReadType.NO_CACHE); if (k == 0) { Assert.assertTrue(is instanceof EmptyBlockInStream); } else { Assert.assertTrue(is instanceof LocalBlockInStream); } is.seek(k / 3); Assert.assertEquals(k / 3, is.read()); is.seek(k / 2); Assert.assertEquals(k / 2, is.read()); is.seek(k / 4); Assert.assertEquals(k / 4, is.read()); is.close(); } } } /** * Test <code>long skip(long len)</code>. */ @Test public void skipTest() throws IOException { for (int k = MIN_LEN + DELTA; k <= MAX_LEN; k += DELTA) { for (WriteType op : mWriteCacheType) { int fileId = TestUtils.createByteFile(mTfs, "/root/testFile_" + k + "_" + op, op, k); TachyonFile file = mTfs.getFile(fileId); InStream is = file.getInStream(ReadType.CACHE); Assert.assertTrue(is instanceof LocalBlockInStream); Assert.assertEquals(k / 2, is.skip(k / 2)); Assert.assertEquals(k / 2, is.read()); is.close(); Assert.assertTrue(file.isInMemory()); is = file.getInStream(ReadType.CACHE); Assert.assertTrue(is instanceof LocalBlockInStream); int t = k / 3; Assert.assertEquals(t, is.skip(t)); Assert.assertEquals(t, is.read()); Assert.assertEquals(t, is.skip(t)); Assert.assertEquals(2 * t + 1, is.read()); is.close(); Assert.assertTrue(file.isInMemory()); } } } }