Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Unit testing parallel scheduled jobs?

Discussion in 'Entity Component System' started by LuckyWonton, Jul 28, 2020.

  1. LuckyWonton

    LuckyWonton

    Joined:
    Feb 28, 2014
    Posts:
    19
    I am trying to set up the most basic unit test possible. I have similar tests for existing code already for Unity3D OOP. All I want to do is run a job once and check and see if it works.

    Game/Physics/GasHeatDiffusionSystem.cs
    Code (csharp):
    1. using System.Diagnostics;
    2. using Unity.Collections;
    3. using Unity.Entities;
    4. using Unity.Jobs;
    5.  
    6. namespace Game.Physics
    7. {
    8.     /// <summary>
    9.     /// System that evenly distributes heat between gas elements in an entity.
    10.     /// </summary>
    11.     public class GasHeatDiffusionSystem : SystemBase
    12.     {
    13.         EndSimulationEntityCommandBufferSystem m_EndSimulationEcbSystem;
    14.         protected override void OnCreate() {
    15.             base.OnCreate();
    16.             m_EndSimulationEcbSystem = World.GetOrCreateSystem<EndSimulationEntityCommandBufferSystem>();
    17.         }
    18.  
    19.         protected override void OnUpdate() {
    20.             var entityCommandBuffer = m_EndSimulationEcbSystem.CreateCommandBuffer().AsParallelWriter();
    21.  
    22.             // find all jars that have been made inhomogenous by content changes
    23.             Entities
    24.                 .WithAll<GasHeatInhomogeneousTag>()
    25.                 .ForEach((Entity entity, int entityInQueryIndex, DynamicBuffer<ChemicalComposition> contents) => {
    26.                     // Removed code for sake of testing.
    27.                     // Heat is now homogenous, queue entity to remove tag.
    28.                     entityCommandBuffer.RemoveComponent<GasHeatInhomogeneousTag>(entityInQueryIndex, entity);
    29.                 })
    30.                 .ScheduleParallel();
    31.  
    32.             m_EndSimulationEcbSystem.AddJobHandleForProducer(this.Dependency);
    33.         }
    34.     }
    35. }
    Tests/ChemicalTestScript.cs
    Code (csharp):
    1. using NUnit.Framework;
    2. using Unity.Entities.Tests;
    3. using Game.Physics;
    4. using Unity.Entities;
    5.  
    6. namespace Tests
    7. {
    8.     [TestFixture]
    9.     public class ChemicalTestScript : ECSTestsFixture
    10.     {
    11.         const float MolesInLiterOfWater = 55.5084343f;
    12.  
    13.         #region Chemical References
    14.         Chemical Nitrogen() {
    15.             return new Chemical {
    16.                 MeltingPoint = 63.15f,
    17.                 BoilingPoint = 77.355f,
    18.                 MolarMass = 28.014f,
    19.                 RelativeDensity = 0.804f,
    20.                 HeatCapacity = 29.124f,
    21.                 ThermalConductivity = 0.02583f
    22.             };
    23.         }
    24.  
    25.         Chemical Oxygen() {
    26.             return new Chemical {
    27.                 MeltingPoint = 54.36f,
    28.                 BoilingPoint = 90.188f,
    29.                 MolarMass = 31.988f,
    30.                 RelativeDensity = 1.14f,
    31.                 HeatCapacity = 29.378f,
    32.                 ThermalConductivity = 0.02658f
    33.             };
    34.         }
    35.  
    36.         Chemical Water() {
    37.             return new Chemical {
    38.                 MeltingPoint = 273.15f,
    39.                 BoilingPoint = 373.13f,
    40.                 MolarMass = 18.01528f,
    41.                 RelativeDensity = 1f,
    42.                 HeatCapacity = 75.385f,
    43.                 ThermalConductivity = 0.6065f
    44.             };
    45.         }
    46.         #endregion
    47.  
    48.         [Test]
    49.         public void GasHeatDiffusionSystemDiffusionPasses() {
    50.             var entity = m_Manager.CreateEntity(
    51.                 typeof(GasHeatInhomogeneousTag),
    52.                 typeof(ChemicalComposition)
    53.             );
    54.  
    55.             var buffer = m_Manager.GetBuffer<ChemicalComposition>(entity);
    56.             buffer.Add(new ChemicalComposition {
    57.                 Chemical = Nitrogen(),
    58.                 Matter = 79f,
    59.                 Temp = Temp.Boiling
    60.             });
    61.             buffer.Add(new ChemicalComposition {
    62.                 Chemical = Oxygen(),
    63.                 Matter = 21f,
    64.                 Temp = Temp.Room
    65.             });
    66.  
    67.             m_Manager.World.CreateSystem<GasHeatDiffusionSystem>();
    68.             m_Manager.World.Update();
    69.  
    70.             Assert.IsFalse(m_Manager.HasComponent<GasHeatInhomogeneousTag>(entity));
    71.             Assert.AreEqual(buffer[0].Temp, buffer[1].Temp);
    72.         }
    73.     }
    74. }
    This fails right at the first hurdle. The `GasHeatInhomogenousTag` is not removed from the entity. I have tried many different system types and ways of calling the update, none work.

    m_Manager.World.CreateSystem<GasHeatDiffusionSystem>().Update;
    World.CreateSystem<GasHeatDiffusionSystem>().Update();
    World.Update();

    etc. nothing works. I cannot make a system run, conclude it, and see the results. Why? This was so easy to do before.
     
    florianhanke likes this.
  2. Joachim_Ante

    Joachim_Ante

    Unity Technologies

    Joined:
    Mar 16, 2005
    Posts:
    5,203
    You need to explicitly update the EndSimulationEntityCommandBufferSystem, because thats where the ECB you are registering will be played back.