@@ -88,6 +88,80 @@ UNIT_TEST_CASE(DartAPI_DartInitializeCallsCodeObserver) {
8888 EXPECT (Dart_Cleanup () == NULL );
8989}
9090
91+ TEST_CASE (Dart_KillIsolate) {
92+ const char * kScriptChars =
93+ " int testMain() {\n "
94+ " return 42;\n "
95+ " }\n " ;
96+ Dart_Handle lib = TestCase::LoadTestScript (kScriptChars , NULL );
97+ EXPECT_VALID (lib);
98+ Dart_Handle result = Dart_Invoke (lib, NewString (" testMain" ), 0 , NULL );
99+ EXPECT_VALID (result);
100+ int64_t value = 0 ;
101+ EXPECT_VALID (Dart_IntegerToInt64 (result, &value));
102+ EXPECT_EQ (42 , value);
103+ Dart_Isolate isolate = reinterpret_cast <Dart_Isolate>(Isolate::Current ());
104+ Dart_KillIsolate (isolate);
105+ result = Dart_Invoke (lib, NewString (" testMain" ), 0 , NULL );
106+ EXPECT (Dart_IsError (result));
107+ EXPECT_STREQ (" isolate terminated by Isolate.kill" , Dart_GetError (result));
108+ }
109+
110+ class InfiniteLoopTask : public ThreadPool ::Task {
111+ public:
112+ InfiniteLoopTask (Dart_Isolate* isolate, Monitor* monitor, bool * interrupted)
113+ : isolate_(isolate), monitor_(monitor), interrupted_(interrupted) {}
114+ virtual void Run () {
115+ TestIsolateScope scope;
116+ const char * kScriptChars =
117+ " testMain() {\n "
118+ " while(true) {};"
119+ " }\n " ;
120+ Dart_Handle lib = TestCase::LoadTestScript (kScriptChars , NULL );
121+ EXPECT_VALID (lib);
122+ *isolate_ = reinterpret_cast <Dart_Isolate>(Isolate::Current ());
123+ {
124+ MonitorLocker ml (monitor_);
125+ ml.Notify ();
126+ }
127+ Dart_Handle result = Dart_Invoke (lib, NewString (" testMain" ), 0 , NULL );
128+ // Test should run an inifinite loop and expect that to be killed.
129+ EXPECT (Dart_IsError (result));
130+ EXPECT_STREQ (" isolate terminated by Isolate.kill" , Dart_GetError (result));
131+ {
132+ MonitorLocker ml (monitor_);
133+ *interrupted_ = true ;
134+ ml.Notify ();
135+ }
136+ }
137+
138+ private:
139+ Dart_Isolate* isolate_;
140+ Monitor* monitor_;
141+ bool * interrupted_;
142+ };
143+
144+ TEST_CASE (Dart_KillIsolatePriority) {
145+ Monitor monitor;
146+ bool interrupted = false ;
147+ Dart_Isolate isolate;
148+ Dart::thread_pool ()->Run <InfiniteLoopTask>(&isolate, &monitor, &interrupted);
149+ {
150+ MonitorLocker ml (&monitor);
151+ ml.Wait ();
152+ }
153+
154+ Dart_KillIsolate (isolate);
155+
156+ {
157+ MonitorLocker ml (&monitor);
158+ while (!interrupted) {
159+ ml.Wait ();
160+ }
161+ }
162+ EXPECT (interrupted);
163+ }
164+
91165TEST_CASE (DartAPI_ErrorHandleBasics) {
92166 const char * kScriptChars =
93167 " void testMain() {\n "
0 commit comments