package org.threadly.concurrent.lock;

import static org.junit.Assert.*;

import org.junit.Test;

@SuppressWarnings("javadoc")
public class NativeLockTest {
  private static final NativeLock lock = new NativeLock();
  
  @Test
  public void awaitTimeoutTest() {
    final long waitTimeInMs = 10;
    
    long start = System.currentTimeMillis();
    try {
      lock.await(waitTimeInMs);
    } catch (InterruptedException e) {
      fail("Exception: " + e);
    }
    long resume = System.currentTimeMillis();
    
    assertTrue(resume - start >= waitTimeInMs);
    assertTrue(resume - start < waitTimeInMs + 50);
  }
  
  @Test
  public void awaitUninterruptiblyTimeoutTest() {
    final long waitTimeInMs = 10;
    
    long start = System.currentTimeMillis();
    lock.awaitUninterruptibly(waitTimeInMs);
    long resume = System.currentTimeMillis();
    
    assertTrue(resume - start >= waitTimeInMs);
    assertTrue(resume - start < waitTimeInMs + 50);
  }
  
  @Test
  public void sleepTest() {
    final long sleepTimeInMs = 10;
    
    long start = System.currentTimeMillis();
    try {
      lock.sleep(sleepTimeInMs);
    } catch (InterruptedException e) {
      fail("Exception: " + e);
    }
    long resume = System.currentTimeMillis();
    
    assertTrue(resume - start >= sleepTimeInMs);
    assertTrue(resume - start < sleepTimeInMs + 50);
  }
  
  @Test
  public void sleepUninterruptiblyTest() {
    final long sleepTimeInMs = 10;
    
    long start = System.currentTimeMillis();
    lock.sleepUninterruptibly(sleepTimeInMs);
    long resume = System.currentTimeMillis();
    
    assertTrue(resume - start >= sleepTimeInMs);
    assertTrue(resume - start < sleepTimeInMs + 50);
  }
}

@@ -295,32 +295,88 @@
 ;%0A    %0A    long 
+start;%0A    long resume;%0A    synchronized (lock) %7B%0A      
 start = System.c
@@ -387,32 +387,34 @@
 ntTimeMillis();%0A
+  
     try %7B%0A      
@@ -409,24 +409,26 @@
 try %7B%0A      
+  
 lock.await(w
@@ -433,32 +433,34 @@
 (waitTimeInMs);%0A
+  
     %7D catch (Int
@@ -480,32 +480,34 @@
 tion e) %7B%0A      
+ 
+ 
 fail(%22Exception:
@@ -515,34 +515,33 @@
 %22 + e);%0A    
+  
 %7D%0A    
-long
+ 
  resume = Sy
@@ -558,32 +558,38 @@
 ntTimeMillis();%0A
+    %7D%0A
     %0A    assertT
@@ -772,37 +772,89 @@
 eInMs = 10;%0A
+%0A
     
-%0A    long
+long start;%0A    long resume;%0A    synchronized (lock) %7B%0A     
  start = Sys
@@ -870,32 +870,34 @@
 ntTimeMillis();%0A
+  
     lock.awaitUn
@@ -921,36 +921,33 @@
 tTimeInMs);%0A    
-long
+ 
  resume = System
@@ -960,32 +960,38 @@
 ntTimeMillis();%0A
+    %7D%0A
     %0A    assertT

Fix for an exception thrown in these unit tests