Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5234 was 5156, checked in by ascheibe, 14 years ago

#1233

  • Implemented communication interface between Slave and a Slave Client using Named Pipes and callbacks
  • Added new project for testing Slave - Client communication
  • Added some copyright info headers
File size: 6.9 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Hive;
26using HeuristicLab.Clients.Hive.Slave.Tests;
27using HeuristicLab.Services.Hive.Common.DataTransfer;
28using HeuristicLab.Services.Hive.Common.ServiceContracts;
29using Microsoft.VisualStudio.TestTools.UnitTesting;
30
31namespace HeuristicLab.Services.Hive.Tests {
32  using System.Threading;
33  using HeuristicLab.Hive;
34  using HeuristicLab.Services.Hive.Common;
35  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
36
37  [TestClass]
38  public class ServiceTests {
39
40    [ClassInitialize]
41    public static void MyClassInitialize(TestContext testContext) {
42      PluginLoader.pluginAssemblies.Any();
43      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
44    }
45
46    private IHiveService GetLocalService() {
47      return new HiveService();
48    }
49
50    [TestMethod]
51    public void TestJobs() {
52      var service = GetLocalService();
53
54      DT.HiveExperiment experiment = new DT.HiveExperiment() {
55        Name = "TestExperiment",
56        Description = ""
57      };
58
59      DT.Job job = new DT.Job() {
60        CoresNeeded = 1,
61        MemoryNeeded = 0,
62        Priority = 0
63      };
64
65      DT.JobData jobData = new DT.JobData() {
66        Data = PersistenceUtil.Serialize(new MockJob(500, true))
67      };
68
69      job.Id = service.AddJob(job, jobData, null);
70      experiment.RootJobId = job.Id;
71
72      DT.Job jobLoaded = service.GetJob(job.Id);
73      Assert.AreEqual(job.Id, jobLoaded.Id);
74      Assert.AreEqual(job.CoresNeeded, jobLoaded.CoresNeeded);
75      Assert.AreEqual(job.MemoryNeeded, jobLoaded.MemoryNeeded);
76      Assert.AreEqual(job.Priority, jobLoaded.Priority);
77      Assert.AreEqual(JobState.Waiting, jobLoaded.JobState);
78      Assert.AreEqual(ServiceLocator.Instance.AuthorizationManager.UserId, job.UserId);
79
80      DT.JobData jobDataLoaded = service.GetJobData(job.Id);
81      Assert.AreEqual(job.Id, jobDataLoaded.JobId);
82      Assert.IsTrue(jobData.Data.SequenceEqual(jobDataLoaded.Data));
83
84      experiment.Id = service.AddHiveExperiment(experiment);
85
86      DT.HiveExperiment experimentLoaded = service.GetHiveExperiment(experiment.Id);
87      Assert.AreEqual(experiment.Id, experimentLoaded.Id);
88      Assert.AreEqual(experiment.Name, experimentLoaded.Name);
89      Assert.AreEqual(experiment.Description, experimentLoaded.Description);
90      Assert.AreEqual(experiment.RootJobId, experimentLoaded.RootJobId);
91
92      service.DeleteHiveExperiment(experiment.Id);
93      Assert.AreEqual(null, service.GetHiveExperiment(experiment.Id));
94      Assert.AreEqual(null, service.GetJob(job.Id));
95      Assert.AreEqual(null, service.GetJobData(job.Id));
96    }
97
98    List<DT.Job> jobs = new List<DT.Job>();
99
100    [TestMethod]
101    public void TestHeartbeats() {
102      var service = GetLocalService();
103      Guid groupId = service.AddSlaveGroup(new SlaveGroup() { Name = "TestGroup", Description = "Used for unit tests" });
104
105      for (int i = 0; i < 2; i++) {
106        Job job = new Job() {
107          CoresNeeded = 1, MemoryNeeded = 0
108        };
109        JobData jobData = new JobData() { Data = PersistenceUtil.Serialize(new MockJob(500, false)) };
110        job.Id = service.AddJob(job, jobData, null);
111        jobs.Add(job);
112      }
113
114      var slaves = new List<DT.Slave>();
115      for (int i = 0; i < 1; i++) {
116        DT.Slave slave = new DT.Slave() {
117          Cores = 2,
118          Memory = 4096,
119          Name = "Slave " + i,
120          IsAllowedToCalculate = true,
121          SlaveState = SlaveState.Idle,
122          CpuSpeed = 2800,
123          FreeCores = 2,
124          FreeMemory = 3000
125        };
126        slave.Id = service.AddSlave(slave);
127        service.AddResourceToGroup(groupId, slave.Id);
128        slaves.Add(slave);
129      }
130
131      foreach (var slave in slaves) {
132        new Thread(new ParameterizedThreadStart(RunSlaveThread)).Start(slave);
133      }
134
135      // send heartbeats
136      IEnumerable<LightweightJob> lightweightJobs;
137      do {
138        Thread.Sleep(500);
139        lightweightJobs = service.GetLightweightJobs(jobs.Select(x => x.Id));
140      } while (!lightweightJobs.All(x => x.JobState == JobState.Finished));
141
142      // delete slaves
143      foreach (var slave in slaves) {
144        service.DeleteSlave(slave.Id);
145        Assert.AreEqual(null, service.GetSlave(slave.Id));
146      }
147
148      // delete group
149      service.DeleteSlaveGroup(groupId);
150
151      // delete jobs
152      foreach (var job in jobs) {
153        service.DeleteJob(job.Id);
154      }
155    }
156
157    public void RunSlaveThread(object slaveobj) {
158      try {
159        var service = GetLocalService();
160        Slave slave = (Slave)slaveobj;
161        int freeCores = slave.Cores.Value;
162
163        var messages = service.Heartbeat(new Heartbeat() { SlaveId = slave.Id, FreeMemory = 2423, FreeCores = freeCores, JobProgress = new Dictionary<Guid, TimeSpan>() });
164        if (messages.Count == 0)
165          return; // no more jobs
166
167        Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.AbortJob).Count() == 0);
168        Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.SayHello).Count() == 0);
169        Assert.IsTrue(messages.Where(x => x.Message == MessageContainer.MessageType.PauseJob).Count() == 0);
170
171        if (messages.Where(x => x.Message == MessageContainer.MessageType.AquireJob).Count() > 0) {
172          Guid jobId = messages.Where(x => x.Message == MessageContainer.MessageType.AquireJob).SingleOrDefault().JobId;
173          service.AquireJob(jobId);
174          Job job = service.GetJob(jobId);
175          JobData jobData = service.GetJobData(jobId);
176          IJob deserializedJob = PersistenceUtil.Deserialize<IJob>(jobData.Data);
177          deserializedJob.Start();
178          job.JobState = JobState.Finished;
179          jobs.Where(x => x.Id == jobId).Single().JobState = JobState.Finished;
180          jobData.Data = PersistenceUtil.Serialize(deserializedJob);
181          service.UpdateJob(job, jobData);
182        }
183      }
184      catch (Exception e) {
185        Assert.Fail(e.Message, e);
186      }
187    }
188  }
189}
Note: See TracBrowser for help on using the repository browser.