1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
4 | *
|
---|
5 | * This file is part of HeuristicLab.
|
---|
6 | *
|
---|
7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
8 | * it under the terms of the GNU General Public License as published by
|
---|
9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
10 | * (at your option) any later version.
|
---|
11 | *
|
---|
12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | * GNU General Public License for more details.
|
---|
16 | *
|
---|
17 | * You should have received a copy of the GNU General Public License
|
---|
18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 | #endregion
|
---|
21 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
26 |
|
---|
27 | namespace HeuristicLab.Services.Hive.Tests {
|
---|
28 |
|
---|
29 | using HeuristicLab.Services.Hive.Common;
|
---|
30 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
31 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
32 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
33 |
|
---|
34 | [TestClass]
|
---|
35 | public class ServiceTests {
|
---|
36 |
|
---|
37 | [ClassInitialize]
|
---|
38 | public static void MyClassInitialize(TestContext testContext) {
|
---|
39 | ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
|
---|
40 | }
|
---|
41 |
|
---|
42 | private IHiveService GetLocalService() {
|
---|
43 | return new HiveService();
|
---|
44 | }
|
---|
45 |
|
---|
46 | [TestMethod]
|
---|
47 | public void TestJobs() {
|
---|
48 | var service = GetLocalService();
|
---|
49 |
|
---|
50 | // create hive experiment
|
---|
51 | DT.HiveExperiment experiment = new DT.HiveExperiment() {
|
---|
52 | Name = "TestExperiment",
|
---|
53 | Description = ""
|
---|
54 | };
|
---|
55 |
|
---|
56 | // create job
|
---|
57 | DT.Job job = new DT.Job() {
|
---|
58 | CoresNeeded = 1,
|
---|
59 | MemoryNeeded = 0,
|
---|
60 | Priority = 0
|
---|
61 | };
|
---|
62 | job.State = JobState.Offline;
|
---|
63 | job.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
64 |
|
---|
65 | DT.JobData jobData = new DT.JobData() {
|
---|
66 | //Data = PersistenceUtil.Serialize(new MockJob(500, true))
|
---|
67 | Data = new byte[10000]
|
---|
68 | };
|
---|
69 |
|
---|
70 | // create plugin
|
---|
71 | DT.Plugin plugin1 = new DT.Plugin();
|
---|
72 | plugin1.Name = "Tests.MyPlugin";
|
---|
73 | plugin1.Version = new Version("1.0.0.0");
|
---|
74 | plugin1.UserId = Guid.Empty;
|
---|
75 | plugin1.IsLocal = true;
|
---|
76 | plugin1.DateCreated = DateTime.Now;
|
---|
77 |
|
---|
78 | DT.PluginData pluginData1 = new DT.PluginData();
|
---|
79 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
80 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
81 |
|
---|
82 | plugin1.Id = service.AddPlugin(plugin1, new List<PluginData> { pluginData1 });
|
---|
83 | pluginData1.PluginId = plugin1.Id;
|
---|
84 |
|
---|
85 | // add plugin
|
---|
86 | job.PluginsNeededIds.Add(plugin1.Id);
|
---|
87 |
|
---|
88 | // create slave
|
---|
89 | DT.Slave slave = new Slave();
|
---|
90 | slave.Id = Guid.NewGuid();
|
---|
91 | slave.Name = "TestSlave";
|
---|
92 | slave.Memory = 1024;
|
---|
93 | slave.Cores = 4;
|
---|
94 | slave.CpuSpeed = 2800;
|
---|
95 | slave.OperatingSystem = "Windows 3.11";
|
---|
96 | slave.CpuArchitecture = CpuArchitecture.x64;
|
---|
97 |
|
---|
98 | // add slave
|
---|
99 | service.AddSlave(slave);
|
---|
100 |
|
---|
101 | // add hive experiment
|
---|
102 | experiment.Id = service.AddHiveExperiment(experiment);
|
---|
103 |
|
---|
104 | // add job
|
---|
105 | job.HiveExperimentId = experiment.Id;
|
---|
106 | job.Id = service.AddJob(job, jobData, new List<Guid> { slave.Id });
|
---|
107 |
|
---|
108 | // test job
|
---|
109 | DT.Job jobLoaded = service.GetJob(job.Id);
|
---|
110 | Assert.AreEqual(job.Id, jobLoaded.Id);
|
---|
111 | Assert.AreEqual(job.CoresNeeded, jobLoaded.CoresNeeded);
|
---|
112 | Assert.AreEqual(job.MemoryNeeded, jobLoaded.MemoryNeeded);
|
---|
113 | Assert.AreEqual(job.Priority, jobLoaded.Priority);
|
---|
114 | Assert.AreEqual(JobState.Waiting, jobLoaded.State);
|
---|
115 | Assert.IsTrue(job.PluginsNeededIds.SequenceEqual(jobLoaded.PluginsNeededIds));
|
---|
116 | Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
|
---|
117 |
|
---|
118 | DT.JobData jobDataLoaded = service.GetJobData(job.Id);
|
---|
119 | Assert.AreEqual(job.Id, jobDataLoaded.JobId);
|
---|
120 | Assert.IsTrue(jobData.Data.SequenceEqual(jobDataLoaded.Data));
|
---|
121 |
|
---|
122 | // test hive experiment
|
---|
123 | DT.HiveExperiment experimentLoaded = service.GetHiveExperiment(experiment.Id);
|
---|
124 | Assert.AreEqual(experiment.Id, experimentLoaded.Id);
|
---|
125 | Assert.AreEqual(experiment.Name, experimentLoaded.Name);
|
---|
126 | Assert.AreEqual(experiment.Description, experimentLoaded.Description);
|
---|
127 |
|
---|
128 | // test assigned ressources
|
---|
129 | var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
130 | Assert.AreEqual(1, actions.Count);
|
---|
131 | Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
|
---|
132 | Assert.AreEqual(job.Id, actions[0].JobId);
|
---|
133 |
|
---|
134 | jobLoaded = service.GetJob(job.Id);
|
---|
135 | Assert.AreEqual(JobState.Transferring, jobLoaded.State);
|
---|
136 |
|
---|
137 | // slave is responsible for updating state to 'Calculating'
|
---|
138 | service.UpdateJobState(jobLoaded.Id, JobState.Calculating, slave.Id, null, null);
|
---|
139 |
|
---|
140 | // send progress
|
---|
141 | var progress = new Dictionary<Guid, TimeSpan>();
|
---|
142 | progress.Add(job.Id, new TimeSpan(1, 5, 10, 20, 30));
|
---|
143 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 3, FreeMemory = 1024, JobProgress = progress });
|
---|
144 | Assert.AreEqual(0, actions.Count);
|
---|
145 |
|
---|
146 | // the job should be in state 'Calculating' now
|
---|
147 | jobLoaded = service.GetJob(job.Id);
|
---|
148 | Assert.AreEqual(JobState.Calculating, jobLoaded.State);
|
---|
149 | Assert.AreEqual(new TimeSpan(1, 5, 10, 20, 30), jobLoaded.ExecutionTime);
|
---|
150 |
|
---|
151 | // test if the job is returned for the resource
|
---|
152 | var jobsBySlave = service.GetJobsByResourceId(slave.Id);
|
---|
153 | Assert.AreEqual(1, jobsBySlave.Count());
|
---|
154 | Assert.AreEqual(job.Id, jobsBySlave.Single().Id);
|
---|
155 |
|
---|
156 | // set it to finished
|
---|
157 | service.UpdateJobState(jobLoaded.Id, JobState.Finished, slave.Id, null, null);
|
---|
158 |
|
---|
159 | // test if the job is returned for the resource (it should not be)
|
---|
160 | var jobsBySlave2 = service.GetJobsByResourceId(slave.Id);
|
---|
161 | Assert.AreEqual(0, jobsBySlave2.Count());
|
---|
162 |
|
---|
163 | // set job waiting again
|
---|
164 | service.UpdateJobState(job.Id, JobState.Waiting, null, null, string.Empty);
|
---|
165 |
|
---|
166 | // get job again
|
---|
167 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
168 | Assert.AreEqual(1, actions.Count);
|
---|
169 | Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
|
---|
170 | Assert.AreEqual(job.Id, actions[0].JobId);
|
---|
171 |
|
---|
172 | // create appointment which should make slave unavailable for calculation
|
---|
173 | Guid appointmentId = service.AddAppointment(new Appointment { ResourceId = slave.Id, StartDate = DateTime.Now - TimeSpan.FromMinutes(1), EndDate = DateTime.Now + TimeSpan.FromMinutes(1), Recurring = false });
|
---|
174 |
|
---|
175 | progress.Clear();
|
---|
176 | progress.Add(job.Id, new TimeSpan(1, 5, 10, 20, 30));
|
---|
177 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
178 | Assert.AreEqual(1, actions.Count);
|
---|
179 | Assert.AreEqual(MessageContainer.MessageType.PauseAll, actions[0].Message);
|
---|
180 | Assert.AreEqual(Guid.Empty, actions[0].JobId);
|
---|
181 |
|
---|
182 | service.DeleteAppointment(appointmentId);
|
---|
183 |
|
---|
184 | // delete
|
---|
185 | service.DeleteHiveExperiment(experiment.Id);
|
---|
186 | Assert.AreEqual(null, service.GetHiveExperiment(experiment.Id));
|
---|
187 | Assert.AreEqual(null, service.GetJob(job.Id));
|
---|
188 | Assert.AreEqual(null, service.GetJobData(job.Id));
|
---|
189 |
|
---|
190 | // send another heartbeat with the deleted job; the server should command the abortion of the job
|
---|
191 | actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 3, FreeMemory = 1024, JobProgress = progress });
|
---|
192 | Assert.AreEqual(1, actions.Count);
|
---|
193 | Assert.AreEqual(MessageContainer.MessageType.AbortJob, actions[0].Message);
|
---|
194 | Assert.AreEqual(job.Id, actions[0].JobId);
|
---|
195 |
|
---|
196 | // delete slave
|
---|
197 | service.DeleteSlave(slave.Id);
|
---|
198 | }
|
---|
199 |
|
---|
200 | [TestMethod]
|
---|
201 | public void TestParentJobs() {
|
---|
202 | var service = GetLocalService();
|
---|
203 |
|
---|
204 | // create hive experiment
|
---|
205 | DT.HiveExperiment experiment = new DT.HiveExperiment() {
|
---|
206 | Name = "TestExperiment",
|
---|
207 | Description = ""
|
---|
208 | };
|
---|
209 |
|
---|
210 | // create parent job
|
---|
211 | DT.Job parentJob = new DT.Job() {
|
---|
212 | CoresNeeded = 1,
|
---|
213 | MemoryNeeded = 0,
|
---|
214 | Priority = 0,
|
---|
215 | IsParentJob = true,
|
---|
216 | FinishWhenChildJobsFinished = true
|
---|
217 | };
|
---|
218 | parentJob.State = JobState.Offline;
|
---|
219 | parentJob.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
220 |
|
---|
221 | DT.JobData parentJobData = new DT.JobData() { Data = new byte[0] };
|
---|
222 |
|
---|
223 | // create child job
|
---|
224 | DT.Job childJob = new DT.Job() {
|
---|
225 | CoresNeeded = 1,
|
---|
226 | MemoryNeeded = 0,
|
---|
227 | Priority = 0
|
---|
228 | };
|
---|
229 | childJob.State = JobState.Offline;
|
---|
230 | childJob.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
|
---|
231 |
|
---|
232 | DT.JobData childJobData = new DT.JobData() { Data = new byte[1000] };
|
---|
233 |
|
---|
234 | // create slave
|
---|
235 | DT.Slave slave = new Slave();
|
---|
236 | slave.Id = Guid.NewGuid();
|
---|
237 | slave.Name = "TestSlave";
|
---|
238 | slave.Memory = 1024;
|
---|
239 | slave.Cores = 4;
|
---|
240 | slave.CpuSpeed = 2800;
|
---|
241 | slave.OperatingSystem = "Windows 3.11";
|
---|
242 | slave.CpuArchitecture = CpuArchitecture.x64;
|
---|
243 |
|
---|
244 | // add slave
|
---|
245 | service.AddSlave(slave);
|
---|
246 |
|
---|
247 | // add hive experiment
|
---|
248 | experiment.Id = service.AddHiveExperiment(experiment);
|
---|
249 |
|
---|
250 | // add parent job
|
---|
251 | parentJob.HiveExperimentId = experiment.Id;
|
---|
252 | parentJob.Id = service.AddJob(parentJob, parentJobData, new List<Guid> { slave.Id });
|
---|
253 |
|
---|
254 | // add child job
|
---|
255 | childJob.HiveExperimentId = experiment.Id;
|
---|
256 | childJob.Id = service.AddChildJob(parentJob.Id, childJob, childJobData);
|
---|
257 | childJob.ParentJobId = parentJob.Id;
|
---|
258 |
|
---|
259 | // test child job
|
---|
260 | var childJobLoaded = service.GetJob(childJob.Id);
|
---|
261 | Assert.AreEqual(childJob.ParentJobId, childJobLoaded.ParentJobId);
|
---|
262 | Assert.AreEqual(childJob.HiveExperimentId, childJobLoaded.HiveExperimentId);
|
---|
263 | Assert.AreEqual(JobState.Waiting, childJobLoaded.State);
|
---|
264 | Assert.AreEqual(false, childJobLoaded.FinishWhenChildJobsFinished);
|
---|
265 | Assert.AreEqual(false, childJobLoaded.IsParentJob);
|
---|
266 |
|
---|
267 | // test parent job
|
---|
268 | var parentJobLoaded = service.GetJob(parentJob.Id);
|
---|
269 | Assert.AreEqual(parentJob.HiveExperimentId, parentJobLoaded.HiveExperimentId);
|
---|
270 | Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
|
---|
271 | Assert.AreEqual(true, parentJobLoaded.FinishWhenChildJobsFinished);
|
---|
272 | Assert.AreEqual(true, parentJobLoaded.IsParentJob);
|
---|
273 |
|
---|
274 | // test heartbeat
|
---|
275 | var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
276 | Assert.AreEqual(1, actions.Count); // only the child job should be assigned
|
---|
277 | Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
|
---|
278 | Assert.AreEqual(childJob.Id, actions[0].JobId);
|
---|
279 |
|
---|
280 | // lifecycle - let it process one server-heartbeat; the parent job must NOT be set to finished
|
---|
281 | service.TriggerLifecycle(true);
|
---|
282 |
|
---|
283 | parentJobLoaded = service.GetJob(parentJob.Id);
|
---|
284 | Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
|
---|
285 |
|
---|
286 | // set child job to finished
|
---|
287 | childJobLoaded = service.UpdateJobState(childJobLoaded.Id, JobState.Finished, slave.Id, null, null);
|
---|
288 |
|
---|
289 | // lifecycle - let it process one server-heartbeat; this should set the parent job to finished
|
---|
290 | service.TriggerLifecycle(true);
|
---|
291 |
|
---|
292 | // test if parent job is finished
|
---|
293 | parentJobLoaded = service.GetJob(parentJob.Id);
|
---|
294 | Assert.AreEqual(JobState.Finished, parentJobLoaded.State);
|
---|
295 |
|
---|
296 | // delete experiment
|
---|
297 | service.DeleteHiveExperiment(experiment.Id);
|
---|
298 | Assert.AreEqual(null, service.GetJob(parentJob.Id));
|
---|
299 | Assert.AreEqual(null, service.GetJob(childJob.Id));
|
---|
300 |
|
---|
301 | service.DeleteSlave(slave.Id);
|
---|
302 | }
|
---|
303 | }
|
---|
304 | }
|
---|