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.recovery;
15  
16  import static org.mockito.Mockito.spy;
17  import static org.mockito.Mockito.verify;
18  
19  import java.io.File;
20  
21  import org.junit.jupiter.api.BeforeAll;
22  import org.junit.jupiter.api.Test;
23  
24  import ch.qos.logback.core.Context;
25  import ch.qos.logback.core.ContextBase;
26  import ch.qos.logback.core.FileAppender;
27  import ch.qos.logback.core.testUtil.CoreTestConstants;
28  import ch.qos.logback.core.testUtil.RandomUtil;
29  
30  /**
31   * @author Ceki Gülcü
32   */
33  public class ResilientOutputStreamTest {
34  
35      int diff = RandomUtil.getPositiveInt();
36      Context context = new ContextBase();
37  
38      @BeforeAll
39      public static void setUp() {
40          File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX);
41          file.mkdirs();
42      }
43  
44      @Test
45      public void verifyRecuperationAfterFailure() throws Exception {
46          File file = new File(CoreTestConstants.OUTPUT_DIR_PREFIX + "resilient" + diff + ".log");
47          ResilientFileOutputStream rfos = new ResilientFileOutputStream(file, true, FileAppender.DEFAULT_BUFFER_SIZE);
48          rfos.setContext(context);
49  
50          ResilientFileOutputStream spy = spy(rfos);
51  
52          spy.write("a".getBytes());
53          spy.flush();
54  
55          spy.getChannel().close();
56          spy.write("b".getBytes());
57          spy.flush();
58          Thread.sleep(RecoveryCoordinator.BACKOFF_COEFFICIENT_MIN + 10);
59          spy.write("c".getBytes());
60          spy.flush();
61          verify(spy).openNewOutputStream();
62  
63      }
64  
65  }