Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Services.Hive.Tests/DaoTests.cs @ 6723

Last change on this file since 6723 was 6723, checked in by ascheibe, 13 years ago

#1233 Review comments: renamed HiveEperiment to Job

File size: 13.1 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 System.Threading;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27using DT = HeuristicLab.Services.Hive.DataTransfer;
28
29namespace HeuristicLab.Services.Hive.Tests {
30  [TestClass]
31  public class DaoTests {
32
33    [ClassInitialize]
34    public static void MyClassInitialize(TestContext testContext) {
35      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
36    }
37
38    private HeuristicLab.Services.Hive.ServiceContracts.IHiveService GetLocalService() {
39      return new HeuristicLab.Services.Hive.HiveService();
40    }
41
42    [TestMethod]
43    public void TestJobDao() {
44      IHiveDao dao = ServiceLocator.Instance.HiveDao;
45
46      DT.Job he = new DT.Job();
47      he.DateCreated = DateTime.Now;
48      he.Name = "TestExperiment";
49      he.OwnerUserId = Guid.NewGuid();
50      he.ResourceNames = "HEAL";
51      he.Id = dao.AddHiveExperiment(he);
52
53      DT.Task job1 = new DT.Task();
54      job1.State = DT.TaskState.Offline;
55      job1.StateLog.Add(new DT.StateLog { State = DT.TaskState.Offline, DateTime = DateTime.Now });
56      job1.Command = DT.Command.Pause;
57      job1.JobId = he.Id;
58      job1.IsPrivileged = true;
59
60      DT.TaskData jobData1 = new DT.TaskData();
61      jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
62      jobData1.LastUpdate = DateTime.Now;
63
64      DT.Plugin plugin1 = new DT.Plugin();
65      plugin1.Name = "Tests.MyPlugin";
66      plugin1.Version = new Version("1.0.0.0");
67      plugin1.UserId = Guid.Empty;
68      plugin1.DateCreated = DateTime.Now;
69      plugin1.Hash = new byte[] { 1, 2, 3 };
70
71      DT.PluginData pluginData1 = new DT.PluginData();
72      pluginData1.PluginId = plugin1.Id;
73      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
74      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
75
76      plugin1.Id = dao.AddPlugin(plugin1);
77      pluginData1.PluginId = plugin1.Id;
78      pluginData1.Id = dao.AddPluginData(pluginData1);
79
80      job1.PluginsNeededIds.Add(plugin1.Id);
81
82      job1.Id = dao.AddJob(job1);
83      jobData1.TaskId = job1.Id;
84      dao.AddJobData(jobData1);
85
86      DT.Task job1loaded = dao.GetJob(job1.Id);
87      Assert.AreEqual(job1.Id, job1loaded.Id);
88      Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded);
89      Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString());
90      Assert.AreEqual(null, job1loaded.DateFinished);
91      Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds));
92      Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count);
93      Assert.AreEqual(job1.Command, job1loaded.Command);
94      Assert.AreEqual(job1.JobId, job1loaded.JobId);
95      Assert.AreEqual(job1.IsPrivileged, job1loaded.IsPrivileged);
96      Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1);
97      for (int i = 0; i < job1.StateLog.Count; i++) {
98        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].TaskId);
99        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
100        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
101        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
102        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
103        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
104      }
105
106      job1 = job1loaded;
107
108      // test jobstates
109      job1.StateLog.Add(new DT.StateLog { State = DT.TaskState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
110      job1.StateLog.Add(new DT.StateLog { State = DT.TaskState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10);
111      job1.StateLog.Add(new DT.StateLog { State = DT.TaskState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
112      job1.StateLog.Add(new DT.StateLog { State = DT.TaskState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10);
113      dao.UpdateJob(job1);
114
115      job1loaded = dao.GetJob(job1.Id);
116      for (int i = 0; i < job1.StateLog.Count; i++) {
117        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].TaskId);
118        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
119        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
120        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
121        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
122        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
123      }
124
125      DT.TaskData jobData1Loaded = dao.GetJobData(job1.Id);
126      Assert.AreEqual(jobData1.TaskId, jobData1Loaded.TaskId);
127      Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1);
128      Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data));
129
130      dao.DeleteHiveExperiment(he.Id);
131
132      Assert.AreEqual(null, dao.GetJob(job1.Id));
133      Assert.AreEqual(null, dao.GetJobData(job1.Id));
134      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
135
136      dao.DeletePlugin(plugin1.Id);
137      Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
138    }
139
140    [TestMethod]
141    public void TestSlaveDao() {
142      IHiveDao dao = ServiceLocator.Instance.HiveDao;
143      DT.SlaveGroup slaveGroup = new DT.SlaveGroup() {
144        Name = "Test"
145      };
146      slaveGroup.Id = dao.AddSlaveGroup(slaveGroup);
147
148      DT.Slave slave = new DT.Slave() {
149        Id = Guid.NewGuid(),
150        Name = "Test",
151        OperatingSystem = null, //"Windows 3.11",
152        Cores = 2,
153        Memory = 1024,
154        FreeMemory = 512
155      };
156
157      Assert.AreEqual(slave.Id, dao.AddSlave(slave));
158
159      // update
160      slave.FreeMemory = 255;
161      slave.OperatingSystem = Environment.OSVersion.VersionString;
162      dao.UpdateSlave(slave);
163
164      DT.Slave slaveLoaded = dao.GetSlave(slave.Id);
165      Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory);
166
167      dao.DeleteSlave(slave.Id);
168      dao.DeleteSlaveGroup(slaveGroup.Id);
169    }
170
171    [TestMethod]
172    public void TestPluginDao() {
173      IHiveDao dao = ServiceLocator.Instance.HiveDao;
174
175      DT.Plugin plugin1 = new DT.Plugin();
176      plugin1.DateCreated = DateTime.Now;
177      plugin1.Name = "Tests.MyPlugin";
178      plugin1.Version = new Version("1.0.0.0");
179      plugin1.UserId = Guid.Empty;
180      plugin1.Hash = new byte[] { 1, 2, 3 };
181
182      plugin1.Id = dao.AddPlugin(plugin1);
183
184      DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id);
185      Assert.AreEqual(plugin1.Id, plugin1loaded.Id);
186      Assert.AreEqual(plugin1.Name, plugin1loaded.Name);
187      Assert.AreEqual(plugin1.Version, plugin1loaded.Version);
188      Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId);
189      Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString());
190      Assert.IsTrue(plugin1.Hash.SequenceEqual(plugin1loaded.Hash));
191
192      DT.PluginData pluginData1 = new DT.PluginData();
193      pluginData1.PluginId = plugin1.Id;
194      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
195      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
196
197      pluginData1.Id = dao.AddPluginData(pluginData1);
198
199      DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id);
200      Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id);
201
202      Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId);
203      Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName);
204      Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data));
205
206      dao.DeletePluginData(pluginData1.Id);
207      dao.DeletePlugin(plugin1.Id);
208
209      Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
210      Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id));
211    }
212
213    [TestMethod]
214    public void TestHiveExperimentDao() {
215      IHiveDao dao = ServiceLocator.Instance.HiveDao;
216
217      DT.Job he = new DT.Job();
218      he.DateCreated = DateTime.Now;
219      he.Name = "TestExperiment";
220      he.OwnerUserId = Guid.NewGuid();
221      he.ResourceNames = "HEAL";
222      he.Id = dao.AddHiveExperiment(he);
223
224      DT.Task job = new DT.Task();
225      job.State = DT.TaskState.Offline;
226      job.StateLog.Add(new DT.StateLog { State = DT.TaskState.Offline, DateTime = DateTime.Now });
227      job.JobId = he.Id;
228      job.Id = dao.AddJob(job);
229
230      DT.JobPermission perm = new DT.JobPermission();
231      perm.JobId = he.Id;
232      perm.GrantedByUserId = he.OwnerUserId;
233      perm.GrantedUserId = Guid.NewGuid();
234      perm.Permission = DT.Permission.Full;
235      dao.AddHiveExperimentPermission(perm);
236
237      DT.Job heLoaded = dao.GetHiveExperiment(he.Id);
238      Assert.AreEqual(he.Id, heLoaded.Id);
239      Assert.AreEqual(he.Name, heLoaded.Name);
240      Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames);
241      //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
242      //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
243
244      DT.Task jobLoaded = dao.GetJob(job.Id);
245      Assert.AreEqual(job.Id, jobLoaded.Id);
246      Assert.AreEqual(job.State, jobLoaded.State);
247      Assert.AreEqual(job.JobId, jobLoaded.JobId);
248
249      DT.JobPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId);
250      Assert.AreEqual(perm.JobId, permLoaded.JobId);
251      Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId);
252      Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId);
253      Assert.AreEqual(perm.Permission, permLoaded.Permission);
254
255      dao.DeleteHiveExperiment(he.Id);
256      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
257      Assert.AreEqual(null, dao.GetJob(job.Id));
258      Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.JobId, perm.GrantedUserId));
259
260    }
261
262
263    [TestMethod]
264    public void TestStatistics() {
265      IHiveDao dao = ServiceLocator.Instance.HiveDao;
266
267      var stats = new DT.Statistics();
268      stats.TimeStamp = DateTime.Now;
269      var slaveStats = new List<DT.SlaveStatistics>();
270      slaveStats.Add(new DT.SlaveStatistics() { Cores = 8, FreeCores = 3, Memory = 8000, FreeMemory = 3000, CpuUtilization = 0.8, SlaveId = Guid.NewGuid() });
271      slaveStats.Add(new DT.SlaveStatistics() { Cores = 2, FreeCores = 0, Memory = 1024, FreeMemory = 100, CpuUtilization = 0.99, SlaveId = Guid.NewGuid() });
272      slaveStats.Add(new DT.SlaveStatistics() { Cores = 4, FreeCores = 4, Memory = 3024, FreeMemory = 2300, CpuUtilization = 0.01, SlaveId = Guid.NewGuid() });
273      stats.SlaveStatistics = slaveStats;
274
275      var userStats = new List<DT.UserStatistics>();
276      userStats.Add(new DT.UserStatistics() { UsedCores = 15, ExecutionTime = TimeSpan.FromHours(123), UserId = Guid.NewGuid() });
277      userStats.Add(new DT.UserStatistics() { UsedCores = 42, ExecutionTime = TimeSpan.FromHours(945), UserId = Guid.NewGuid() });
278      stats.UserStatistics = userStats;
279
280      stats.Id = dao.AddStatistics(stats);
281
282      var statsLoaded = dao.GetStatistic(stats.Id);
283      Assert.AreEqual(stats.Id, statsLoaded.Id);
284      Assert.IsTrue(Math.Abs((stats.TimeStamp - statsLoaded.TimeStamp).TotalSeconds) < 1);
285
286      for (int i = 0; i < slaveStats.Count; i++) {
287        Assert.AreEqual(slaveStats[i].Cores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Cores);
288        Assert.AreEqual(slaveStats[i].FreeCores, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeCores);
289        Assert.AreEqual(slaveStats[i].Memory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).Memory);
290        Assert.AreEqual(slaveStats[i].FreeMemory, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).FreeMemory);
291        Assert.AreEqual(slaveStats[i].CpuUtilization, statsLoaded.SlaveStatistics.Single(x => x.SlaveId == slaveStats[i].SlaveId).CpuUtilization);
292      }
293
294      for (int i = 0; i < userStats.Count; i++) {
295        Assert.AreEqual(userStats[i].ExecutionTime, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).ExecutionTime);
296        Assert.AreEqual(userStats[i].UsedCores, statsLoaded.UserStatistics.Single(x => x.UserId == userStats[i].UserId).UsedCores);
297      }
298
299      dao.DeleteStatistics(stats.Id);
300      Assert.AreEqual(null, dao.GetStatistic(stats.Id));
301    }
302  }
303}
Note: See TracBrowser for help on using the repository browser.