package com.twitter.service;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 * @author Attila Szegedi
 */
public class TestVirtualMachineErrorTerminator
{
    private static boolean exitInvoked;
    private static final Object lock = new Object();

    static {
        VirtualMachineErrorTerminator.setExitInvoker(new Runnable() {
            public void run() {
                synchronized(lock) {
                    exitInvoked = true;
                    lock.notify();
                }
            }
        });
    }

    protected void tearDown() {
      VirtualMachineErrorTerminator.reset();
    }

    public void testNotTerminatingWhenNew() {
        assertNotTerminating();
    }

    private static void assertNotTerminating() {
        try {
            VirtualMachineErrorTerminator.checkTerminating();
        }
        catch(Throwable t) {
            fail();
        }
    }

    public void testNotTerminatingOnNull() {
        VirtualMachineErrorTerminator.terminateVMIfMust(null);
        assertNotTerminating();
    }

    public void testNotTerminatingOnOtherException() {
        VirtualMachineErrorTerminator.terminateVMIfMust(new Exception());
        assertNotTerminating();
    }

    public void testTerminatingOnNestedVirtualMachineError() throws Exception {
        Exception e = new Exception();
        OutOfMemoryError oome = new OutOfMemoryError();
        e.initCause(oome);
        VirtualMachineErrorTerminator.terminateVMIfMust(e);
        assertTerminated();
    }

    public void testTerminatingOnVirtualMachineError() throws Exception {
        VirtualMachineErrorTerminator.terminateVMIfMust(new OutOfMemoryError());
        assertTerminated();
    }

    public void testTerminateDoesntAcceptNull() throws Exception {
        try {
            VirtualMachineErrorTerminator.terminateVM(null);
            fail();
        }
        catch(IllegalArgumentException e) {
        }
    }

    public void testSecondTerminateIneffective() throws Exception {
        OutOfMemoryError oome1 = new OutOfMemoryError();
        OutOfMemoryError oome2 = new OutOfMemoryError();
        VirtualMachineErrorTerminator.terminateVM(oome1);
        VirtualMachineErrorTerminator.terminateVM(oome2);
        try {
            VirtualMachineErrorTerminator.checkTerminating();
            fail();
        }
        catch(OutOfMemoryError oome) {
            assertSame(oome, oome1);
        }
    }

    private static void assertTerminated() throws InterruptedException {
        try {
            VirtualMachineErrorTerminator.checkTerminating();
            fail();
        }
        catch(VirtualMachineError e) {
            synchronized(lock) {
                long timeout = System.currentTimeMillis() + 1000;
                for(;;) {
                    if(exitInvoked) {
                        exitInvoked = false;
                        break;
                    }
                    long now = System.currentTimeMillis();
                    if(now >= timeout) {
                        fail();
                    }
                    lock.wait(timeout - now);
                }
            }
        }
    }
}

@@ -593,17 +593,25 @@
     
-protected
+@After%0A    public
  voi
@@ -669,32 +669,42 @@
 reset();%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -965,32 +965,42 @@
        %7D%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1122,32 +1122,42 @@
 ating();%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1300,32 +1300,42 @@
 ating();%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1607,32 +1607,42 @@
 nated();%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -1807,32 +1807,42 @@
 nated();%0A    %7D%0A%0A
+    @Test%0A
     public void 
@@ -2054,24 +2054,34 @@
    %7D%0A    %7D%0A%0A
+    @Test%0A
     public v

Add @Test and @After annotations to //util/util-core/src/test/java/com/twitter/service:service