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