/* * 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.io.InputStream; import org.junit.After; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import tachyon.TestUtils; import tachyon.UnderFileSystem; import tachyon.UnderFileSystemCluster; import tachyon.master.LocalTachyonCluster; import tachyon.thrift.FileAlreadyExistException; import tachyon.thrift.InvalidPathException; /** * Unit tests for <code>tachyon.client.FileOutStream</code>. */ public class FileOutStreamTest { private final int MIN_LEN = 0; private final int MAX_LEN = 255; private final int DELTA = 32; private LocalTachyonCluster mLocalTachyonCluster = null; private TachyonFS mTfs = null; @After public final void after() throws Exception { mLocalTachyonCluster.stop(); System.clearProperty("tachyon.user.quota.unit.bytes"); System.clearProperty("tachyon.user.default.block.size.byte"); } @Before public final void before() throws IOException { System.setProperty("tachyon.user.quota.unit.bytes", "1000"); System.setProperty("tachyon.user.default.block.size.byte", "128"); mLocalTachyonCluster = new LocalTachyonCluster(10000); mLocalTachyonCluster.start(); mTfs = mLocalTachyonCluster.getClient(); } /** * Test <code>void write(int b)</code>. */ @Test public void writeTest1() throws IOException, InvalidPathException, FileAlreadyExistException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : WriteType.values()) { writeTest1Util("/root/testFile_" + k + "_" + op, op, k); } } } private void writeTest1Util(String filePath, WriteType op, int len) throws InvalidPathException, FileAlreadyExistException, IOException { int fileId = mTfs.createFile(filePath); TachyonFile file = mTfs.getFile(fileId); OutStream os = file.getOutStream(op); Assert.assertTrue(os instanceof FileOutStream); for (int k = 0; k < len; k ++) { os.write((byte) k); } os.close(); for (ReadType rOp : ReadType.values()) { file = mTfs.getFile(filePath); InStream is = file.getInStream(rOp); Assert.assertEquals(len, file.length()); byte[] res = new byte[(int) file.length()]; Assert.assertEquals((int) file.length(), is.read(res)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(len, res)); } if (op.isThrough()) { file = mTfs.getFile(filePath); String checkpointPath = file.getUfsPath(); UnderFileSystem ufs = UnderFileSystem.get(checkpointPath); InputStream is = ufs.open(checkpointPath); byte[] res = new byte[(int) file.length()]; if (UnderFileSystemCluster.isUFSHDFS() && 0 == res.length) { // HDFS returns -1 for zero-sized byte array to indicate no more bytes available here. Assert.assertEquals(-1, is.read(res)); } else { Assert.assertEquals((int) file.length(), is.read(res)); } Assert.assertTrue(TestUtils.equalIncreasingByteArray(len, res)); } } /** * Test <code>void write(byte b[])</code>. */ @Test public void writeTest2() throws IOException, InvalidPathException, FileAlreadyExistException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : WriteType.values()) { writeTest2Util("/root/testFile_" + k + "_" + op, op, k); } } } private void writeTest2Util(String filePath, WriteType op, int len) throws InvalidPathException, FileAlreadyExistException, IOException { int fileId = mTfs.createFile(filePath); TachyonFile file = mTfs.getFile(fileId); OutStream os = file.getOutStream(op); Assert.assertTrue(os instanceof FileOutStream); os.write(TestUtils.getIncreasingByteArray(len)); os.close(); for (ReadType rOp : ReadType.values()) { file = mTfs.getFile(filePath); InStream is = file.getInStream(rOp); Assert.assertEquals(len, file.length()); byte[] res = new byte[(int) file.length()]; Assert.assertEquals((int) file.length(), is.read(res)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(len, res)); } if (op.isThrough()) { file = mTfs.getFile(filePath); String checkpointPath = file.getUfsPath(); UnderFileSystem ufs = UnderFileSystem.get(checkpointPath); InputStream is = ufs.open(checkpointPath); byte[] res = new byte[(int) file.length()]; if (UnderFileSystemCluster.isUFSHDFS() && 0 == res.length) { // HDFS returns -1 for zero-sized byte array to indicate no more bytes available here. Assert.assertEquals(-1, is.read(res)); } else { Assert.assertEquals((int) file.length(), is.read(res)); } Assert.assertTrue(TestUtils.equalIncreasingByteArray(len, res)); } } /** * Test <code>void write(byte[] b, int off, int len)</code>. */ @Test public void writeTest3() throws IOException, InvalidPathException, FileAlreadyExistException { for (int k = MIN_LEN; k <= MAX_LEN; k += DELTA) { for (WriteType op : WriteType.values()) { writeTest3Util("/root/testFile_" + k + "_" + op, op, k); } } } private void writeTest3Util(String filePath, WriteType op, int len) throws InvalidPathException, FileAlreadyExistException, IOException { int fileId = mTfs.createFile(filePath); TachyonFile file = mTfs.getFile(fileId); OutStream os = file.getOutStream(op); Assert.assertTrue(os instanceof FileOutStream); os.write(TestUtils.getIncreasingByteArray(0, len / 2), 0, len / 2); os.write(TestUtils.getIncreasingByteArray(len / 2, len / 2), 0, len / 2); os.close(); for (ReadType rOp : ReadType.values()) { file = mTfs.getFile(filePath); InStream is = file.getInStream(rOp); Assert.assertEquals(len, file.length()); byte[] res = new byte[(int) file.length()]; Assert.assertEquals((int) file.length(), is.read(res)); Assert.assertTrue(TestUtils.equalIncreasingByteArray(len / 2 * 2, res)); } if (op.isThrough()) { file = mTfs.getFile(filePath); String checkpointPath = file.getUfsPath(); UnderFileSystem ufs = UnderFileSystem.get(checkpointPath); InputStream is = ufs.open(checkpointPath); byte[] res = new byte[(int) file.length()]; if (UnderFileSystemCluster.isUFSHDFS() && 0 == res.length) { // HDFS returns -1 for zero-sized byte array to indicate no more bytes available here. Assert.assertEquals(-1, is.read(res)); } else { Assert.assertEquals((int) file.length(), is.read(res)); } Assert.assertTrue(TestUtils.equalIncreasingByteArray(len / 2 * 2, res)); } } }