Class AbstractWireTestCase<T>

java.lang.Object
org.junit.Assert
org.apache.lucene.util.LuceneTestCase
org.elasticsearch.test.ESTestCase
org.elasticsearch.test.AbstractWireTestCase<T>
Direct Known Subclasses:
AbstractNamedWriteableTestCase, AbstractWireSerializingTestCase

public abstract class AbstractWireTestCase<T> extends ESTestCase
Standard test case for testing wire serialization. If the class being tested extends Writeable then prefer extending AbstractWireSerializingTestCase.
  • Field Details

  • Constructor Details

    • AbstractWireTestCase

      public AbstractWireTestCase()
  • Method Details

    • createTestInstance

      protected abstract T createTestInstance()
      Creates a random test instance to use in the tests. This method will be called multiple times during test execution and should return a different random instance each time it is called.
    • mutateInstance

      protected T mutateInstance(T instance) throws IOException
      Returns an instance which is mutated slightly so it should not be equal to the given instance.
      Throws:
      IOException
    • testEqualsAndHashcode

      public final void testEqualsAndHashcode()
      Tests that the equals and hashcode methods are consistent and copied versions of the instance are equal.
    • testConcurrentEquals

      public final void testConcurrentEquals() throws IOException, InterruptedException, ExecutionException
      Calls Object.equals(java.lang.Object) on equal objects on many threads and verifies they all return true. Folks tend to assume this is true about Object.equals(java.lang.Object) and it generally is. But some equals implementations violate this assumption and it's very surprising. This tries to fail when that assumption is violated.
      Throws:
      IOException
      InterruptedException
      ExecutionException
    • concurrentTest

      protected void concurrentTest(Runnable r) throws InterruptedException, ExecutionException
      Call some test on many threads in parallel.
      Throws:
      InterruptedException
      ExecutionException
    • testConcurrentHashCode

      public final void testConcurrentHashCode() throws IOException, InterruptedException, ExecutionException
      Calls Object.hashCode() on the same object on many threads and verifies they return the same result. Folks tend to assume this is true about Object.hashCode() and it generally is. But some hashCode implementations violate this assumption and it's very surprising. This tries to fail when that assumption is violated.
      Throws:
      IOException
      InterruptedException
      ExecutionException
    • testSerialization

      public final void testSerialization() throws IOException
      Test serialization and deserialization of the test instance.
      Throws:
      IOException
    • testConcurrentSerialization

      public final void testConcurrentSerialization() throws IOException, InterruptedException, ExecutionException
      Test serializing the same object on many threads always deserializes to equal instances. Folks tend to assume this is true about serialization and it generally is. But some implementations violate this assumption and it's very surprising. This tries to fail when that assumption is violated.

      Async search can serialize responses concurrently with other operations like ToXContent.toXContent(org.elasticsearch.xcontent.XContentBuilder, org.elasticsearch.xcontent.ToXContent.Params). This doesn't check that exactly, but it's close.

      Throws:
      IOException
      InterruptedException
      ExecutionException
    • assertSerialization

      protected final void assertSerialization(T testInstance) throws IOException
      Serialize the given instance and asserts that both are equal.
      Throws:
      IOException
    • assertSerialization

      protected final void assertSerialization(T testInstance, org.elasticsearch.Version version) throws IOException
      Assert that instances copied at a particular version are equal. The version is useful for sanity checking the backwards compatibility of the wire. It isn't a substitute for real backwards compatibility tests but it is *so* much faster.
      Throws:
      IOException
    • assertEqualInstances

      protected void assertEqualInstances(T expectedInstance, T newInstance)
      Assert that two instances are equal. This is intentionally not final so we can override how equality is checked.
    • copyInstance

      protected final T copyInstance(T instance) throws IOException
      Throws:
      IOException
    • copyInstance

      protected abstract T copyInstance(T instance, org.elasticsearch.Version version) throws IOException
      Copy the instance as by reading and writing using the code specific to the provided version. The version is useful for sanity checking the backwards compatibility of the wire. It isn't a substitute for real backwards compatibility tests but it is *so* much faster.
      Throws:
      IOException
    • getNamedWriteableRegistry

      protected org.elasticsearch.common.io.stream.NamedWriteableRegistry getNamedWriteableRegistry()
      Get the NamedWriteableRegistry to use when de-serializing the object. Override this method if you need to register NamedWriteables for the test object to de-serialize. By default this will return a NamedWriteableRegistry with no registered NamedWriteables