more tests on ThreadedBuildTriggerTest
authorEugene Petrenko <eugene.petrenko@gmail.com>
Fri, 15 Jul 2011 11:52:18 +0000 (15:52 +0400)
committerEugene Petrenko <eugene.petrenko@gmail.com>
Fri, 15 Jul 2011 11:52:18 +0000 (15:52 +0400)
nuget-server/src/jetbrains/buildServer/nuget/server/trigger/ThreadedBuildTriggerPolicy.java
nuget-tests/src/jetbrains/buildServer/nuget/tests/server/ThreadedBuildTriggerPolicyTest.java [new file with mode: 0644]
nuget-tests/src/testng-nuget-tests..xml

index 0ebbaaf172e118a2a77cd38aac4b7e29cd3944e5..c6784442c3e0ecb905ed765e4de7e1b351003739 100644 (file)
 \r
 package jetbrains.buildServer.nuget.server.trigger;\r
 \r
+import com.intellij.openapi.diagnostic.Logger;\r
 import jetbrains.buildServer.buildTriggers.BuildTriggerException;\r
 import jetbrains.buildServer.buildTriggers.PolledBuildTrigger;\r
 import jetbrains.buildServer.buildTriggers.PolledTriggerContext;\r
 import org.jetbrains.annotations.NotNull;\r
 \r
-import java.util.concurrent.Callable;\r
-import java.util.concurrent.ExecutionException;\r
-import java.util.concurrent.ExecutorService;\r
-import java.util.concurrent.Future;\r
+import java.util.concurrent.*;\r
 \r
 /**\r
  * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
  * Date: 12.07.11 23:29\r
  */\r
 public class ThreadedBuildTriggerPolicy extends PolledBuildTrigger {\r
+  private static final Logger LOG = Logger.getInstance(ThreadedBuildTriggerPolicy.class.getName());\r
+\r
   private final ExecutorService myExecutor;\r
   private final TriggerUpdateChecker myUpdater;\r
   private Future<BuildStartReason> myUpdateRequired;\r
@@ -44,7 +44,14 @@ public class ThreadedBuildTriggerPolicy extends PolledBuildTrigger {
   @Override\r
   public synchronized void triggerBuild(@NotNull PolledTriggerContext context) throws BuildTriggerException {\r
     if (myUpdateRequired == null) {\r
-      myUpdateRequired = myExecutor.submit(createUpdateTask(context));\r
+      try {\r
+        myUpdateRequired = myExecutor.submit(createUpdateTask(context));\r
+      } catch (RejectedExecutionException e) {\r
+        String msg = "Failed to enqueue trigger task: " + myUpdater + ". " + e.getMessage();\r
+        LOG.warn(msg);\r
+        LOG.debug(msg, e);\r
+        return;\r
+      }\r
     }\r
 \r
     if (!myUpdateRequired.isDone()) return;\r
diff --git a/nuget-tests/src/jetbrains/buildServer/nuget/tests/server/ThreadedBuildTriggerPolicyTest.java b/nuget-tests/src/jetbrains/buildServer/nuget/tests/server/ThreadedBuildTriggerPolicyTest.java
new file mode 100644 (file)
index 0000000..5520cdc
--- /dev/null
@@ -0,0 +1,140 @@
+/*\r
+ * Copyright 2000-2011 JetBrains s.r.o.\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
+\r
+package jetbrains.buildServer.nuget.tests.server;\r
+\r
+import jetbrains.buildServer.BaseTestCase;\r
+import jetbrains.buildServer.buildTriggers.BuildTriggerException;\r
+import jetbrains.buildServer.buildTriggers.PolledTriggerContext;\r
+import jetbrains.buildServer.nuget.server.trigger.BuildStartReason;\r
+import jetbrains.buildServer.nuget.server.trigger.ThreadedBuildTriggerPolicy;\r
+import jetbrains.buildServer.nuget.server.trigger.TriggerUpdateChecker;\r
+import jetbrains.buildServer.serverSide.SBuildType;\r
+import org.jmock.Expectations;\r
+import org.jmock.Mockery;\r
+import org.testng.Assert;\r
+import org.testng.annotations.BeforeMethod;\r
+import org.testng.annotations.Test;\r
+\r
+import java.util.concurrent.*;\r
+\r
+/**\r
+ * Created by Eugene Petrenko (eugene.petrenko@gmail.com)\r
+ * Date: 15.07.11 14:10\r
+ */\r
+public class ThreadedBuildTriggerPolicyTest extends BaseTestCase {\r
+  private Mockery m;\r
+  private ExecutorService executor;\r
+  private TriggerUpdateChecker checker;\r
+  private ThreadedBuildTriggerPolicy policy;\r
+  private PolledTriggerContext ctx;\r
+  private Future future;\r
+  private SBuildType bt;\r
+\r
+\r
+  @BeforeMethod\r
+  @Override\r
+  protected void setUp() throws Exception {\r
+    super.setUp();\r
+    m = new Mockery();\r
+    executor = m.mock(ExecutorService.class);\r
+    checker = m.mock(TriggerUpdateChecker.class);\r
+    policy = new ThreadedBuildTriggerPolicy(executor, checker);\r
+    ctx = m.mock(PolledTriggerContext.class);\r
+    future = m.mock(Future.class);\r
+    bt = m.mock(SBuildType.class);\r
+\r
+    m.checking(new Expectations(){{\r
+      allowing(ctx).getBuildType(); will(returnValue(bt));\r
+    }});\r
+  }\r
+\r
+  @Test\r
+  public void test_should_start_thread_on_call() {\r
+    m.checking(new Expectations(){{\r
+      //noinspection unchecked\r
+      oneOf(executor).submit(with(any(Callable.class)));\r
+      will(returnValue(future));\r
+\r
+      oneOf(future).isDone(); will(returnValue(false));\r
+    }});\r
+     policy.triggerBuild(ctx);\r
+  }\r
+\r
+  @Test\r
+  public void test_should_not_restart_thread_on_second_call() {\r
+    test_should_start_thread_on_call();\r
+\r
+    m.checking(new Expectations(){{\r
+      oneOf(future).isDone(); will(returnValue(false));\r
+    }});\r
+\r
+    policy.triggerBuild(ctx);\r
+  }\r
+\r
+  @Test\r
+  public void test_should_start_build_on_result() throws ExecutionException, InterruptedException {\r
+    test_should_start_thread_on_call();\r
+\r
+    m.checking(new Expectations(){{\r
+      oneOf(future).isDone(); will(returnValue(true));\r
+      oneOf(future).get(); will(returnValue(new BuildStartReason("aaa")));\r
+      oneOf(bt).addToQueue("aaa");\r
+    }});\r
+\r
+    policy.triggerBuild(ctx);\r
+  }\r
+\r
+  @Test\r
+  public void test_should_not_start_build_on_result() throws ExecutionException, InterruptedException {\r
+    test_should_start_thread_on_call();\r
+\r
+    m.checking(new Expectations(){{\r
+      oneOf(future).isDone(); will(returnValue(true));\r
+      oneOf(future).get(); will(returnValue(null));\r
+    }});\r
+\r
+    policy.triggerBuild(ctx);\r
+  }\r
+\r
+  @Test\r
+  public void test_should_throw_on_execution_exception() throws ExecutionException, InterruptedException {\r
+    test_should_start_thread_on_call();\r
+\r
+    m.checking(new Expectations(){{\r
+      oneOf(future).isDone(); will(returnValue(true));\r
+      oneOf(future).get(); will(throwException(new ExecutionException(new RuntimeException("fail"))));\r
+    }});\r
+\r
+    try {\r
+      policy.triggerBuild(ctx);\r
+      Assert.fail("excption must be thrown");\r
+    } catch (BuildTriggerException e) {\r
+      Assert.assertEquals(e.getMessage(), "fail");\r
+    }\r
+  }\r
+\r
+  @Test\r
+  public void test_should_not_fail_on_rejected_execution_exception() {\r
+    m.checking(new Expectations(){{\r
+      //noinspection unchecked\r
+      oneOf(executor).submit(with(any(Callable.class)));\r
+      will(throwException(new RejectedExecutionException("failed to exec")));\r
+    }});\r
+\r
+    policy.triggerBuild(ctx);\r
+  }\r
+}\r
index 6b53facc6f78003e8f45af77b691d91fae57de06..e589277c35d092c0c257f1b86bd0c82228ebdea9 100644 (file)
@@ -13,6 +13,7 @@
       <class name="jetbrains.buildServer.nuget.tests.server.ListPackagesCommandTest"/>\r
       <class name="jetbrains.buildServer.nuget.tests.server.ListPackagesCommandProcessorTest"/>\r
 \r
+      <class name="jetbrains.buildServer.nuget.tests.server.ThreadedBuildTriggerPolicyTest"/>\r
       <class name="jetbrains.buildServer.nuget.tests.server.NamedPackagesUpdateCheckerTest"/>\r
     </classes>\r
   </test>\r