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.issue.LOGBACK_849;
15  
16  import java.util.concurrent.ExecutorService;
17  import java.util.concurrent.TimeUnit;
18  
19  import org.junit.jupiter.api.Disabled;
20  import org.junit.jupiter.api.Test;
21  
22  import ch.qos.logback.core.Context;
23  import ch.qos.logback.core.ContextBase;
24  import ch.qos.logback.core.util.ExecutorServiceUtil;
25  import org.junit.jupiter.api.Timeout;
26  
27  public class Basic {
28  
29      ExecutorService executor = ExecutorServiceUtil.newScheduledExecutorService();
30      Context context = new ContextBase();
31  
32      @Test
33      @Timeout(value=100, unit=TimeUnit.MILLISECONDS)
34      public void withNoSubmittedTasksShutdownNowShouldReturnImmediately() throws InterruptedException {
35          executor.shutdownNow();
36          executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
37      }
38  
39      @Disabled
40      @Test
41      public void withOneSlowTask() throws InterruptedException {
42          executor.execute(new InterruptIgnoring(1000));
43          Thread.sleep(100);
44          ExecutorServiceUtil.shutdown(executor);
45      }
46  
47      // InterruptIgnoring ===========================================
48      static class InterruptIgnoring implements Runnable {
49  
50          int delay;
51  
52          InterruptIgnoring(int delay) {
53              this.delay = delay;
54          }
55  
56          public void run() {
57              long runUntil = System.currentTimeMillis() + delay;
58  
59              while (true) {
60                  try {
61                      long sleep = runUntil - System.currentTimeMillis();
62                      System.out.println("will sleep " + sleep);
63                      if (sleep > 0) {
64                          Thread.sleep(delay);
65                      } else {
66                          return;
67                      }
68                  } catch (InterruptedException e) {
69                      // ignore the exception
70                  }
71              }
72          }
73      }
74  
75  }