Skip to content
Merged
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
Avoid test interdependency
  • Loading branch information
amos402 authored and Martin-Molinero committed Mar 27, 2019
commit fccd1d874f6ade3490cb8597ee42d0dc526e7f86
101 changes: 55 additions & 46 deletions src/embed_tests/TestPyScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -293,24 +293,27 @@ public void TestImportScopeByName()
[Test]
public void TestVariables()
{
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
var a0 = ps.Get<int>("ee");
Assert.AreEqual(200, a0);
using (Py.GIL())
{
(ps.Variables() as dynamic)["ee"] = new PyInt(200);
var a0 = ps.Get<int>("ee");
Assert.AreEqual(200, a0);

ps.Exec("locals()['ee'] = 210");
var a1 = ps.Get<int>("ee");
Assert.AreEqual(210, a1);
ps.Exec("locals()['ee'] = 210");
var a1 = ps.Get<int>("ee");
Assert.AreEqual(210, a1);

ps.Exec("globals()['ee'] = 220");
var a2 = ps.Get<int>("ee");
Assert.AreEqual(220, a2);
ps.Exec("globals()['ee'] = 220");
var a2 = ps.Get<int>("ee");
Assert.AreEqual(220, a2);

using (var item = ps.Variables())
{
item["ee"] = new PyInt(230);
using (var item = ps.Variables())
{
item["ee"] = new PyInt(230);
}
var a3 = ps.Get<int>("ee");
Assert.AreEqual(230, a3);
}
var a3 = ps.Get<int>("ee");
Assert.AreEqual(230, a3);
}

/// <summary>
Expand All @@ -324,49 +327,55 @@ public void TestThread()
//should be removed.
dynamic _ps = ps;
var ts = PythonEngine.BeginAllowThreads();
using (Py.GIL())
{
_ps.res = 0;
_ps.bb = 100;
_ps.th_cnt = 0;
//add function to the scope
//can be call many times, more efficient than ast
ps.Exec(
"def update():\n" +
" global res, th_cnt\n" +
" res += bb + 1\n" +
" th_cnt += 1\n"
);
}
int th_cnt = 3;
for (int i =0; i< th_cnt; i++)
try
{
System.Threading.Thread th = new System.Threading.Thread(()=>
using (Py.GIL())
{
_ps.res = 0;
_ps.bb = 100;
_ps.th_cnt = 0;
//add function to the scope
//can be call many times, more efficient than ast
ps.Exec(
"def update():\n" +
" global res, th_cnt\n" +
" res += bb + 1\n" +
" th_cnt += 1\n"
);
}
int th_cnt = 3;
for (int i = 0; i < th_cnt; i++)
{
System.Threading.Thread th = new System.Threading.Thread(() =>
{
using (Py.GIL())
{
//ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly
_ps.update();
}
});
th.Start();
}
//equivalent to Thread.Join, make the main thread join the GIL competition
int cnt = 0;
while (cnt != th_cnt)
{
using (Py.GIL())
{
//ps.GetVariable<dynamic>("update")(); //call the scope function dynamicly
_ps.update();
cnt = ps.Get<int>("th_cnt");
}
});
th.Start();
}
//equivalent to Thread.Join, make the main thread join the GIL competition
int cnt = 0;
while(cnt != th_cnt)
{
System.Threading.Thread.Sleep(10);
}
using (Py.GIL())
{
cnt = ps.Get<int>("th_cnt");
var result = ps.Get<int>("res");
Assert.AreEqual(101 * th_cnt, result);
}
System.Threading.Thread.Sleep(10);
}
using (Py.GIL())
finally
{
var result = ps.Get<int>("res");
Assert.AreEqual(101* th_cnt, result);
PythonEngine.EndAllowThreads(ts);
}
PythonEngine.EndAllowThreads(ts);
}
}
}