[5156] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2010 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;
|
---|
[5106] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
[5156] | 25 | using HeuristicLab.Clients.Hive;
|
---|
| 26 | using HeuristicLab.Clients.Hive.Slave.Tests;
|
---|
[5106] | 27 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
| 28 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
[5156] | 29 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[5404] | 30 | using System.Threading;
|
---|
| 31 | using HeuristicLab.Hive;
|
---|
| 32 | using HeuristicLab.Services.Hive.Common;
|
---|
[5106] | 33 |
|
---|
| 34 | namespace HeuristicLab.Services.Hive.Tests {
|
---|
[5404] | 35 |
|
---|
[5156] | 36 | using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
[5404] | 37 | using System.Diagnostics;
|
---|
[5106] | 38 |
|
---|
| 39 | [TestClass]
|
---|
| 40 | public class ServiceTests {
|
---|
| 41 |
|
---|
| 42 | [ClassInitialize]
|
---|
| 43 | public static void MyClassInitialize(TestContext testContext) {
|
---|
| 44 | PluginLoader.pluginAssemblies.Any();
|
---|
| 45 | ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | private IHiveService GetLocalService() {
|
---|
| 49 | return new HiveService();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | [TestMethod]
|
---|
| 53 | public void TestJobs() {
|
---|
| 54 | var service = GetLocalService();
|
---|
| 55 |
|
---|
| 56 | DT.HiveExperiment experiment = new DT.HiveExperiment() {
|
---|
| 57 | Name = "TestExperiment",
|
---|
| 58 | Description = ""
|
---|
| 59 | };
|
---|
| 60 |
|
---|
| 61 | DT.Job job = new DT.Job() {
|
---|
| 62 | CoresNeeded = 1,
|
---|
| 63 | MemoryNeeded = 0,
|
---|
| 64 | Priority = 0
|
---|
| 65 | };
|
---|
| 66 |
|
---|
| 67 | DT.JobData jobData = new DT.JobData() {
|
---|
| 68 | Data = PersistenceUtil.Serialize(new MockJob(500, true))
|
---|
| 69 | };
|
---|
| 70 |
|
---|
[5404] | 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.PluginId = plugin1.Id;
|
---|
| 80 | pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
|
---|
| 81 | pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
|
---|
| 82 |
|
---|
| 83 | job.PluginsNeededIds.Add(plugin1.Id);
|
---|
| 84 |
|
---|
[5155] | 85 | job.Id = service.AddJob(job, jobData, null);
|
---|
[5106] | 86 | experiment.RootJobId = job.Id;
|
---|
| 87 |
|
---|
| 88 | DT.Job jobLoaded = service.GetJob(job.Id);
|
---|
| 89 | Assert.AreEqual(job.Id, jobLoaded.Id);
|
---|
| 90 | Assert.AreEqual(job.CoresNeeded, jobLoaded.CoresNeeded);
|
---|
| 91 | Assert.AreEqual(job.MemoryNeeded, jobLoaded.MemoryNeeded);
|
---|
| 92 | Assert.AreEqual(job.Priority, jobLoaded.Priority);
|
---|
| 93 | Assert.AreEqual(JobState.Waiting, jobLoaded.JobState);
|
---|
| 94 | Assert.AreEqual(ServiceLocator.Instance.AuthorizationManager.UserId, job.UserId);
|
---|
[5404] | 95 | Assert.IsTrue(job.PluginsNeededIds.SequenceEqual(jobLoaded.PluginsNeededIds));
|
---|
[5106] | 96 |
|
---|
| 97 | DT.JobData jobDataLoaded = service.GetJobData(job.Id);
|
---|
| 98 | Assert.AreEqual(job.Id, jobDataLoaded.JobId);
|
---|
| 99 | Assert.IsTrue(jobData.Data.SequenceEqual(jobDataLoaded.Data));
|
---|
| 100 |
|
---|
| 101 | experiment.Id = service.AddHiveExperiment(experiment);
|
---|
| 102 |
|
---|
| 103 | DT.HiveExperiment experimentLoaded = service.GetHiveExperiment(experiment.Id);
|
---|
| 104 | Assert.AreEqual(experiment.Id, experimentLoaded.Id);
|
---|
| 105 | Assert.AreEqual(experiment.Name, experimentLoaded.Name);
|
---|
| 106 | Assert.AreEqual(experiment.Description, experimentLoaded.Description);
|
---|
| 107 | Assert.AreEqual(experiment.RootJobId, experimentLoaded.RootJobId);
|
---|
| 108 |
|
---|
| 109 | service.DeleteHiveExperiment(experiment.Id);
|
---|
| 110 | Assert.AreEqual(null, service.GetHiveExperiment(experiment.Id));
|
---|
| 111 | Assert.AreEqual(null, service.GetJob(job.Id));
|
---|
| 112 | Assert.AreEqual(null, service.GetJobData(job.Id));
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | List<DT.Job> jobs = new List<DT.Job>();
|
---|
| 116 |
|
---|
| 117 | [TestMethod]
|
---|
| 118 | public void TestHeartbeats() {
|
---|
| 119 | var service = GetLocalService();
|
---|
[5404] | 120 | // check if group already exists and delete
|
---|
| 121 | var existingSlaveGroup = service.GetSlaveGroups().SingleOrDefault(g => g.Name == "TestGroup");
|
---|
| 122 | if (existingSlaveGroup != null) {
|
---|
| 123 | var slavesToDelete = service.GetSlaves().Where(s => s.ParentResourceId == existingSlaveGroup.Id);
|
---|
| 124 | foreach (var slave in slavesToDelete) service.DeleteSlave(slave.Id);
|
---|
| 125 | service.DeleteSlaveGroup(existingSlaveGroup.Id);
|
---|
| 126 | }
|
---|
| 127 |
|
---|
[5106] | 128 | Guid groupId = service.AddSlaveGroup(new SlaveGroup() { Name = "TestGroup", Description = "Used for unit tests" });
|
---|
| 129 |
|
---|
[5404] | 130 | // create slaves
|
---|
[5106] | 131 | var slaves = new List<DT.Slave>();
|
---|
| 132 | for (int i = 0; i < 1; i++) {
|
---|
| 133 | DT.Slave slave = new DT.Slave() {
|
---|
| 134 | Cores = 2,
|
---|
| 135 | Memory = 4096,
|
---|
| 136 | Name = "Slave " + i,
|
---|
| 137 | IsAllowedToCalculate = true,
|
---|
| 138 | SlaveState = SlaveState.Idle,
|
---|
| 139 | CpuSpeed = 2800,
|
---|
| 140 | FreeCores = 2,
|
---|
| 141 | FreeMemory = 3000
|
---|
| 142 | };
|
---|
[5404] | 143 | // check if slave with this name already exists and delete
|
---|
| 144 | var existingSlave = service.GetSlaves().Where(s => s.Name == slave.Name).SingleOrDefault();
|
---|
| 145 | if (existingSlave != null) service.DeleteSlave(existingSlave.Id);
|
---|
| 146 |
|
---|
[5106] | 147 | slave.Id = service.AddSlave(slave);
|
---|
| 148 | service.AddResourceToGroup(groupId, slave.Id);
|
---|
| 149 | slaves.Add(slave);
|
---|
| 150 | }
|
---|
| 151 |
|
---|
[5404] | 152 | // create jobs with different group, they should not be assigned
|
---|
| 153 | existingSlaveGroup = service.GetSlaveGroups().SingleOrDefault(g => g.Name == "DummyGroup");
|
---|
| 154 | if (existingSlaveGroup != null) service.DeleteSlaveGroup(existingSlaveGroup.Id);
|
---|
| 155 |
|
---|
| 156 | Guid dummyGroupId = service.AddSlaveGroup(new SlaveGroup() { Name = "DummyGroup", Description = "Used for unit tests; jobs from this group shall not be calculated" });
|
---|
| 157 | // create dummy jobs
|
---|
| 158 | var dummyJobs = new List<Job>();
|
---|
| 159 | for (int i = 0; i < 2; i++) {
|
---|
| 160 | Job job = new Job() {
|
---|
| 161 | CoresNeeded = 1, MemoryNeeded = 0
|
---|
| 162 | };
|
---|
| 163 | JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
|
---|
| 164 | job.Id = service.AddJob(job, jobData, new List<Guid> { dummyGroupId });
|
---|
| 165 | dummyJobs.Add(job);
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | // create jobs
|
---|
| 169 | for (int i = 0; i < 2; i++) {
|
---|
| 170 | Job job = new Job() {
|
---|
| 171 | CoresNeeded = 1, MemoryNeeded = 0
|
---|
| 172 | };
|
---|
| 173 | JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
|
---|
| 174 | job.Id = service.AddJob(job, jobData, new List<Guid> { groupId });
|
---|
| 175 | jobs.Add(job);
|
---|
| 176 | }
|
---|
| 177 |
|
---|
| 178 | // send heartbeats
|
---|
[5106] | 179 | foreach (var slave in slaves) {
|
---|
| 180 | new Thread(new ParameterizedThreadStart(RunSlaveThread)).Start(slave);
|
---|
| 181 | }
|
---|
| 182 |
|
---|
| 183 | IEnumerable<LightweightJob> lightweightJobs;
|
---|
| 184 | do {
|
---|
| 185 | Thread.Sleep(500);
|
---|
| 186 | lightweightJobs = service.GetLightweightJobs(jobs.Select(x => x.Id));
|
---|
| 187 | } while (!lightweightJobs.All(x => x.JobState == JobState.Finished));
|
---|
| 188 |
|
---|
| 189 | // delete slaves
|
---|
| 190 | foreach (var slave in slaves) {
|
---|
| 191 | service.DeleteSlave(slave.Id);
|
---|
| 192 | Assert.AreEqual(null, service.GetSlave(slave.Id));
|
---|
| 193 | }
|
---|
| 194 |
|
---|
[5404] | 195 | // delete groups
|
---|
[5106] | 196 | service.DeleteSlaveGroup(groupId);
|
---|
[5404] | 197 | service.DeleteSlaveGroup(dummyGroupId);
|
---|
[5106] | 198 |
|
---|
| 199 | // delete jobs
|
---|
| 200 | foreach (var job in jobs) {
|
---|
| 201 | service.DeleteJob(job.Id);
|
---|
| 202 | }
|
---|
[5404] | 203 |
|
---|
| 204 | // delete dummy jobs
|
---|
| 205 | foreach (var job in dummyJobs) {
|
---|
| 206 | service.DeleteJob(job.Id);
|
---|
| 207 | }
|
---|
[5106] | 208 | }
|
---|
| 209 |
|
---|
| 210 | public void RunSlaveThread(object slaveobj) {
|
---|
| 211 | try {
|
---|
| 212 | var service = GetLocalService();
|
---|
| 213 | Slave slave = (Slave)slaveobj;
|
---|
| 214 | int freeCores = slave.Cores.Value;
|
---|
| 215 |
|
---|
[5404] | 216 | for (int i = 0; i < 10; i++) {
|
---|
[5106] | 217 |
|
---|
[5404] | 218 | var messages = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, FreeMemory = 2423, FreeCores = freeCores, JobProgress = new Dictionary<Guid, TimeSpan>() });
|
---|
| 219 | if (messages.Count == 0) {
|
---|
| 220 | Debug.WriteLine("No job available");
|
---|
| 221 | return; // no more jobs
|
---|
| 222 | }
|
---|
[5106] | 223 |
|
---|
[5404] | 224 | Debug.WriteLine("Messages: {0}", string.Join(", ", messages.Select(m => m.Message)));
|
---|
| 225 |
|
---|
| 226 | Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.AbortJob).Count() == 0);
|
---|
| 227 | Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.SayHello).Count() == 0);
|
---|
| 228 | Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.PauseJob).Count() == 0);
|
---|
| 229 |
|
---|
| 230 | var calculateJobMessage = messages.Where(x => x.Message == MessageContainer.MessageType.CalculateJob).SingleOrDefault();
|
---|
| 231 | if (calculateJobMessage != null) {
|
---|
| 232 | if (!jobs.Select(j => j.Id).Contains(calculateJobMessage.JobId))
|
---|
| 233 | Assert.Fail("Got job which was not assigned to the slavegroup");
|
---|
| 234 |
|
---|
| 235 | Debug.WriteLine("Job available, calculating");
|
---|
| 236 | Job job = service.GetJob(calculateJobMessage.JobId);
|
---|
| 237 |
|
---|
| 238 | JobData jobData = service.GetJobData(job.Id);
|
---|
| 239 | IJob deserializedJob = PersistenceUtil.Deserialize<IJob>(jobData.Data);
|
---|
| 240 | deserializedJob.Start();
|
---|
| 241 | job.JobState = JobState.Finished;
|
---|
| 242 | jobs.Where(x => x.Id == job.Id).Single().JobState = JobState.Finished;
|
---|
| 243 | jobData.Data = PersistenceUtil.Serialize(deserializedJob);
|
---|
| 244 | service.UpdateJob(job, jobData);
|
---|
| 245 | Debug.WriteLine("finished calculating");
|
---|
| 246 | }
|
---|
[5106] | 247 | }
|
---|
| 248 | }
|
---|
| 249 | catch (Exception e) {
|
---|
| 250 | Assert.Fail(e.Message, e);
|
---|
| 251 | }
|
---|
| 252 | }
|
---|
| 253 | }
|
---|
| 254 | }
|
---|