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 ;
19using NUnit . Framework ;
210using Python . Runtime ;
311
412namespace 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