Package jnr.posix

Source Code of jnr.posix.SignalTest

package jnr.posix;

import jnr.constants.platform.Signal;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;

import java.util.concurrent.atomic.AtomicBoolean;

public class SignalTest {
    private static POSIX posix;
    private static POSIX javaPosix;
   
    @BeforeClass
    public static void setupClass() throws Exception {
        posix = POSIXFactory.getPOSIX(new DummyPOSIXHandler(), true);
        javaPosix = new JavaPOSIX(new DummyPOSIXHandler());
    }
   
    private static void waitUntilTrue(AtomicBoolean var, long maxWait) {
        long start = System.currentTimeMillis();
        while (!var.get() && (System.currentTimeMillis() - start) < maxWait) {
            try {
                Thread.sleep(1);
            } catch (InterruptedException e) {
                break;
            }
        }
    }
   
    @Test
    public void testBasicSignal() {
        Signal s = Signal.SIGHUP;
        final AtomicBoolean fired = new AtomicBoolean(false);
        posix.signal(s, new SignalHandler() {
            public void handle(int signal) {
                fired.set(true);
            }
        });
       
        posix.kill(posix.getpid(), s.intValue());
        waitUntilTrue(fired, 200);
        Assert.assertTrue(fired.get());
    }
   
    @Test
    public void testJavaSignal() {
        Signal s = Signal.SIGHUP;
        final AtomicBoolean fired = new AtomicBoolean(false);
        javaPosix.signal(s, new SignalHandler() {
            public void handle(int signal) {
                fired.set(true);
            }
        });
       
        // have to use native here; no abstraction for kill in pure Java
        // TODO: sun.misc.Signal.raise can be used to kill current pid
        posix.kill(posix.getpid(), s.intValue());

        waitUntilTrue(fired, 200);
        Assert.assertTrue(fired.get());
    }
}
TOP

Related Classes of jnr.posix.SignalTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.