Skip to content

Commit 48be42f

Browse files
committed
Add test for public API in experimental nuget.
1 parent c8c5f50 commit 48be42f

2 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/Microsoft.ML.Data/MLContext.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,8 @@ internal void StopExecution()
127127
foreach(var host in _hosts)
128128
if (host is ICancelableHost)
129129
((ICancelableHost)host).StopExecution();
130+
131+
_hosts.RemoveAll(h => true);
130132
}
131133
}
132134
}

test/Microsoft.ML.Core.Tests/UnitTests/TestHosts.cs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,45 @@ public void TestCancellation()
7272
}
7373
}
7474

75+
[Fact]
76+
public void TestCancellationRoot()
77+
{
78+
IHostEnvironment env = new MLContext(seed: 42);
79+
var mainHost = env.Register("Main");
80+
var children = new ConcurrentDictionary<IHost, List<IHost>>();
81+
var hosts = new BlockingCollection<Tuple<IHost, int>>();
82+
hosts.Add(new Tuple<IHost, int>(mainHost.Register("1"), 1));
83+
hosts.Add(new Tuple<IHost, int>(mainHost.Register("2"), 1));
84+
hosts.Add(new Tuple<IHost, int>(mainHost.Register("3"), 1));
85+
hosts.Add(new Tuple<IHost, int>(mainHost.Register("4"), 1));
86+
hosts.Add(new Tuple<IHost, int>(mainHost.Register("5"), 1));
87+
Random rand = new Random();
88+
var addThread = new Thread(
89+
() =>
90+
{
91+
for (int i = 0; i < 100; i++)
92+
{
93+
var randHostTuple = hosts.ElementAt(rand.Next(hosts.Count - 1));
94+
var newHost = randHostTuple.Item1.Register((randHostTuple.Item2 + 1).ToString());
95+
hosts.Add(new Tuple<IHost, int>(newHost, randHostTuple.Item2 + 1));
96+
if (!children.ContainsKey(randHostTuple.Item1))
97+
children[randHostTuple.Item1] = new List<IHost>();
98+
else
99+
children[randHostTuple.Item1].Add(newHost);
100+
}
101+
});
102+
addThread.Start();
103+
var index = 0;
104+
do
105+
{
106+
index = rand.Next(hosts.Count);
107+
} while ((hosts.ElementAt(index).Item1 as ICancelableEnvironment).IsCanceled || hosts.ElementAt(index).Item2 < 3);
108+
((MLContext)env).StopExecution();
109+
addThread.Join();
110+
foreach (var value in children.Values)
111+
value.ForEach(v => Assert.True((v as ICancelableEnvironment).IsCanceled));
112+
}
113+
75114
/// <summary>
76115
/// Tests that MLContext's Log event intercepts messages properly.
77116
/// </summary>

0 commit comments

Comments
 (0)