Açıklama Yok
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.8KB

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