"""Tests related to the scheduling information feature"""
import omni.graph.core as og
import omni.graph.core.tests as og_tests


# ==============================================================================================================
class TestSchedulingHints(og_tests.OmniGraphTestCase):
    """Testing to ensure scheduling information work"""

    # ----------------------------------------------------------------------
    async def test_scheduling_hints(self):
        """Test the scheduling information embedded in .ogn files"""
        controller = og.Controller()
        keys = og.Controller.Keys

        (_, (list_node, string_node), _, _,) = controller.edit(
            "/TestGraph",
            {
                keys.CREATE_NODES: [
                    ("AsList", "omni.graph.test.TestSchedulingHintsList"),
                    ("AsString", "omni.graph.test.TestSchedulingHintsString"),
                ]
            },
        )

        scheduling_hints = list_node.get_node_type().get_scheduling_hints()
        scheduling_hints_2 = og.ISchedulingHints2(scheduling_hints)
        self.assertEqual(og.eThreadSafety.E_SAFE, scheduling_hints.thread_safety)
        self.assertEqual(og.eAccessType.E_NONE, scheduling_hints.get_data_access(og.eAccessLocation.E_GLOBAL))
        self.assertEqual(og.eAccessType.E_NONE, scheduling_hints.get_data_access(og.eAccessLocation.E_STATIC))
        self.assertEqual(og.eAccessType.E_NONE, scheduling_hints.get_data_access(og.eAccessLocation.E_TOPOLOGY))
        self.assertEqual(og.eAccessType.E_NONE, scheduling_hints.get_data_access(og.eAccessLocation.E_USD))
        self.assertEqual(og.eComputeRule.E_DEFAULT, scheduling_hints.compute_rule)
        if scheduling_hints_2:
            self.assertEqual(og.ePurityStatus.E_IMPURE, scheduling_hints_2.purity_status)
        scheduling_hints.thread_safety = og.eThreadSafety.E_UNKNOWN
        list_node.get_node_type().set_scheduling_hints(scheduling_hints)
        self.assertEqual(og.eThreadSafety.E_UNKNOWN, scheduling_hints.thread_safety)
        scheduling_hints.compute_rule = og.eComputeRule.E_ON_REQUEST
        list_node.get_node_type().set_scheduling_hints(scheduling_hints)
        self.assertEqual(og.eComputeRule.E_ON_REQUEST, scheduling_hints.compute_rule)

        # restore the node to its default values
        scheduling_hints.thread_safety = og.eThreadSafety.E_SAFE
        scheduling_hints.compute_rule = og.eComputeRule.E_DEFAULT
        list_node.get_node_type().set_scheduling_hints(scheduling_hints)

        scheduling_hints = string_node.get_node_type().get_scheduling_hints()
        scheduling_hints_2 = og.ISchedulingHints2(scheduling_hints)
        self.assertEqual(og.eThreadSafety.E_UNSAFE, scheduling_hints.thread_safety)
        self.assertEqual(og.eAccessType.E_READ, scheduling_hints.get_data_access(og.eAccessLocation.E_GLOBAL))
        self.assertEqual(og.eAccessType.E_READ_WRITE, scheduling_hints.get_data_access(og.eAccessLocation.E_STATIC))
        self.assertEqual(og.eAccessType.E_WRITE, scheduling_hints.get_data_access(og.eAccessLocation.E_TOPOLOGY))
        self.assertEqual(og.eAccessType.E_READ_WRITE, scheduling_hints.get_data_access(og.eAccessLocation.E_USD))
        self.assertEqual(og.eComputeRule.E_ON_REQUEST, scheduling_hints.compute_rule)
        if scheduling_hints_2:
            self.assertEqual(og.ePurityStatus.E_IMPURE, scheduling_hints_2.purity_status)

        scheduling_hints.set_data_access(og.eAccessLocation.E_GLOBAL, og.eAccessType.E_WRITE)
        scheduling_hints.set_data_access(og.eAccessLocation.E_STATIC, og.eAccessType.E_NONE)
        scheduling_hints.set_data_access(og.eAccessLocation.E_TOPOLOGY, og.eAccessType.E_READ_WRITE)
        scheduling_hints.set_data_access(og.eAccessLocation.E_USD, og.eAccessType.E_READ)
        scheduling_hints.compute_rule = og.eComputeRule.E_DEFAULT
        if scheduling_hints_2:
            scheduling_hints_2.purity_status = og.ePurityStatus.E_PURE
        string_node.get_node_type().set_scheduling_hints(scheduling_hints)

        self.assertEqual(og.eAccessType.E_WRITE, scheduling_hints.get_data_access(og.eAccessLocation.E_GLOBAL))
        self.assertEqual(og.eAccessType.E_NONE, scheduling_hints.get_data_access(og.eAccessLocation.E_STATIC))
        self.assertEqual(og.eAccessType.E_READ_WRITE, scheduling_hints.get_data_access(og.eAccessLocation.E_TOPOLOGY))
        self.assertEqual(og.eAccessType.E_READ, scheduling_hints.get_data_access(og.eAccessLocation.E_USD))
        self.assertEqual(og.eComputeRule.E_DEFAULT, scheduling_hints.compute_rule)
        if scheduling_hints_2:
            self.assertEqual(og.ePurityStatus.E_PURE, scheduling_hints_2.purity_status)

        # restore to default values
        scheduling_hints.set_data_access(og.eAccessLocation.E_GLOBAL, og.eAccessType.E_READ)
        scheduling_hints.set_data_access(og.eAccessLocation.E_STATIC, og.eAccessType.E_READ_WRITE)
        scheduling_hints.set_data_access(og.eAccessLocation.E_TOPOLOGY, og.eAccessType.E_WRITE)
        scheduling_hints.set_data_access(og.eAccessLocation.E_USD, og.eAccessType.E_READ_WRITE)
        scheduling_hints.compute_rule = og.eComputeRule.E_ON_REQUEST
        if scheduling_hints_2:
            scheduling_hints_2.purity_status = og.ePurityStatus.E_IMPURE
        string_node.get_node_type().set_scheduling_hints(scheduling_hints)
