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 ;
1
9
using NUnit . Framework ;
2
10
using Python . Runtime ;
3
11
4
12
namespace Python . EmbeddingTest
5
13
{
6
-
7
14
// As the SetUpFixture, the OneTimeTearDown of this class is executed after
8
15
// all tests have run.
9
16
[ SetUpFixture ]
10
17
public class GlobalTestsSetup
11
18
{
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
+
12
33
[ OneTimeTearDown ]
13
34
public void FinalCleanup ( )
14
35
{
@@ -17,5 +38,48 @@ public void FinalCleanup()
17
38
PythonEngine . Shutdown ( ) ;
18
39
}
19
40
}
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
+ }
20
84
}
21
85
}
0 commit comments