暫無描述
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

NativeTriggeredManagedExceptionsBurstJobs.cs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using NUnit.Framework;
  2. using System.Text.RegularExpressions;
  3. using Unity.Burst;
  4. using Unity.Collections;
  5. using Unity.Collections.LowLevel.Unsafe;
  6. using Unity.Jobs;
  7. using UnityEngine;
  8. using UnityEngine.TestTools;
  9. #if UNITY_2019_3_OR_NEWER
  10. namespace ExceptionsFromBurstJobs
  11. {
  12. class NativeTriggeredManagedExceptionsBurstJobs
  13. {
  14. [BurstCompile(CompileSynchronously = true)]
  15. struct RaiseMonoExceptionJob : IJob
  16. {
  17. public float output;
  18. public void Execute()
  19. {
  20. output = Time.deltaTime;
  21. }
  22. }
  23. [Test]
  24. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  25. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  26. public void RaiseMonoException()
  27. {
  28. var job = new RaiseMonoExceptionJob();
  29. LogAssert.Expect(LogType.Exception, new Regex(
  30. "UnityEngine::UnityException: get_deltaTime can only be called from the main thread." + "[\\s]*" +
  31. "Constructors and field initializers will be executed from the loading thread when loading a scene." + "[\\s]*" +
  32. "Don't use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function." + "[\\s]*" +
  33. "This Exception was thrown from a job compiled with Burst, which has limited exception support."
  34. ));
  35. job.Run();
  36. }
  37. [BurstCompile(CompileSynchronously = true)]
  38. struct RaiseInvalidOperationExceptionJob : IJob
  39. {
  40. [ReadOnly]
  41. public NativeArray<int> test;
  42. public void Execute()
  43. {
  44. test[0] = 5;
  45. }
  46. }
  47. [Test]
  48. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  49. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  50. public void RaiseInvalidOperationException()
  51. {
  52. var jobData = new RaiseInvalidOperationExceptionJob();
  53. var testArray = new NativeArray<int>(1, Allocator.Persistent);
  54. jobData.test = testArray;
  55. LogAssert.Expect(LogType.Exception, new Regex(
  56. "System::InvalidOperationException: The .+ has been declared as \\[ReadOnly\\] in the job( .+)?, but you are writing to it\\." + "[\\s]*" +
  57. "This Exception was thrown from a job compiled with Burst, which has limited exception support."
  58. ));
  59. jobData.Run();
  60. testArray.Dispose();
  61. }
  62. [BurstCompile(CompileSynchronously = true)]
  63. unsafe struct RaiseArgumentNullExceptionJob : IJob
  64. {
  65. #pragma warning disable 649
  66. [NativeDisableUnsafePtrRestriction] public void* dst;
  67. #pragma warning restore 649
  68. public void Execute()
  69. {
  70. UnsafeUtility.MemCpy(dst, null, 10);
  71. }
  72. }
  73. [Test]
  74. [UnityPlatform(RuntimePlatform.WindowsEditor, RuntimePlatform.OSXEditor, RuntimePlatform.LinuxEditor)]
  75. [Description("Requires ENABLE_UNITY_COLLECTIONS_CHECKS which is currently only enabled in the Editor")]
  76. unsafe public void RaiseArgumentNullException()
  77. {
  78. var jobData = new RaiseArgumentNullExceptionJob();
  79. jobData.dst = UnsafeUtility.Malloc(10, 4, Allocator.Temp);
  80. LogAssert.Expect(LogType.Exception, new Regex(
  81. "System.ArgumentNullException: source" + "[\\s]*" +
  82. "This Exception was thrown from a job compiled with Burst, which has limited exception support."
  83. ));
  84. jobData.Run();
  85. UnsafeUtility.Free(jobData.dst, Allocator.Temp);
  86. }
  87. }
  88. }
  89. #endif // #if UNITY_2019_3_OR_NEWER