001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.issue.LOGBACK_849;
015
016import java.util.concurrent.ExecutorService;
017import java.util.concurrent.TimeUnit;
018
019import org.junit.Ignore;
020import org.junit.Test;
021
022import ch.qos.logback.core.Context;
023import ch.qos.logback.core.ContextBase;
024import ch.qos.logback.core.util.ExecutorServiceUtil;
025
026public class Basic {
027
028    ExecutorService executor = ExecutorServiceUtil.newScheduledExecutorService();
029    Context context = new ContextBase();
030
031    @Test(timeout = 100)
032    public void withNoSubmittedTasksShutdownNowShouldReturnImmediately() throws InterruptedException {
033        executor.shutdownNow();
034        executor.awaitTermination(5000, TimeUnit.MILLISECONDS);
035    }
036
037    @Ignore
038    @Test
039    public void withOneSlowTask() throws InterruptedException {
040        executor.execute(new InterruptIgnoring(1000));
041        Thread.sleep(100);
042        ExecutorServiceUtil.shutdown(executor);
043    }
044
045    // InterruptIgnoring ===========================================
046    static class InterruptIgnoring implements Runnable {
047
048        int delay;
049
050        InterruptIgnoring(int delay) {
051            this.delay = delay;
052        }
053
054        public void run() {
055            long runUntil = System.currentTimeMillis() + delay;
056
057            while (true) {
058                try {
059                    long sleep = runUntil - System.currentTimeMillis();
060                    System.out.println("will sleep " + sleep);
061                    if (sleep > 0) {
062                        Thread.sleep(delay);
063                    } else {
064                        return;
065                    }
066                } catch (InterruptedException e) {
067                    // ignore the exception
068                }
069            }
070        }
071    }
072
073}