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;
015
016import java.io.BufferedReader;
017import java.io.File;
018import java.io.FileReader;
019import java.io.IOException;
020import java.util.concurrent.CountDownLatch;
021import org.junit.Before;
022import org.junit.Test;
023import static org.junit.Assert.assertEquals;
024
025import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
026import ch.qos.logback.core.encoder.EchoEncoder;
027import ch.qos.logback.core.status.OnConsoleStatusListener;
028import ch.qos.logback.core.testUtil.CoreTestConstants;
029import ch.qos.logback.core.testUtil.RandomUtil;
030
031public class PrudentFileAppenderInterruptTest {
032
033    FileAppender<Object> fa = new FileAppender<Object>();
034    Context context = new ContextBase();
035    int diff = RandomUtil.getPositiveInt();
036    String outputDirStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "resilience-" + diff + "/";
037    String logfileStr = outputDirStr + "output.log";
038
039    @Before
040    public void setUp() throws InterruptedException {
041        context.getStatusManager().add(new OnConsoleStatusListener());
042
043        File outputDir = new File(outputDirStr);
044        outputDir.mkdirs();
045
046        fa.setContext(context);
047        fa.setName("FILE");
048        fa.setPrudent(true);
049        fa.setEncoder(new EchoEncoder<Object>());
050        fa.setFile(logfileStr);
051        fa.start();
052    }
053
054    @Test
055    public void smoke() throws InterruptedException, IOException {
056        Runner runner = new Runner(fa);
057        Thread t = new Thread(runner);
058        t.start();
059
060        runner.latch.await();
061
062        fa.doAppend("hello not interrupted");
063
064        FileReader fr = new FileReader(logfileStr);
065        BufferedReader br = new BufferedReader(fr);
066
067        int totalLines = 0;
068        while (br.readLine() != null) {
069            totalLines++; // In this test, the content of the file does not matter
070        }
071        fr.close();
072        br.close();
073
074        assertEquals("Incorrect number of logged lines", 2, totalLines);
075    }
076
077    class Runner extends RunnableWithCounterAndDone {
078        FileAppender<Object> fa;
079        CountDownLatch latch = new CountDownLatch(1); // Just to make sure this is executed before we log in the test
080                                                      // method
081
082        Runner(FileAppender<Object> fa) {
083            this.fa = fa;
084        }
085
086        public void run() {
087            Thread.currentThread().interrupt();
088            fa.doAppend("hello interrupted");
089            latch.countDown();
090        }
091    }
092
093}