1
2
3
4
5
6
7
8
9
10
11
12
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
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 }