|
| 1 | +// |
| 2 | +// DebuggerSessionTests.cs |
| 3 | +// |
| 4 | +// Author: |
| 5 | +// Greg Munn <gregm@microsoft.com> |
| 6 | +// |
| 7 | +// Copyright (c) 2019 Microsoft |
| 8 | +// |
| 9 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 10 | +// of this software and associated documentation files (the "Software"), to deal |
| 11 | +// in the Software without restriction, including without limitation the rights |
| 12 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 13 | +// copies of the Software, and to permit persons to whom the Software is |
| 14 | +// furnished to do so, subject to the following conditions: |
| 15 | +// |
| 16 | +// The above copyright notice and this permission notice shall be included in |
| 17 | +// all copies or substantial portions of the Software. |
| 18 | +// |
| 19 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 20 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 21 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 22 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 23 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 24 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 25 | +// THE SOFTWARE. |
| 26 | + |
| 27 | +using System; |
| 28 | +using System.Threading; |
| 29 | +using System.Threading.Tasks; |
| 30 | + |
| 31 | +using Mono.Debugging.Client; |
| 32 | + |
| 33 | +using NUnit.Framework; |
| 34 | + |
| 35 | +namespace Mono.Debugging.Tests |
| 36 | +{ |
| 37 | + [TestFixture] |
| 38 | + public class DebuggerSessionTests |
| 39 | + { |
| 40 | + TestDebuggerSession session; |
| 41 | + |
| 42 | + [TestFixtureSetUp] |
| 43 | + public virtual void SetUp () |
| 44 | + { |
| 45 | + session = new TestDebuggerSession (); |
| 46 | + } |
| 47 | + |
| 48 | + [TestFixtureTearDown] |
| 49 | + public virtual void TearDown () |
| 50 | + { |
| 51 | + session.Dispose (); |
| 52 | + } |
| 53 | + |
| 54 | + [Test] |
| 55 | +#pragma warning disable VSTHRD200 // Use "Async" suffix for async methods |
| 56 | + public async Task WhenQueryigBreakpoints_ThenDoNotDeadlockDispatchedMethods() |
| 57 | +#pragma warning restore VSTHRD200 // Use "Async" suffix for async methods |
| 58 | + { |
| 59 | + // thread 1 cancels debugging, which triggers hot reload to clear breakpoints |
| 60 | + // thread 2 dispatches the OnExit method which is call via Dispatch and uses the same lock |
| 61 | + |
| 62 | + var thread1 = new TaskCompletionSource<bool> (); |
| 63 | + Task thread2 = null; |
| 64 | + |
| 65 | + session.HandleOnExit = () => { |
| 66 | + // we are on a background thread via session.Dispatch |
| 67 | + thread2 = Task.Run (() => { |
| 68 | + var bk = session.Breakpoints; |
| 69 | + thread1.TrySetResult (true); |
| 70 | + }); |
| 71 | + |
| 72 | + // mimic the cause of the deadlock by waiting here for a while |
| 73 | + // we don't really want to deadlock, just long enough that we should have |
| 74 | + // completed the call to breakpoints first. |
| 75 | + Thread.Sleep (1000); |
| 76 | + |
| 77 | + thread1.TrySetResult (false); |
| 78 | + }; |
| 79 | + |
| 80 | + session.Exit (); |
| 81 | + |
| 82 | + var result= await thread1.Task; |
| 83 | + await thread2; |
| 84 | + |
| 85 | + Assert.IsTrue (result, "the call to get breakpoints should have completed first"); |
| 86 | + } |
| 87 | + |
| 88 | + class TestDebuggerSession : DebuggerSession |
| 89 | + { |
| 90 | + public Action HandleOnExit; |
| 91 | + |
| 92 | + protected override void OnAttachToProcess (long processId) |
| 93 | + { |
| 94 | + throw new NotImplementedException (); |
| 95 | + } |
| 96 | + |
| 97 | + protected override void OnContinue () |
| 98 | + { |
| 99 | + throw new NotImplementedException (); |
| 100 | + } |
| 101 | + |
| 102 | + protected override void OnDetach () |
| 103 | + { |
| 104 | + throw new NotImplementedException (); |
| 105 | + } |
| 106 | + |
| 107 | + protected override void OnEnableBreakEvent (BreakEventInfo eventInfo, bool enable) |
| 108 | + { |
| 109 | + throw new NotImplementedException (); |
| 110 | + } |
| 111 | + |
| 112 | + protected override void OnExit () |
| 113 | + { |
| 114 | + // this is called in dispatch |
| 115 | + HandleOnExit?.Invoke (); |
| 116 | + //throw new NotImplementedException (); |
| 117 | + } |
| 118 | + |
| 119 | + protected override void OnFinish () |
| 120 | + { |
| 121 | + throw new NotImplementedException (); |
| 122 | + } |
| 123 | + |
| 124 | + protected override ProcessInfo [] OnGetProcesses () |
| 125 | + { |
| 126 | + throw new NotImplementedException (); |
| 127 | + } |
| 128 | + |
| 129 | + protected override Backtrace OnGetThreadBacktrace (long processId, long threadId) |
| 130 | + { |
| 131 | + throw new NotImplementedException (); |
| 132 | + } |
| 133 | + |
| 134 | + protected override ThreadInfo [] OnGetThreads (long processId) |
| 135 | + { |
| 136 | + throw new NotImplementedException (); |
| 137 | + } |
| 138 | + |
| 139 | + protected override BreakEventInfo OnInsertBreakEvent (BreakEvent breakEvent) |
| 140 | + { |
| 141 | + throw new NotImplementedException (); |
| 142 | + } |
| 143 | + |
| 144 | + protected override void OnNextInstruction () |
| 145 | + { |
| 146 | + throw new NotImplementedException (); |
| 147 | + } |
| 148 | + |
| 149 | + protected override void OnNextLine () |
| 150 | + { |
| 151 | + throw new NotImplementedException (); |
| 152 | + } |
| 153 | + |
| 154 | + protected override void OnRemoveBreakEvent (BreakEventInfo eventInfo) |
| 155 | + { |
| 156 | + throw new NotImplementedException (); |
| 157 | + } |
| 158 | + |
| 159 | + protected override void OnRun (DebuggerStartInfo startInfo) |
| 160 | + { |
| 161 | + throw new NotImplementedException (); |
| 162 | + } |
| 163 | + |
| 164 | + protected override void OnSetActiveThread (long processId, long threadId) |
| 165 | + { |
| 166 | + throw new NotImplementedException (); |
| 167 | + } |
| 168 | + |
| 169 | + protected override void OnStepInstruction () |
| 170 | + { |
| 171 | + throw new NotImplementedException (); |
| 172 | + } |
| 173 | + |
| 174 | + protected override void OnStepLine () |
| 175 | + { |
| 176 | + throw new NotImplementedException (); |
| 177 | + } |
| 178 | + |
| 179 | + protected override void OnStop () |
| 180 | + { |
| 181 | + throw new NotImplementedException (); |
| 182 | + } |
| 183 | + |
| 184 | + protected override void OnUpdateBreakEvent (BreakEventInfo eventInfo) |
| 185 | + { |
| 186 | + throw new NotImplementedException (); |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | +} |
0 commit comments