package org.threadly.concurrent;

import static org.junit.Assert.*;

import org.junit.Test;
import org.threadly.test.TestUtil;

@SuppressWarnings("javadoc")
public class PriorityScheduledExecutorTest {
  @Test
  public void getDefaultPriorityTest() {
    TaskPriority priority = TaskPriority.High;
    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(1, 1, 1000, 
                                                                        priority, 1000);
    assertEquals(scheduler.getDefaultPriority(), priority);
    scheduler.shutdown();
    
    priority = TaskPriority.Low;
    scheduler = new PriorityScheduledExecutor(1, 1, 1000, 
                                              priority, 1000);
    assertEquals(scheduler.getDefaultPriority(), priority);
    scheduler.shutdown();
  }
  
  @Test
  public void getAndSetCorePoolSizeTest() {
    int corePoolSize = 1;
    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(corePoolSize, 
                                                                        corePoolSize + 10, 1000);
    
    assertEquals(scheduler.getCorePoolSize(), corePoolSize);
    
    corePoolSize = 10;
    scheduler.setMaxPoolSize(corePoolSize + 10);
    scheduler.setCorePoolSize(corePoolSize);
    
    assertEquals(scheduler.getCorePoolSize(), corePoolSize);
    
    scheduler.shutdown();
  }
  
  /*@Test
  public void getAndSetCorePoolSizeFail() {
    // TODO
  }*/
  
  @Test
  public void getAndSetMaxPoolSizeTest() {
    int maxPoolSize = 1;
    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(1, maxPoolSize, 1000);
    
    assertEquals(scheduler.getMaxPoolSize(), maxPoolSize);
    
    maxPoolSize = 10;
    scheduler.setMaxPoolSize(maxPoolSize);
    
    assertEquals(scheduler.getMaxPoolSize(), maxPoolSize);
    
    scheduler.shutdown();
  }
  
  /*@Test
  public void getAndSetMaxPoolSizeFail() {
    // TODO
  }*/
  
  @Test
  public void getAndSetKeepAliveTimeTest() {
    long keepAliveTime = 1000;
    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(1, 1, keepAliveTime);
    assertEquals(scheduler.getKeepAliveTime(), keepAliveTime);
    
    keepAliveTime = Long.MAX_VALUE;
    scheduler.setKeepAliveTime(keepAliveTime);
    
    assertEquals(scheduler.getKeepAliveTime(), keepAliveTime);
    
    scheduler.shutdown();
  }
  
  /*@Test
  public void setKeepAliveTimeFail() {
    // TODO
  }*/
  
  @Test
  public void getCurrentPoolSizeTest() {
    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(1, 1, 1000);
    // verify nothing at the start
    assertEquals(scheduler.getCurrentPoolSize(), 0);
    
    scheduler.execute(new TestRunnable());
    
    TestUtil.sleep(100);  // wait for execution
    
    assertEquals(scheduler.getCurrentPoolSize(), 1);
  }
  
  private class TestRunnable implements Runnable {
    private int ranCount = 0;
    
    @Override
    public void run() {
      ranCount++;
    }
  }
}

@@ -62,16 +62,58 @@
 ert.*;%0A%0A
+import java.util.NoSuchElementException;%0A%0A
 import o
@@ -1395,34 +1395,32 @@
 own();%0A  %7D%0A  %0A  
-/*
 @Test%0A  public v
@@ -1427,55 +1427,775 @@
 oid 
-getAndSetCorePoolSizeFail(
+setCorePoolSizeFail() %7B%0A    int corePoolSize = 1;%0A    int maxPoolSize = 10;%0A    // first construct a valid scheduler%0A    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(corePoolSize, %0A                                                                        maxPoolSize, 1000);%0A    %0A    // verify no negative values%0A    try %7B%0A      scheduler.setCorePoolSize(-1);%0A      fail(%22Exception should have been thrown%22);%0A    %7D catch (IllegalArgumentException expected
 ) %7B%0A
+  
     // 
-TODO%0A  %7D*/
+ignored%0A    %7D%0A    // verify can't be set higher than max size%0A    try %7B%0A      scheduler.setCorePoolSize(maxPoolSize + 1);%0A      fail(%22Exception should have been thrown%22);%0A    %7D catch (IllegalArgumentException expected) %7B%0A      // ignored%0A    %7D%0A    %0A    scheduler.shutdown();%0A  %7D
 %0A  %0A
@@ -2607,76 +2607,378 @@
  %0A  
-/*
 @Test
-%0A  public void getAndSetMaxPoolSizeFail() %7B%0A    // TODO
+ (expected = IllegalArgumentException.class)%0A  public void setMaxPoolSizeFail() %7B%0A    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(1, 1, 1000);%0A    %0A    try %7B%0A      scheduler.setMaxPoolSize(-1); // should throw exception for negative value%0A      fail(%22Exception should have been thrown%22);%0A    %7D finally %7B%0A      scheduler.shutdown();%0A    %7D
 %0A  %7D
-*/
 %0A  %0A
@@ -3418,72 +3418,383 @@
  %0A  
-/*
 @Test
-%0A  public void setKeepAliveTimeFail() %7B%0A    // TODO%0A  %7D*/
+ (expected = IllegalArgumentException.class)%0A  public void setKeepAliveTimeFail() %7B%0A    PriorityScheduledExecutor scheduler = new PriorityScheduledExecutor(1, 1, 1000);%0A    %0A    try %7B%0A      scheduler.setKeepAliveTime(-1L); // should throw exception for negative value%0A      fail(%22Exception should have been thrown%22);%0A    %7D finally %7B%0A      scheduler.shutdown();%0A    %7D%0A  %7D
 %0A  %0A

Implement these TODO notes