001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.recovery;
015
016import static org.mockito.Mockito.spy;
017import static org.mockito.Mockito.verify;
018
019import java.io.File;
020
021import org.junit.BeforeClass;
022import org.junit.Test;
023
024import ch.qos.logback.core.Context;
025import ch.qos.logback.core.ContextBase;
026import ch.qos.logback.core.FileAppender;
027import ch.qos.logback.core.testUtil.CoreTestConstants;
028import ch.qos.logback.core.testUtil.RandomUtil;
029
030/**
031 * @author Ceki Gülcü
032 */
033public class ResilientOutputStreamTest {
034
035    int diff = RandomUtil.getPositiveInt();
036    Context context = new ContextBase();
037
038    @BeforeClass
039    public static void setUp() {
040        File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX);
041        file.mkdirs();
042    }
043
044    @Test
045    public void verifyRecuperationAfterFailure() throws Exception {
046        File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "resilient" + diff + ".log");
047        ResilientFileOutputStream rfos = new ResilientFileOutputStream(file, true, FileAppender.DEFAULT_BUFFER_SIZE);
048        rfos.setContext(context);
049
050        ResilientFileOutputStream spy = spy(rfos);
051
052        spy.write("a".getBytes());
053        spy.flush();
054
055        spy.getChannel().close();
056        spy.write("b".getBytes());
057        spy.flush();
058        Thread.sleep(RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN + 10);
059        spy.write("c".getBytes());
060        spy.flush();
061        verify(spy).openNewOutputStream();
062
063    }
064
065}