| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -1,6 +1,7 @@ | |||
| 1 | 1 | using System; | |
| 2 | 2 | using System.Diagnostics; | |
| 3 | 3 | using System.Threading; | |
| 4 | + using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| 4 | 5 | ||
| 5 | 6 | namespace TensorFlowNET.UnitTest | |
| 6 | 7 | { | |
@@ -13,6 +14,7 @@ public class MultiThreadedUnitTestExecuter : IDisposable | |||
| 13 | 14 | { | |
| 14 | 15 | public int ThreadCount { get; } | |
| 15 | 16 | public Thread[] Threads { get; } | |
| 17 | + public Exception[] Exceptions { get; } | ||
| 16 | 18 | private readonly SemaphoreSlim barrier_threadstarted; | |
| 17 | 19 | private readonly ManualResetEventSlim barrier_corestart; | |
| 18 | 20 | private readonly SemaphoreSlim done_barrier2; | |
@@ -57,6 +59,7 @@ public MultiThreadedUnitTestExecuter(int threadCount) | |||
| 57 | 59 | throw new ArgumentOutOfRangeException(nameof(threadCount)); | |
| 58 | 60 | ThreadCount = threadCount; | |
| 59 | 61 | Threads = new Thread[ThreadCount]; | |
| 62 | + Exceptions = new Exception[ThreadCount]; | ||
| 60 | 63 | done_barrier2 = new SemaphoreSlim(0, threadCount); | |
| 61 | 64 | barrier_corestart = new ManualResetEventSlim(); | |
| 62 | 65 | barrier_threadstarted = new SemaphoreSlim(0, threadCount); | |
@@ -72,28 +75,53 @@ public void Run(params MultiThreadedTestDelegate[] workloads) | |||
| 72 | 75 | ||
| 73 | 76 | if (ThreadCount == 1) | |
| 74 | 77 | { | |
| 78 | + Exception ex = null; | ||
| 75 | 79 | new Thread(() => | |
| 76 | 80 | { | |
| 77 | - workloads[0](0); | ||
| 78 | - done_barrier2.Release(1); | ||
| 81 | + try | ||
| 82 | + { | ||
| 83 | + workloads[0](0); | ||
| 84 | + } catch (Exception e) | ||
| 85 | + { | ||
| 86 | + if (Debugger.IsAttached) | ||
| 87 | + throw; | ||
| 88 | + ex = e; | ||
| 89 | + } finally | ||
| 90 | + { | ||
| 91 | + done_barrier2.Release(1); | ||
| 92 | + } | ||
| 79 | 93 | }).Start(); | |
| 80 | - | ||
| 94 | + | ||
| 81 | 95 | done_barrier2.Wait(); | |
| 96 | + | ||
| 97 | + if (ex != null) | ||
| 98 | + throw new Exception($"Thread 0 has failed: ", ex); | ||
| 99 | + | ||
| 82 | 100 | PostRun?.Invoke(this); | |
| 83 | 101 | ||
| 84 | 102 | return; | |
| 85 | 103 | } | |
| 86 | 104 | ||
| 87 | 105 | //thread core | |
| 88 | - void ThreadCore(MultiThreadedTestDelegate core, int threadid) | ||
| 106 | + Exception ThreadCore(MultiThreadedTestDelegate core, int threadid) | ||
| 89 | 107 | { | |
| 90 | 108 | barrier_threadstarted.Release(1); | |
| 91 | 109 | barrier_corestart.Wait(); | |
| 92 | - | ||
| 93 | 110 | //workload | |
| 94 | - core(threadid); | ||
| 111 | + try | ||
| 112 | + { | ||
| 113 | + core(threadid); | ||
| 114 | + } catch (Exception e) | ||
| 115 | + { | ||
| 116 | + if (Debugger.IsAttached) | ||
| 117 | + throw; | ||
| 118 | + return e; | ||
| 119 | + } finally | ||
| 120 | + { | ||
| 121 | + done_barrier2.Release(1); | ||
| 122 | + } | ||
| 95 | 123 | ||
| 96 | - done_barrier2.Release(1); | ||
| 124 | + return null; | ||
| 97 | 125 | } | |
| 98 | 126 | ||
| 99 | 127 | //initialize all threads | |
@@ -103,15 +131,15 @@ void ThreadCore(MultiThreadedTestDelegate core, int threadid) | |||
| 103 | 131 | for (int i = 0; i < ThreadCount; i++) | |
| 104 | 132 | { | |
| 105 | 133 | var i_local = i; | |
| 106 | - Threads[i] = new Thread(() => ThreadCore(workload, i_local)); | ||
| 134 | + Threads[i] = new Thread(() => Exceptions[i_local] = ThreadCore(workload, i_local)); | ||
| 107 | 135 | } | |
| 108 | 136 | } else | |
| 109 | 137 | { | |
| 110 | 138 | for (int i = 0; i < ThreadCount; i++) | |
| 111 | 139 | { | |
| 112 | 140 | var i_local = i; | |
| 113 | 141 | var workload = workloads[i_local % workloads.Length]; | |
| 114 | - Threads[i] = new Thread(() => ThreadCore(workload, i_local)); | ||
| 142 | + Threads[i] = new Thread(() => Exceptions[i_local] = ThreadCore(workload, i_local)); | ||
| 115 | 143 | } | |
| 116 | 144 | } | |
| 117 | 145 | ||
@@ -126,6 +154,11 @@ void ThreadCore(MultiThreadedTestDelegate core, int threadid) | |||
| 126 | 154 | //wait for threads to finish | |
| 127 | 155 | for (int i = 0; i < ThreadCount; i++) done_barrier2.Wait(); | |
| 128 | 156 | ||
| 157 | + //handle fails | ||
| 158 | + for (int i = 0; i < ThreadCount; i++) | ||
| 159 | + if (Exceptions[i] != null) | ||
| 160 | + throw new Exception($"Thread {i} has failed: ", Exceptions[i]); | ||
| 161 | + | ||
| 129 | 162 | //checks after ended | |
| 130 | 163 | PostRun?.Invoke(this); | |
| 131 | 164 | } | |
| Back | FazBrowse Home | New Git URL |
0 commit comments