package com.hubspot.singularity;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import org.junit.Assert;
import org.junit.Test;

import com.google.common.base.Throwables;
import com.hubspot.mesos.JavaUtils;

public class JavaUtilsTest {

  @Test
  public void testFixedTimingOutThreadPool() throws Exception {
    int numMaxThreads = 5;
    long timeoutMillis = 2;

    ThreadPoolExecutor es = JavaUtils.newFixedTimingOutThreadPool(numMaxThreads, timeoutMillis, "test");

    Thread.sleep(timeoutMillis + 100);

    Assert.assertTrue(es.getPoolSize() == 0);

    final CountDownLatch block = new CountDownLatch(1);
    final CountDownLatch cdl = new CountDownLatch(numMaxThreads);

    for (int i = 0; i < numMaxThreads; i++) {
      es.submit(new Runnable() {

        @Override
        public void run() {
          try {
            cdl.countDown();
            cdl.await();
            block.await();
          } catch (Throwable t) {
            throw Throwables.propagate(t);
          }
        }
      });
    }

    cdl.await();
    // all threads are running:
    Assert.assertTrue(es.getPoolSize() == numMaxThreads);
    block.countDown();

    Thread.sleep(timeoutMillis + 100);
    Assert.assertTrue(es.getMaximumPoolSize() == numMaxThreads);
    Assert.assertTrue(es.getPoolSize() == 0);

    es.shutdown();
    es.awaitTermination(timeoutMillis + 1, TimeUnit.MILLISECONDS);
  }

}

@@ -222,73 +222,348 @@
 com.
-google.common.base.Throwables;%0Aimport com.hubspot.mesos.JavaUtils
+fasterxml.jackson.annotation.JsonInclude.Include;%0Aimport com.fasterxml.jackson.databind.DeserializationFeature;%0Aimport com.fasterxml.jackson.databind.ObjectMapper;%0Aimport com.google.common.base.Throwables;%0Aimport com.hubspot.jackson.datatype.protobuf.ProtobufModule;%0Aimport com.hubspot.mesos.JavaUtils;%0A%0Aimport io.dropwizard.jackson.Jackson
 ;%0A%0Ap
@@ -1747,10 +1747,1242 @@
 );%0A  %7D%0A%0A
+  @Test%0A  public void testSingularityTaskIdSerialization() throws Exception %7B%0A    ObjectMapper om = Jackson.newObjectMapper()%0A        .setSerializationInclusion(Include.NON_NULL)%0A        .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)%0A        .registerModule(new ProtobufModule());%0A%0A    SingularityTaskId taskId = new SingularityTaskId(%22rid%22, %22did%22, 100, 1, %22host%22, %22rack%22);%0A    String id = taskId.getId();%0A    SingularityTaskId fromId = SingularityTaskId.valueOf(id);%0A    SingularityTaskId fromJson = om.readValue(om.writeValueAsBytes(taskId), SingularityTaskId.class);%0A%0A    assertEquals(taskId, fromId);%0A    assertEquals(taskId, fromJson);%0A    assertEquals(fromId, fromJson);%0A  %7D%0A%0A  private void assertEquals(SingularityTaskId one, SingularityTaskId two) %7B%0A%0A    Assert.assertEquals(one, two);%0A%0A    Assert.assertEquals(one.getDeployId(), two.getDeployId());%0A    Assert.assertEquals(one.getRequestId(), two.getRequestId());%0A    Assert.assertEquals(one.getSanitizedHost(), two.getSanitizedHost());%0A    Assert.assertEquals(one.getSanitizedRackId(), two.getSanitizedRackId());%0A    Assert.assertEquals(one.getStartedAt(), two.getStartedAt());%0A    Assert.assertEquals(one.getInstanceNo(), two.getInstanceNo());%0A  %7D%0A%0A
 %7D%0A

add serialization test