8000 Add more domain reload tests by BadSingleton · Pull Request #1269 · pythonnet/pythonnet · GitHub
[go: up one dir, main page]

Skip to content

Add more domain reload tests #1269

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
(wip)
  • Loading branch information
BadSingleton committed Oct 27, 2020
commit a5cf8a4443c8ddd738304b1aa0e53aeb7a427049
78 changes: 43 additions & 35 deletions src/domain_tests/TestRunner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,34 +33,39 @@ public class TestClass
}}
}}";

/// <summary>
/// The Python code that accesses the test class in the first step of the run
/// </summary>
const string PythonCodeStep1 = @"import clr
clr.AddReference('TestClass')
import sys
from TestNamespace import TestClass
foo = None
def do_work():
global foo
obj = TestClass()
foo = TestClass.{0}
sys.my_obj = foo
print(sys.my_obj)
";
// /// <summary>
// /// The Python code that accesses the test class in the first step of the run
// /// </summary>
// const string PythonCodeStep1 = @"import clr
// clr.AddReference('TestClass')
// import sys
// from TestNamespace import TestClass
// foo = None
// def do_work():
// global foo
// obj = TestClass()
// foo = TestClass.{0}
// sys.my_obj = foo
// print(sys.my_obj)
// ";

/// <summary>
/// The Python code that accesses the test class
/// </summary>
const string PythonCodeStep2 = @"import clr
const string PythonCodeStep = @"import clr
clr.AddReference('TestClass')
import sys
from TestNamespace import TestClass
import TestNamespace
foo = None
def do_work():
global foo
obj = TestClass()
foo = TestClass.{0}
sys.my_obj = foo
print(sys.my_obj)

def test_work():
print(foo)
print(sys.my_obj)
";

/// <summary>
Expand All @@ -76,18 +81,19 @@ class CaseRunner
{{
public static int Main()
{{
PythonEngine.Initialize(mode:{0});
try
{{
using (Py.GIL())
{{
// Because the generated assemblies are in the $TEMP folder, add it to the path
var temp = Path.GetTempPath();
dynamic sys = Py.Import(""sys"");
sys.path.append(new PyString(temp));
dynamic test_mod = Py.Import(""domain_test_module.mod"");
test_mod.do_work();
}}
PythonEngine.Initialize(mode:{0});
// using (Py.GIL())
// {{
// // Because the generated assemblies are in the $TEMP folder, add it to the path
// // var temp = Path.GetTempPath();
// // dynamic sys = Py.Import(""sys"");
// // sys.path.append(new PyString(temp));
// // dynamic test_mod = Py.Import(""domain_test_module.mod"");
// // Console.WriteLine(""verb: {1}"");
// // test_mod.{1}_work();
// }}
PythonEngine.Shutdown();
}}
catch (PythonException pe)
Expand All @@ -105,7 +111,8 @@ public static int Main(string[] args)
{
if (args.Length < 3)
{
args = new string[] { "public static int TestMember = 2;", "", "TestMember" };
// args = new string[] { "public static int TestMember = 2;", "", "TestMember" };
args = new string[] { @"public static void TestMethod() {Console.WriteLine(""from test method"");}", "", "TestMethod()" };
// return 123;
}
Console.WriteLine($"Testing with arguments: {string.Join(", ", args)}");
Expand All @@ -118,19 +125,20 @@ public static int Main(string[] args)

File.Copy(PythonDllLocation, tempFolderPython);

CreatePythonModule(string.Format(PythonCodeStep1, args[2]));
var runnerAssembly = CreateCaseRunnerAssembly();
CreatePythonModule(string.Format(PythonCodeStep, args[2]));
{
var runnerAssembly = CreateCaseRunnerAssembly(verb:"do");
CreateTestClassAssembly(m1: args[0]);

var runnerDomain = CreateDomain("case runner");
RunAndUnload(runnerDomain, runnerAssembly);
}

// Re-create the python module to checkup on the members
CreatePythonModule(PythonCodeStep2);
// CreatePythonModule(PythonCodeStep2);

{
var runnerAssembly = CreateCaseRunnerAssembly(verb:"test");
// remove the method
CreateTestClassAssembly(m1: args[1]);

Expand Down Expand Up @@ -166,16 +174,16 @@ static string CreateTestClassAssembly(string m1 = "", string m2 = "")
return CreateAssembly(name, string.Format(ChangingClassTemplate, m1, m2), exe: false);
}

static string CreateCaseRunnerAssembly(string shutdownMode = "ShutdownMode.Reload")
static string CreateCaseRunnerAssembly(string shutdownMode = "ShutdownMode.Reload", string verb = "do")
{
var code = string.Format(CaseRunnerTemplate, shutdownMode);
var code = string.Format(CaseRunnerTemplate, shutdownMode, verb);
var name = "TestCaseRunner.exe";
return CreateAssembly(name, code, exe: true);
}

static string CreateAssembly(string name, string code, bool exe = false)
{
//Console.WriteLine(code);
// Console.WriteLine(code);
// Never return or hold the Assembly instance. This will cause
// the assembly to be loaded into the current domain and this
// interferes with the tests. The Domain can execute fine from a
Expand Down
0