View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core;
15  
16  import java.io.BufferedReader;
17  import java.io.File;
18  import java.io.FileReader;
19  import java.io.IOException;
20  import java.util.concurrent.CountDownLatch;
21  import org.junit.Before;
22  import org.junit.Test;
23  import static org.junit.Assert.assertEquals;
24  
25  import ch.qos.logback.core.contention.RunnableWithCounterAndDone;
26  import ch.qos.logback.core.encoder.EchoEncoder;
27  import ch.qos.logback.core.status.OnConsoleStatusListener;
28  import ch.qos.logback.core.testUtil.CoreTestConstants;
29  import ch.qos.logback.core.testUtil.RandomUtil;
30  
31  public class PrudentFileAppenderInterruptTest {
32  
33      FileAppender<Object> fa = new FileAppender<Object>();
34      Context context = new ContextBase();
35      int diff = RandomUtil.getPositiveInt();
36      String outputDirStr = CoreTestConstants.OUTPUT_DIR_PREFIX + "resilience-" + diff + "/";
37      String logfileStr = outputDirStr + "output.log";
38  
39      @Before
40      public void setUp() throws InterruptedException {
41          context.getStatusManager().add(new OnConsoleStatusListener());
42  
43          File outputDir = new File(outputDirStr);
44          outputDir.mkdirs();
45  
46          fa.setContext(context);
47          fa.setName("FILE");
48          fa.setPrudent(true);
49          fa.setEncoder(new EchoEncoder<Object>());
50          fa.setFile(logfileStr);
51          fa.start();
52      }
53  
54      @Test
55      public void smoke() throws InterruptedException, IOException {
56          Runner runner = new Runner(fa);
57          Thread t = new Thread(runner);
58          t.start();
59  
60          runner.latch.await();
61  
62          fa.doAppend("hello not interrupted");
63  
64          FileReader fr = new FileReader(logfileStr);
65          BufferedReader br = new BufferedReader(fr);
66  
67          int totalLines = 0;
68          while (br.readLine() != null) {
69              totalLines++; // In this test, the content of the file does not matter
70          }
71          fr.close();
72          br.close();
73  
74          assertEquals("Incorrect number of logged lines", 2, totalLines);
75      }
76  
77      class Runner extends RunnableWithCounterAndDone {
78          FileAppender<Object> fa;
79          CountDownLatch latch = new CountDownLatch(1); // Just to make sure this is executed before we log in the test
80                                                        // method
81  
82          Runner(FileAppender<Object> fa) {
83              this.fa = fa;
84          }
85  
86          public void run() {
87              Thread.currentThread().interrupt();
88              fa.doAppend("hello interrupted");
89              latch.countDown();
90          }
91      }
92  
93  }