Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/ServiceTests.cs @ 6452

Last change on this file since 6452 was 6452, checked in by cneumuel, 13 years ago

#1233

  • renamed UptimeCalendar and Appointment to Downtime
  • added service methods to delete plugins and get plugin by hash
  • made reverted TransactionManager change, made it non-static and added interface
  • moved magic numbers to application settings
File size: 12.4 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Services.Hive.Common;
26using HeuristicLab.Services.Hive.Common.DataTransfer;
27using HeuristicLab.Services.Hive.Common.ServiceContracts;
28using Microsoft.VisualStudio.TestTools.UnitTesting;
29using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
30
31namespace HeuristicLab.Services.Hive.Tests {
32  [TestClass]
33  public class ServiceTests {
34
35    [ClassInitialize]
36    public static void MyClassInitialize(TestContext testContext) {
37      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
38    }
39
40    private IHiveService GetLocalService() {
41      return new HiveService();
42    }
43
44    [TestMethod]
45    public void TestJobs() {
46      var service = GetLocalService();
47
48      // create hive experiment
49      DT.HiveExperiment experiment = new DT.HiveExperiment() {
50        Name = "TestExperiment",
51        Description = ""
52      };
53
54      // create job
55      DT.Job job = new DT.Job() {
56        CoresNeeded = 1,
57        MemoryNeeded = 0,
58        Priority = 0
59      };
60      job.State = JobState.Offline;
61      job.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
62
63      DT.JobData jobData = new DT.JobData() {
64        //Data = PersistenceUtil.Serialize(new MockJob(500, true))
65        Data = new byte[10000]
66      };
67
68      // delete plugin first (otherwise the system would not allow it because of the same hash code
69      var hash = new byte[] { 1, 2, 3 };
70      var p = service.GetPluginByHash(hash);
71      if (p != null) service.DeletePlugin(p.Id);
72
73      // create plugin
74      DT.Plugin plugin1 = new DT.Plugin();
75      plugin1.Name = "Tests.MyPlugin";
76      plugin1.Version = new Version("1.0.0.0");
77      plugin1.UserId = Guid.Empty;
78      plugin1.DateCreated = DateTime.Now;
79      plugin1.Hash = hash;
80     
81      DT.PluginData pluginData1 = new DT.PluginData();
82      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
83      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
84
85      plugin1.Id = service.AddPlugin(plugin1, new List<PluginData> { pluginData1 });
86      pluginData1.PluginId = plugin1.Id;
87
88      // add plugin
89      job.PluginsNeededIds.Add(plugin1.Id);
90
91      // create slave
92      DT.Slave slave = new Slave();
93      slave.Id = Guid.NewGuid();
94      slave.Name = "TestSlave";
95      slave.Memory = 1024;
96      slave.Cores = 4;
97      slave.CpuSpeed = 2800;
98      slave.OperatingSystem = "Windows 3.11";
99      slave.CpuArchitecture = CpuArchitecture.x64;
100
101      // add slave
102      service.AddSlave(slave);
103
104      // add hive experiment
105      experiment.Id = service.AddHiveExperiment(experiment);
106
107      // add job
108      job.HiveExperimentId = experiment.Id;
109      job.Id = service.AddJob(job, jobData, new List<Guid> { slave.Id });
110
111      // test job
112      DT.Job jobLoaded = service.GetJob(job.Id);
113      Assert.AreEqual(job.Id, jobLoaded.Id);
114      Assert.AreEqual(job.CoresNeeded, jobLoaded.CoresNeeded);
115      Assert.AreEqual(job.MemoryNeeded, jobLoaded.MemoryNeeded);
116      Assert.AreEqual(job.Priority, jobLoaded.Priority);
117      Assert.AreEqual(JobState.Waiting, jobLoaded.State);
118      Assert.IsTrue(job.PluginsNeededIds.SequenceEqual(jobLoaded.PluginsNeededIds));
119      Assert.AreEqual(job.HiveExperimentId, jobLoaded.HiveExperimentId);
120
121      DT.JobData jobDataLoaded = service.GetJobData(job.Id);
122      Assert.AreEqual(job.Id, jobDataLoaded.JobId);
123      Assert.IsTrue(jobData.Data.SequenceEqual(jobDataLoaded.Data));
124
125      // test hive experiment
126      DT.HiveExperiment experimentLoaded = service.GetHiveExperiment(experiment.Id);
127      Assert.AreEqual(experiment.Id, experimentLoaded.Id);
128      Assert.AreEqual(experiment.Name, experimentLoaded.Name);
129      Assert.AreEqual(experiment.Description, experimentLoaded.Description);
130
131      // test assigned ressources
132      var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
133      Assert.AreEqual(1, actions.Count);
134      Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
135      Assert.AreEqual(job.Id, actions[0].JobId);
136
137      jobLoaded = service.GetJob(job.Id);
138      Assert.AreEqual(JobState.Transferring, jobLoaded.State);
139
140      // slave is responsible for updating state to 'Calculating'
141      service.UpdateJobState(jobLoaded.Id, JobState.Calculating, slave.Id, null, null);
142
143      // send progress
144      var progress = new Dictionary<Guid, TimeSpan>();
145      progress.Add(job.Id, new TimeSpan(1, 5, 10, 20, 30));
146      actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 3, FreeMemory = 1024, JobProgress = progress });
147      Assert.AreEqual(0, actions.Count);
148
149      // the job should be in state 'Calculating' now
150      jobLoaded = service.GetJob(job.Id);
151      Assert.AreEqual(JobState.Calculating, jobLoaded.State);
152      Assert.AreEqual(new TimeSpan(1, 5, 10, 20, 30), jobLoaded.ExecutionTime);
153
154      // test if the job is returned for the resource
155      var jobsBySlave = service.GetJobsByResourceId(slave.Id);
156      Assert.AreEqual(1, jobsBySlave.Count());
157      Assert.AreEqual(job.Id, jobsBySlave.Single().Id);
158
159      // set it to finished
160      service.UpdateJobState(jobLoaded.Id, JobState.Finished, slave.Id, null, null);
161
162      // test if the job is returned for the resource (it should not be)
163      var jobsBySlave2 = service.GetJobsByResourceId(slave.Id);
164      Assert.AreEqual(0, jobsBySlave2.Count());
165
166      // set job waiting again
167      service.UpdateJobState(job.Id, JobState.Waiting, null, null, string.Empty);
168
169      // get job again
170      actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
171      Assert.AreEqual(1, actions.Count);
172      Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
173      Assert.AreEqual(job.Id, actions[0].JobId);
174
175      // create downtime which should make slave unavailable for calculation
176      Guid downtimeId = service.AddDowntime(new Downtime { ResourceId = slave.Id, StartDate = DateTime.Now - TimeSpan.FromMinutes(1), EndDate = DateTime.Now + TimeSpan.FromMinutes(1), Recurring = false });
177
178      progress.Clear();
179      progress.Add(job.Id, new TimeSpan(1, 5, 10, 20, 30));
180      actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
181      Assert.AreEqual(1, actions.Count);
182      Assert.AreEqual(MessageContainer.MessageType.PauseAll, actions[0].Message);
183      Assert.AreEqual(Guid.Empty, actions[0].JobId);
184
185      service.DeleteDowntime(downtimeId);
186
187      // delete
188      service.DeleteHiveExperiment(experiment.Id);
189      Assert.AreEqual(null, service.GetHiveExperiment(experiment.Id));
190      Assert.AreEqual(null, service.GetJob(job.Id));
191      Assert.AreEqual(null, service.GetJobData(job.Id));
192
193      // send another heartbeat with the deleted job; the server should command the abortion of the job
194      actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 3, FreeMemory = 1024, JobProgress = progress });
195      Assert.AreEqual(1, actions.Count);
196      Assert.AreEqual(MessageContainer.MessageType.AbortJob, actions[0].Message);
197      Assert.AreEqual(job.Id, actions[0].JobId);
198
199      // delete slave
200      service.DeleteSlave(slave.Id);
201    }
202
203    [TestMethod]
204    public void TestParentJobs() {
205      var service = GetLocalService();
206
207      // create hive experiment
208      DT.HiveExperiment experiment = new DT.HiveExperiment() {
209        Name = "TestExperiment",
210        Description = ""
211      };
212
213      // create parent job
214      DT.Job parentJob = new DT.Job() {
215        CoresNeeded = 1,
216        MemoryNeeded = 0,
217        Priority = 0,
218        IsParentJob = true,
219        FinishWhenChildJobsFinished = true
220      };
221      parentJob.State = JobState.Offline;
222      parentJob.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
223
224      DT.JobData parentJobData = new DT.JobData() { Data = new byte[0] };
225
226      // create child job
227      DT.Job childJob = new DT.Job() {
228        CoresNeeded = 1,
229        MemoryNeeded = 0,
230        Priority = 0
231      };
232      childJob.State = JobState.Offline;
233      childJob.StateLog.Add(new StateLog { State = JobState.Offline, DateTime = DateTime.Now });
234
235      DT.JobData childJobData = new DT.JobData() { Data = new byte[1000] };
236
237      // create slave
238      DT.Slave slave = new Slave();
239      slave.Id = Guid.NewGuid();
240      slave.Name = "TestSlave";
241      slave.Memory = 1024;
242      slave.Cores = 4;
243      slave.CpuSpeed = 2800;
244      slave.OperatingSystem = "Windows 3.11";
245      slave.CpuArchitecture = CpuArchitecture.x64;
246
247      // add slave
248      service.AddSlave(slave);
249
250      // add hive experiment
251      experiment.Id = service.AddHiveExperiment(experiment);
252
253      // add parent job
254      parentJob.HiveExperimentId = experiment.Id;
255      parentJob.Id = service.AddJob(parentJob, parentJobData, new List<Guid> { slave.Id });
256
257      // add child job
258      childJob.HiveExperimentId = experiment.Id;
259      childJob.Id = service.AddChildJob(parentJob.Id, childJob, childJobData);
260      childJob.ParentJobId = parentJob.Id;
261
262      // test child job
263      var childJobLoaded = service.GetJob(childJob.Id);
264      Assert.AreEqual(childJob.ParentJobId, childJobLoaded.ParentJobId);
265      Assert.AreEqual(childJob.HiveExperimentId, childJobLoaded.HiveExperimentId);
266      Assert.AreEqual(JobState.Waiting, childJobLoaded.State);
267      Assert.AreEqual(false, childJobLoaded.FinishWhenChildJobsFinished);
268      Assert.AreEqual(false, childJobLoaded.IsParentJob);
269
270      // test parent job
271      var parentJobLoaded = service.GetJob(parentJob.Id);
272      Assert.AreEqual(parentJob.HiveExperimentId, parentJobLoaded.HiveExperimentId);
273      Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
274      Assert.AreEqual(true, parentJobLoaded.FinishWhenChildJobsFinished);
275      Assert.AreEqual(true, parentJobLoaded.IsParentJob);
276
277      // test heartbeat
278      var actions = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, AssignJob = true, FreeCores = 4, FreeMemory = 1024, JobProgress = new Dictionary<Guid, TimeSpan>() });
279      Assert.AreEqual(1, actions.Count); // only the child job should be assigned
280      Assert.AreEqual(MessageContainer.MessageType.CalculateJob, actions[0].Message);
281      Assert.AreEqual(childJob.Id, actions[0].JobId);
282
283      // lifecycle - let it process one server-heartbeat; the parent job must NOT be set to finished
284      service.TriggerLifecycle(true);
285
286      parentJobLoaded = service.GetJob(parentJob.Id);
287      Assert.AreEqual(JobState.Waiting, parentJobLoaded.State);
288
289      // set child job to finished
290      childJobLoaded = service.UpdateJobState(childJobLoaded.Id, JobState.Finished, slave.Id, null, null);
291
292      // lifecycle - let it process one server-heartbeat; this should set the parent job to finished
293      service.TriggerLifecycle(true);
294
295      // test if parent job is finished
296      parentJobLoaded = service.GetJob(parentJob.Id);
297      Assert.AreEqual(JobState.Finished, parentJobLoaded.State);
298
299      // delete experiment
300      service.DeleteHiveExperiment(experiment.Id);
301      Assert.AreEqual(null, service.GetJob(parentJob.Id));
302      Assert.AreEqual(null, service.GetJob(childJob.Id));
303
304      service.DeleteSlave(slave.Id);
305    }
306  }
307}
Note: See TracBrowser for help on using the repository browser.