@@ -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