TimerTest: invert order of tasks for tests about exception and cancelling

The tests checking for timer cancellation after exception were
scheduling two tasks. First, one to that throws an exception (supposed
to cancel the timer) and another one that shouldn't execute (but
should be scheduled) because of the timer being cancelled.

In some interleavings, the first task would execute before the second
one was even scheduled, and the test would fail when trying to schedule
on a cancelled timer.

Inverting the order so that the one that doesn't cancel the Timer
gets scheduled first.

Also, set two different increment amounts between threads, to make
that the one that executed is the intended one.

Bug: 26147977

(cherry picked from commit e3f79caf396d7a3a27a4a55ece1a65e7603cf0cc)

Change-Id: I31e206367c889cc1753c7f12d8254360be8c5c69
diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/TimerTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/TimerTest.java
index a353007..33b17c4 100644
--- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/TimerTest.java
+++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/TimerTest.java
@@ -1111,17 +1111,20 @@
     private static class IncrementCounterTaskAndPossiblyThrowAfter extends TimerTask {
 
         private final AtomicLong counter;
-
+        private final int incrementAmount;
         private final boolean willThrow;
 
-        IncrementCounterTaskAndPossiblyThrowAfter(AtomicLong counter, boolean willThrow) {
+
+        IncrementCounterTaskAndPossiblyThrowAfter(
+                AtomicLong counter, int incrementAmount, boolean willThrow) {
             this.counter = counter;
+            this.incrementAmount = incrementAmount;
             this.willThrow = willThrow;
         }
 
         @Override
         public void run() {
-            counter.incrementAndGet();
+            counter.addAndGet(incrementAmount);
             if (willThrow) {
                 throw new IllegalStateException("TimerTask runtime exception from run()");
             }
@@ -1153,23 +1156,35 @@
         try {
             Timer t = new Timer();
             final AtomicLong counter = new AtomicLong();
-            TimerTask task1 = new IncrementCounterTaskAndPossiblyThrowAfter(
+
+            // Schedule tasks to run:
+            // A) {In 1 millis} Increment a counter by 1 and throw an exception
+            // B) {In 100 millis} Increment a counter by 1000 (but it's not intended to be executed
+            //        because of the previous exception).
+            // We want A and B to be scheduled before A runs.
+            // We add them in reverse order.
+            // We have ~99 millis after scheduling B to schedule A. If A ran before we scheduled B
+            // we would get an exception when we came to schedule B.
+            TimerTask taskThatDoesntThrow = new IncrementCounterTaskAndPossiblyThrowAfter(
                     counter,
-                    true /* willThrow */);
-            TimerTask task2 = new IncrementCounterTaskAndPossiblyThrowAfter(
-                    counter,
+                    1000,  /* incrementAmount */
                     false /* willThrow */);
-            t.scheduleAtFixedRate(task1, 1 /* delay */, 100 /* period */);
-            t.schedule(task2, 100 /* delay */);
+            TimerTask taskThatThrows = new IncrementCounterTaskAndPossiblyThrowAfter(
+                    counter,
+                    1,    /* incrementAmount */
+                    true  /* willThrow */);
+            t.schedule(taskThatDoesntThrow, 100 /* delay */);
+            t.scheduleAtFixedRate(taskThatThrows, 1 /* delay */, 100 /* period */);
+
             swallowUncaughtExceptionHandler.waitForException(1000);
             // Check the counter wasn't increased more than once (ie, the exception killed the
             // execution thread).
             assertEquals("Counter should be 1, and is: " + counter.get(), 1, counter.get());
 
-            assertTrue("The timer should not cancel the tasks", task1.cancel());
-            assertTrue("The timer should not cancel the tasks", task2.cancel());
+            assertTrue("The timer should not cancel the tasks", taskThatDoesntThrow.cancel());
+            assertTrue("The timer should not cancel the tasks", taskThatThrows.cancel());
 
-            TimerTask task3 = new TimerTask() {
+            TimerTask otherTask = new TimerTask() {
                 @Override
                 public void run() {
                     counter.incrementAndGet();
@@ -1177,7 +1192,7 @@
             };
 
             try {
-                t.schedule(task3, 1);
+                t.schedule(otherTask, 1);
                 fail("Timer should be cancelled and no new tasks should be allowed");
             } catch (Exception expected) {
                 // Expected.
@@ -1198,23 +1213,35 @@
         try {
             Timer t = new Timer();
             final AtomicLong counter = new AtomicLong();
-            TimerTask task1 = new IncrementCounterTaskAndPossiblyThrowAfter(
+
+            // Schedule tasks to run:
+            // A) {In 1 millis} Increment a counter by 1 and throw an exception
+            // B) {In 100 millis} Increment a counter by 1000 (but it's not intended to be executed
+            //        because of the previous exception).
+            // We want A and B to be scheduled before A runs.
+            // We add them in reverse order.
+            // We have ~99 millis after scheduling B to schedule A. If A ran before we scheduled B
+            // we would get an exception when we came to schedule B.
+
+            TimerTask taskThatDoesntThrow = new IncrementCounterTaskAndPossiblyThrowAfter(
                     counter,
-                    true /* willThrow */);
-            TimerTask task2 = new IncrementCounterTaskAndPossiblyThrowAfter(
-                    counter,
+                    1000, /* incrementAmount */
                     false /* willThrow */);
-            t.scheduleAtFixedRate(task1, 1 /* delay */, 100 /* period */);
-            t.schedule(task2, 100 /* delay */);
+            TimerTask taskThatThrows = new IncrementCounterTaskAndPossiblyThrowAfter(
+                    counter,
+                    1,    /* incrementAmount */
+                    true  /* willThrow */);
+            t.schedule(taskThatDoesntThrow, 100 /* delay */);
+            t.scheduleAtFixedRate(taskThatThrows, 1 /* delay */, 100 /* period */);
             swallowUncaughtExceptionHandler.waitForException(1000);
             // Check the counter wasn't increased more than once (ie, the exception killed the
             // execution thread).
             assertEquals("Counter should be 1, and is: " + counter.get(), 1, counter.get());
             t.purge();
-            assertTrue("The timer should not cancel the tasks", task1.cancel());
-            assertTrue("The timer should not cancel the tasks", task2.cancel());
+            assertTrue("The timer should not cancel the tasks", taskThatDoesntThrow.cancel());
+            assertTrue("The timer should not cancel the tasks", taskThatThrows.cancel());
 
-            TimerTask task3 = new TimerTask() {
+            TimerTask otherTask = new TimerTask() {
                 @Override
                 public void run() {
                     counter.incrementAndGet();
@@ -1222,7 +1249,7 @@
             };
 
             try {
-                t.schedule(task3, 1);
+                t.schedule(otherTask, 1);
                 fail("Timer should be cancelled and no new tasks should be allowed");
             } catch (Exception expected) {
                 // Expected.