Tweak/Fix ObjectTest.

This CL combines misc tweaks/refactorings and fixes.

It's not clear whether ObjectTest#test_notify() is flaky
because 200msec isn't enough time for the other thread to
wake up, or if there is some other bugs. For example, the
test previously didn't deal correctly with spurious wake-ups.

Fixes:
 - The tests were not dealing with spurious wake-up of the wait()ing
   background threads. This CL introduces the field
   outstandingNotifications to limit the number of tests that
   may wake up to how many were notified.
 - Increase the amount of time that test_notify() waits for the
   notify'ed thread to take action from 200msec to 400msec.
 - Replaced background exception signaling via a global field
   backgroundException that is checked during tearDown, rather
   than by setting status. The field is also guarded by lock to
   avoid introducing a use of volatile/AtomicReference to the
   test.

Tweaks:
 - Replace a few instances of assertTrue(..., ... == ...) with
   assertEquals() or assertSame().
 - When waiting for something to happen, sleep for a short while
   first and only sleep more if the thing hasn't happened yet.
   (Normally this kind of code would be rewritten using a
   CountDownLatch, but I didn't want to change the test by
   introducing other classes that affect threading semantics).
 - make TestThread extend Thread rather than Runnable.
 - Move the field status into the remaining to Thread classes
   that need it.
 - Rename obj1 to lock for use by the tests that use a synchronized
   block, and move obj1, obj2 into the tests that need those.
 - Slight reformatting for conciseness.
 - Drop some redundant lines.

Test: atest --iterations 20 \
      CtsLibcoreTestCases:org.apache.harmony.tests.java.lang.ObjectTest
Test: Ran the following on a Pixel 2; each of the 500 test runs took
      a bit over 2 seconds to complete because we sleep for at least
      100msec waiting for each of the 20 background threads to wake
      up and exit.
      atest --iterations 100 \
      CtsLibcoreTestCases:org.apache.harmony.tests.java.lang.ObjectTest#test_notify

Bug: 144254071
Change-Id: I4cea62b2abde9c06f44e0c8754003ead84d00c00
1 file changed