Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 848e6ba

Browse files
committed
if tests are taking too long, create a memory dump and abort
1 parent 9fb8436 commit 848e6ba

File tree

1 file changed

+65
-1
lines changed

1 file changed

+65
-1
lines changed

src/embed_tests/GlobalTestsSetup.cs

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,35 @@
1+
using System;
2+
using System.ComponentModel;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Runtime.InteropServices;
6+
using System.Threading;
7+
8+
using Microsoft.Win32.SafeHandles;
19
using NUnit.Framework;
210
using Python.Runtime;
311

412
namespace Python.EmbeddingTest
513
{
6-
714
// As the SetUpFixture, the OneTimeTearDown of this class is executed after
815
// all tests have run.
916
[SetUpFixture]
1017
public class GlobalTestsSetup
1118
{
19+
[OneTimeSetUp]
20+
public void GlobalSetup()
21+
{
22+
new Thread(() =>
23+
{
24+
Thread.Sleep(TimeSpan.FromSeconds(30));
25+
UploadDump();
26+
Console.Error.WriteLine("Test has been running for too long. Created memory dump");
27+
Environment.Exit(1);
28+
}) {
29+
IsBackground = true,
30+
}.Start();
31+
}
32+
1233
[OneTimeTearDown]
1334
public void FinalCleanup()
1435
{
@@ -17,5 +38,48 @@ public void FinalCleanup()
1738
PythonEngine.Shutdown();
1839
}
1940
}
41+
42+
static void UploadDump()
43+
{
44+
var self = Process.GetCurrentProcess();
45+
46+
const string dumpPath = "memory.dmp";
47+
48+
// ensure DbgHelp is loaded
49+
MiniDumpWriteDump(IntPtr.Zero, 0, IntPtr.Zero, MiniDumpType.Normal, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero);
50+
51+
using (var fileHandle = CreateFile(dumpPath, FileAccess.Write, FileShare.Write,
52+
securityAttrs: IntPtr.Zero, dwCreationDisposition: FileMode.Create, dwFlagsAndAttributes: 0,
53+
hTemplateFile: IntPtr.Zero))
54+
{
55+
if (fileHandle.IsInvalid)
56+
throw new Win32Exception();
57+
58+
if (!MiniDumpWriteDump(self.Handle, self.Id, fileHandle.DangerousGetHandle(), MiniDumpType.Normal,
59+
causeException: IntPtr.Zero, userStream: IntPtr.Zero, callback: IntPtr.Zero))
60+
throw new Win32Exception();
61+
}
62+
63+
Process.Start("appveyor", arguments: $"PushArtifact -Path \"{dumpPath}\"").WaitForExit();
64+
}
65+
66+
[DllImport("kernel32", CharSet = CharSet.Auto, SetLastError = true)]
67+
private static extern SafeFileHandle CreateFile(
68+
string lpFileName,
69+
FileAccess dwDesiredAccess,
70+
FileShare dwShareMode,
71+
IntPtr securityAttrs,
72+
FileMode dwCreationDisposition,
73+
int dwFlagsAndAttributes,
74+
IntPtr hTemplateFile);
75+
[DllImport("DbgHelp", SetLastError = true)]
76+
static extern bool MiniDumpWriteDump(IntPtr hProcess,
77+
int processID, IntPtr hFile, MiniDumpType dumpType,
78+
IntPtr causeException, IntPtr userStream, IntPtr callback);
79+
80+
enum MiniDumpType
81+
{
82+
Normal,
83+
}
2084
}
2185
}

0 commit comments

Comments
 (0)