package org.embulk.output.sftp.utils;

import org.embulk.EmbulkTestRuntime;
import org.junit.Rule;
import org.junit.Test;

import java.io.Closeable;

import static junit.framework.TestCase.fail;
import static org.junit.Assert.assertTrue;

public class TestTimeoutCloser
{
    @Rule
    public EmbulkTestRuntime runtime = new EmbulkTestRuntime();

    @Test
    public void testTimeoutAndResume()
    {
        TimeoutCloser closer = new TimeoutCloser(new Closeable()
        {
            @Override
            public void close()
            {
                try {
                    Thread.sleep(5000);
                }
                catch (InterruptedException e) {
                    fail("Failed to sleep");
                }
            }
        });
        closer.timeout = 1;
        long start = System.currentTimeMillis();
        closer.close();
        long duration = System.currentTimeMillis() - start;
        assertTrue("longer than 1s", duration > 1000);
        assertTrue("shorter than 5s", duration < 5000);
    }
}

@@ -141,16 +141,62 @@
 oseable;
+%0Aimport java.util.concurrent.TimeoutException;
 %0A%0Aimport
@@ -252,22 +252,84 @@
 tic org.
-junit.
+hamcrest.CoreMatchers.instanceOf;%0Aimport static org.hamcrest.Matcher
 Assert.a
@@ -334,19 +334,19 @@
 .assertT
-rue
+hat
 ;%0A%0Apubli
@@ -492,14 +492,13 @@
 tAnd
-Resume
+Abort
 ()%0A 
@@ -903,141 +903,132 @@
     
-long start = System.currentTimeMillis();%0A        closer.close();%0A        long duration = System.currentTimeMillis() - start;%0A
+try %7B%0A            closer.close();%0A            fail(%22Should not finish%22);%0A        %7D%0A        catch (Exception e) %7B%0A   
         
 asse
@@ -1027,110 +1027,143 @@
     
+ 
 assertT
-rue(%22longer than 1s%22, duration %3E 1000);%0A        assertTrue(%22shorter than 5s%22, duration %3C 5000);
+hat(e, instanceOf(RuntimeException.class));%0A            assertThat(e.getCause(), instanceOf(TimeoutException.class));%0A        %7D
 %0A   

Update unit test of time-out closer