Search Unity

DefaultWorldInitializationTests.Initialize_ShouldLogNothing leaks global state - InjectionHookSuppor

Discussion in 'Entity Component System' started by Deleted User, Jul 13, 2018.

  1. Deleted User

    Deleted User

    Guest

    The project I'm writing code for does not use the
    DefaultWorldInitialization
    (DWI) class but does its own world set up. However, we still need to inject and use
    GameObjectArray
    s. So we register our own hooks to basically re-implement this:

    Code (CSharp):
    1.  
    2.             InjectionHookSupport.RegisterHook(new GameObjectArrayInjectionHook());
    3.             InjectionHookSupport.RegisterHook(new TransformAccessArrayInjectionHook());
    4.             InjectionHookSupport.RegisterHook(new ComponentArrayInjectionHook());
    5.  
    I'm writing some tests that confirm that we inject these sort of hooks properly, so I have a base case that asserts "if we don't set up these injection hooks, there will be an error".

    If I run this test by itself it does not fail, but if the
    Unity.Entities.Tests.DefaultWorldInitializationTests.Initialize_ShouldLogNothing
    test runs, the
    DefaultWorldInitialization.Initialize
    executes, the injection hooks get registered, but they do not get cleaned up.

    So my tests' assertions of "there will be an error" fail as the injection hooks are set up by the DWI instead.

    There are various workarounds:
    • to not run the Unity.Entities.Tests tests
    • to clean up hooks manually through reflection in the test that fails
    For this project I chose option 2; because running the Unity.Entities.Tests allows us to see if we may have affected the global state ourselves with our custom setup.

    I think a nice fix would be to add a Cleanup functionality within DWI and call that in the TearDown of DWITests.

    Another would be to have world-specific injection hooks (this could be quite nice), or to extend InjectionHookSupport to restore original state.

    Additionally I am interested to see what the team's thoughts are on InjectionHookSupport in general. Are there any plans for it to change, for example?