2 * Copyright (c) 2017 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
8 package org.opendaylight.controller.cluster.io;
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertNull;
14 import static org.junit.Assert.assertTrue;
15 import static org.junit.Assert.fail;
17 import com.google.common.base.Stopwatch;
18 import com.google.common.util.concurrent.Uninterruptibles;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.Arrays;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.After;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
33 * Unit tests for FileBackedOutputStream.
35 * @author Thomas Pantelis
37 public class FileBackedOutputStreamTest {
38 private static final Logger LOG = LoggerFactory.getLogger(FileBackedOutputStreamTest.class);
39 private static final String TEMP_DIR = "target/FileBackedOutputStreamTest";
42 public static void staticSetup() {
47 public static void staticCleanup() {
48 deleteTempFiles(TEMP_DIR);
54 deleteTempFiles(TEMP_DIR);
58 public void cleanup() {
59 deleteTempFiles(TEMP_DIR);
63 public void testFileThresholdNotReached() throws IOException {
64 LOG.info("testFileThresholdNotReached starting");
65 try (FileBackedOutputStream fbos = new FileBackedOutputStream(10, TEMP_DIR)) {
66 byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9};
68 fbos.write(bytes, 1, bytes.length - 1);
70 assertEquals("getCount", bytes.length, fbos.getCount());
71 assertNull("Found unexpected temp file", findTempFileName(TEMP_DIR));
72 assertEquals("Size", bytes.length, fbos.asByteSource().size());
75 assertArrayEquals("Read bytes", bytes, fbos.asByteSource().read());
76 assertArrayEquals("Read bytes", bytes, fbos.asByteSource().read());
81 LOG.info("testFileThresholdNotReached ending");
85 public void testFileThresholdReachedWithWriteBytes() throws IOException {
86 LOG.info("testFileThresholdReachedWithWriteBytes starting");
87 try (FileBackedOutputStream fbos = new FileBackedOutputStream(10, TEMP_DIR)) {
88 byte[] bytes = new byte[]{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
90 fbos.write(bytes, 1, 11);
92 String tempFileName = findTempFileName(TEMP_DIR);
93 assertNotNull("Expected temp file created", tempFileName);
95 fbos.write(bytes[12]);
96 fbos.write(bytes, 13, bytes.length - 13);
98 assertEquals("Temp file", tempFileName, findTempFileName(TEMP_DIR));
99 assertEquals("Size", bytes.length, fbos.asByteSource().size());
101 InputStream inputStream = fbos.asByteSource().openStream();
103 assertArrayEquals("Read bytes", bytes, fbos.asByteSource().read());
105 byte[] inBytes = new byte[bytes.length];
106 assertEquals("# bytes read", bytes.length, inputStream.read(inBytes));
107 assertArrayEquals("Read InputStream", bytes, inBytes);
108 assertEquals("End of stream", -1, inputStream.read());
114 assertNull("Found unexpected temp file", findTempFileName(TEMP_DIR));
117 LOG.info("testFileThresholdReachedWithWriteBytes ending");
121 public void testFileThresholdReachedWithWriteByte() throws IOException {
122 LOG.info("testFileThresholdReachedWithWriteByte starting");
123 try (FileBackedOutputStream fbos = new FileBackedOutputStream(2, TEMP_DIR)) {
124 byte[] bytes = new byte[]{0, 1, 2};
125 fbos.write(bytes[0]);
126 fbos.write(bytes[1]);
128 assertNull("Found unexpected temp file", findTempFileName(TEMP_DIR));
130 fbos.write(bytes[2]);
133 assertNotNull("Expected temp file created", findTempFileName(TEMP_DIR));
135 assertEquals("Size", bytes.length, fbos.asByteSource().size());
136 assertArrayEquals("Read bytes", bytes, fbos.asByteSource().read());
139 LOG.info("testFileThresholdReachedWithWriteByte ending");
142 @Test(expected = IOException.class)
143 public void testWriteAfterAsByteSource() throws IOException {
144 LOG.info("testWriteAfterAsByteSource starting");
145 try (FileBackedOutputStream fbos = new FileBackedOutputStream(3, TEMP_DIR)) {
146 byte[] bytes = new byte[]{0, 1, 2};
149 assertNull("Found unexpected temp file", findTempFileName(TEMP_DIR));
150 assertEquals("Size", bytes.length, fbos.asByteSource().size());
152 // Should throw IOException after call to asByteSource.
158 public void testTempFileDeletedOnGC() throws IOException {
159 LOG.info("testTempFileDeletedOnGC starting");
161 FileBackedOutputStream fbos = null;
163 fbos = new FileBackedOutputStream(1, TEMP_DIR);
164 fbos.write(new byte[] {0, 1});
165 assertNotNull("Expected temp file created", findTempFileName(TEMP_DIR));
173 Stopwatch sw = Stopwatch.createStarted();
174 while (sw.elapsed(TimeUnit.SECONDS) <= 20) {
176 if (findTempFileName(TEMP_DIR) == null) {
179 Uninterruptibles.sleepUninterruptibly(50, TimeUnit.MILLISECONDS);
182 fail("Temp file was not deleted");
185 static String findTempFileName(String dirPath) {
186 String[] files = new File(dirPath).list();
187 assertNotNull(files);
188 assertTrue("Found more than one temp file: " + Arrays.toString(files), files.length < 2);
189 return files.length == 1 ? files[0] : null;
192 static boolean deleteFile(String file) {
193 return new File(file).delete();
196 static void deleteTempFiles(String path) {
197 String[] files = new File(path).list();
199 for (String file: files) {
200 deleteFile(path + File.separator + file);
205 static void createDir(String path) {
206 File dir = new File(path);
207 if (!dir.exists() && !dir.mkdirs()) {
208 throw new RuntimeException("Failed to create temp dir " + path);