6
6
package org .elasticsearch .xpack .ml .dataframe .process ;
7
7
8
8
import org .elasticsearch .ElasticsearchException ;
9
+ import org .elasticsearch .ElasticsearchParseException ;
9
10
import org .elasticsearch .action .ActionListener ;
10
11
import org .elasticsearch .common .unit .ByteSizeValue ;
11
12
import org .elasticsearch .common .util .concurrent .EsExecutors ;
@@ -65,7 +66,6 @@ public void setUpMocks() {
65
66
executorServiceForJob = EsExecutors .newDirectExecutorService ();
66
67
executorServiceForProcess = mock (ExecutorService .class );
67
68
process = mock (AnalyticsProcess .class );
68
- when (process .isProcessAlive ()).thenReturn (true );
69
69
when (process .readAnalyticsResults ()).thenReturn (List .of (PROCESS_RESULT ).iterator ());
70
70
processFactory = mock (AnalyticsProcessFactory .class );
71
71
when (processFactory .createAnalyticsProcess (anyString (), any (), any (), any ())).thenReturn (process );
@@ -93,61 +93,61 @@ public void testRunJob_EmptyDataFrame() {
93
93
verifyNoMoreInteractions (process , listener );
94
94
}
95
95
96
- public void testRunJob_ProcessNotAlive () {
97
- when (process .isProcessAlive ()).thenReturn (false );
98
- when (process .readError ()).thenReturn ("Error from inside the process" );
96
+ public void testRunJob_NoResults () throws Exception {
97
+ when (process .readAnalyticsResults ()).thenReturn (List .<MemoryUsageEstimationResult >of ().iterator ());
99
98
100
99
processManager .runJobAsync (TASK_ID , dataFrameAnalyticsConfig , dataExtractorFactory , listener );
101
100
102
101
verify (listener ).onFailure (exceptionCaptor .capture ());
103
102
ElasticsearchException exception = (ElasticsearchException ) exceptionCaptor .getValue ();
104
103
assertThat (exception .status (), equalTo (RestStatus .INTERNAL_SERVER_ERROR ));
105
104
assertThat (exception .getMessage (), containsString (TASK_ID ));
106
- assertThat (exception .getMessage (), containsString ("Error while starting process" ));
107
- assertThat (exception .getMessage (), containsString ("Error from inside the process" ));
105
+ assertThat (exception .getMessage (), containsString ("no results" ));
108
106
109
- verify (process ).isProcessAlive ();
110
- verify (process ).readError ();
107
+ InOrder inOrder = inOrder (process );
108
+ inOrder .verify (process ).readAnalyticsResults ();
109
+ inOrder .verify (process ).readError ();
110
+ inOrder .verify (process ).consumeAndCloseOutputStream ();
111
+ inOrder .verify (process ).close ();
111
112
verifyNoMoreInteractions (process , listener );
112
113
}
113
114
114
- public void testRunJob_NoResults () throws Exception {
115
- when (process .readAnalyticsResults ()).thenReturn (List .< MemoryUsageEstimationResult > of ().iterator ());
115
+ public void testRunJob_MultipleResults () throws Exception {
116
+ when (process .readAnalyticsResults ()).thenReturn (List .of (PROCESS_RESULT , PROCESS_RESULT ).iterator ());
116
117
117
118
processManager .runJobAsync (TASK_ID , dataFrameAnalyticsConfig , dataExtractorFactory , listener );
118
119
119
120
verify (listener ).onFailure (exceptionCaptor .capture ());
120
121
ElasticsearchException exception = (ElasticsearchException ) exceptionCaptor .getValue ();
121
122
assertThat (exception .status (), equalTo (RestStatus .INTERNAL_SERVER_ERROR ));
122
123
assertThat (exception .getMessage (), containsString (TASK_ID ));
123
- assertThat (exception .getMessage (), containsString ("no results " ));
124
+ assertThat (exception .getMessage (), containsString ("more than one result " ));
124
125
125
126
InOrder inOrder = inOrder (process );
126
- inOrder .verify (process ).isProcessAlive ();
127
127
inOrder .verify (process ).readAnalyticsResults ();
128
+ inOrder .verify (process ).readError ();
128
129
inOrder .verify (process ).consumeAndCloseOutputStream ();
129
130
inOrder .verify (process ).close ();
130
131
verifyNoMoreInteractions (process , listener );
131
132
}
132
133
133
- public void testRunJob_MultipleResults () throws Exception {
134
- when (process .readAnalyticsResults ()).thenReturn ( List . of ( PROCESS_RESULT , PROCESS_RESULT ). iterator ( ));
134
+ public void testRunJob_OneResult_ParseException () throws Exception {
135
+ when (process .readAnalyticsResults ()).thenThrow ( new ElasticsearchParseException ( "cannot parse result" ));
135
136
136
137
processManager .runJobAsync (TASK_ID , dataFrameAnalyticsConfig , dataExtractorFactory , listener );
137
138
138
139
verify (listener ).onFailure (exceptionCaptor .capture ());
139
140
ElasticsearchException exception = (ElasticsearchException ) exceptionCaptor .getValue ();
140
141
assertThat (exception .status (), equalTo (RestStatus .INTERNAL_SERVER_ERROR ));
141
142
assertThat (exception .getMessage (), containsString (TASK_ID ));
142
- assertThat (exception .getMessage (), containsString ("more than one result" ));
143
+ assertThat (exception .getMessage (), containsString ("cannot parse result" ));
143
144
144
145
InOrder inOrder = inOrder (process );
145
- inOrder .verify (process ).isProcessAlive ();
146
146
inOrder .verify (process ).readAnalyticsResults ();
147
+ inOrder .verify (process ).readError ();
147
148
inOrder .verify (process ).consumeAndCloseOutputStream ();
148
149
inOrder .verify (process ).close ();
149
150
verifyNoMoreInteractions (process , listener );
150
-
151
151
}
152
152
153
153
public void testRunJob_FailsOnClose () throws Exception {
@@ -162,10 +162,32 @@ public void testRunJob_FailsOnClose() throws Exception {
162
162
assertThat (exception .getMessage (), containsString ("Error while closing process" ));
163
163
164
164
InOrder inOrder = inOrder (process );
165
- inOrder .verify (process ).isProcessAlive ();
166
165
inOrder .verify (process ).readAnalyticsResults ();
167
166
inOrder .verify (process ).consumeAndCloseOutputStream ();
168
167
inOrder .verify (process ).close ();
168
+ inOrder .verify (process ).readError ();
169
+ verifyNoMoreInteractions (process , listener );
170
+ }
171
+
172
+ public void testRunJob_FailsOnClose_ProcessReportsError () throws Exception {
173
+ doThrow (ExceptionsHelper .serverError ("some LOG(ERROR) lines coming from cpp process" )).when (process ).close ();
174
+ when (process .readError ()).thenReturn ("Error from inside the process" );
175
+
176
+ processManager .runJobAsync (TASK_ID , dataFrameAnalyticsConfig , dataExtractorFactory , listener );
177
+
178
+ verify (listener ).onFailure (exceptionCaptor .capture ());
179
+ ElasticsearchException exception = (ElasticsearchException ) exceptionCaptor .getValue ();
180
+ assertThat (exception .status (), equalTo (RestStatus .INTERNAL_SERVER_ERROR ));
181
+ assertThat (exception .getMessage (), containsString (TASK_ID ));
182
+ assertThat (exception .getMessage (), containsString ("Error while closing process" ));
183
+ assertThat (exception .getMessage (), containsString ("some LOG(ERROR) lines coming from cpp process" ));
184
+ assertThat (exception .getMessage (), containsString ("Error from inside the process" ));
185
+
186
+ InOrder inOrder = inOrder (process );
187
+ inOrder .verify (process ).readAnalyticsResults ();
188
+ inOrder .verify (process ).consumeAndCloseOutputStream ();
189
+ inOrder .verify (process ).close ();
190
+ inOrder .verify (process ).readError ();
169
191
verifyNoMoreInteractions (process , listener );
170
192
}
171
193
@@ -177,7 +199,6 @@ public void testRunJob_Ok() throws Exception {
177
199
assertThat (result , equalTo (PROCESS_RESULT ));
178
200
179
201
InOrder inOrder = inOrder (process );
180
- inOrder .verify (process ).isProcessAlive ();
181
202
inOrder .verify (process ).readAnalyticsResults ();
182
203
inOrder .verify (process ).consumeAndCloseOutputStream ();
183
204
inOrder .verify (process ).close ();
0 commit comments