main

square/leakcanary

Last updated at: 29/12/2023 09:39

HprofHeapGraphTest.kt

TLDR

This file contains a test class called HprofHeapGraphTest which tests the parsing and consistency of class and instance indices when parsing a heap dump file. It also verifies the correct conversion of char values.

Methods

There are no methods in this file.

Classes

HprofHeapGraphTest

This class is a test class that contains three test methods:

  • class index is consistent when parsing dump twice: This test verifies that the class index remains consistent when parsing the heap dump file twice. It creates a heap dump containing a class named "SomeClass", opens the heap graph, finds the class by name, and asserts that the class is an instance of HeapClass. It then asserts that the name of the class matches the expected value.
  • instance index is consistent when parsing dump twice: This test verifies that the instance index remains consistent when parsing the heap dump file twice. It creates a heap dump containing an instance of a class named "SomeClass", opens the heap graph, finds the class by name, and gets the instance from the list of instances. It asserts that the instance is an instance of HeapInstance. It then asserts that the instance class name matches the expected value.
  • char is correctly converted back: This test verifies that the char value is correctly converted back. It creates a heap dump containing a class named "SomeClass" with a static field named "myChar" set to the value 'p'. It opens the heap graph, finds the class by name, reads the value of the static field, and asserts that the char value matches the expected value.
package shark

import org.assertj.core.api.Assertions.assertThat
import org.junit.Test
import shark.HeapObject.HeapClass
import shark.HeapObject.HeapInstance
import shark.HprofHeapGraph.Companion.openHeapGraph
import shark.ValueHolder.CharHolder

class HprofHeapGraphTest {

  @Test fun `class index is consistent when parsing dump twice`() {
    val heapDump = dump {
      "SomeClass" clazz {}
    }

    val classIndex = heapDump.openHeapGraph().use { graph ->
      graph.findClassByName("SomeClass")!!.objectIndex
    }

    heapDump.openHeapGraph().use { graph ->
      val heapObject = graph.findObjectByIndex(classIndex)
      assertThat(heapObject).isInstanceOf(HeapClass::class.java)
      heapObject as HeapClass
      assertThat(heapObject.name).isEqualTo("SomeClass")
    }
  }

  @Test fun `instance index is consistent when parsing dump twice`() {
    val heapDump = dump {
      "SomeClass" instance {}
    }

    val instanceIndex = heapDump.openHeapGraph().use { graph ->
      graph.findClassByName("SomeClass")!!.instances.single().objectIndex
    }

    heapDump.openHeapGraph().use { graph ->
      val heapObject = graph.findObjectByIndex(instanceIndex)
      assertThat(heapObject).isInstanceOf(HeapInstance::class.java)
      heapObject as HeapInstance
      assertThat(heapObject.instanceClassName).isEqualTo("SomeClass")
    }
  }

  @Test fun `char is correctly converted back`() {
    val heapDump = dump {
      "SomeClass" clazz { staticField["myChar"] = CharHolder('p') }
    }
    val myChar = heapDump.openHeapGraph().use { graph ->
      val myClass = graph.findClassByName("SomeClass")!!
      myClass.readStaticField("myChar")!!.value.asChar!!
    }
    assertThat(myChar).isEqualTo('p')
  }
}