THIS IS A TEST INSTANCE. ALL YOUR CHANGES WILL BE LOST!!!!
...
Examples: SleepDistributedTest.java, MultiThreadedIntegrationTest
...
- Expected vs unexpected exceptions in tests
- Never catch unexpected exceptions. That is bad... way bad...
- Special case: Asynchronous actions may need to catch checked exceptions and use ErrorCollector
- Use method throws clause for unexpected exceptions
- The alternative serves to make failures more opaque.
- Fixing test failures (as you will have to do at some point) is all about visibility into the problems of the test.
- Use AssertJ catchThrowable or assertThatThrown for expected exceptions
- Avoid using JUnit expected exception support such as ExpectedException Rule or Test annotation expected element. Example: @Test(expected = Exception.class)
- Remember to catch specific exceptions, not whole parent classes. If someone adds an additional exception, will it break your test?
Examples: ErrorHandlingTest, ExpectedExceptionTest
Sometimes you think they’re helping...
...
- Use ExecutorServiceRule or DistributedExcutorServiceRule
- Provides debugging support for hangs
- Cleans up threads on tear down
- Test task code should be interruptible
- Use Future or CompletableFuture for submitted runnable/callable
- Always invoke get() on any Future or CompletableFuture
Examples: AsyncErrorHandlingTest, ExecutorSyncHangIntegrationTest, ExecutorLockHangIntegrationTest, ExecutorErrorHandlingDistributedTest
Handling exceptions in callbacks
- Avoid catching exception and setting some test state to check later
- Use ErrorCollector in unit and integration tests
- Use SharedErrorCollector in distributed tests
Examples: AsyncErrorHandlingIntegrationTest, AsyncErrorHandlingDistributedTest
AsyncInvocation usage
- Timeout now gets a remote stack trace to use as the cause and dumps stack traces for that JVM’s threads
- Always use await() or get()
- Both check and throw any remote exceptions
- Both use GeodeAwaitility Timeout and will throw TimeoutException if it’s exceeded
- Use await() for Void types and get() when expecting a non-null value
Examples: AsyncInvocationDistributedTest, AsyncInvocationHangDistributedTest
Know your DUnit Rules
- DistributedRule -- simply launches DUnit and greps for suspect strings after each test method
- DistributedExecutorServiceRule -- provides an ExecutorService for all VMs
- DistributedRestoreSystemProperties -- restores system properties in all VMs
- SharedCountersRule -- shares counters across all VMs
- SharedErrorCollector -- shares one JUnit ErrorCollector across all VMs
...