Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 5955 was 5786, checked in by cneumuel, 14 years ago

#1233

  • implemented correct numbering of BatchRuns
  • improvements in ExperimentManager
  • fixed bug in server (jobs were scheduled multiple times)
  • added exception handling for task in slave
  • improved timeout handling of jobs (LifecycleManager)
File size: 9.3 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Services.Hive.Common.DataTransfer;
4using HeuristicLab.Services.Hive.Common.ServiceContracts;
5using HeuristicLab.Services.Hive.DataAccess;
6using Microsoft.VisualStudio.TestTools.UnitTesting;
7
8namespace HeuristicLab.Services.Hive.Tests {
9  using System.Threading;
10  using DT = HeuristicLab.Services.Hive.Common.DataTransfer;
11
12  [TestClass]
13  public class DaoTests {
14
15    [ClassInitialize]
16    public static void MyClassInitialize(TestContext testContext) {
17      PluginLoader.pluginAssemblies.Any();
18      ServiceLocator.Instance = new MockServiceLocator(ServiceLocator.Instance);
19    }
20
21    private IHiveService GetLocalService() {
22      return new HiveService();
23    }
24
25    [TestMethod]
26    public void TestJobDao() {
27      IHiveDao dao = ServiceLocator.Instance.HiveDao;
28
29      DT.Job job1 = new DT.Job();
30      job1.State = JobState.Offline;
31      job1.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
32      job1.Command = Command.Pause;
33
34      DT.JobData jobData1 = new DT.JobData();
35      jobData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
36      jobData1.LastUpdate = DateTime.Now;
37
38      DT.Plugin plugin1 = new DT.Plugin();
39      plugin1.Name = "Tests.MyPlugin";
40      plugin1.Version = new Version("1.0.0.0");
41      plugin1.UserId = Guid.Empty;
42      plugin1.IsLocal = true;
43      plugin1.DateCreated = DateTime.Now;
44
45      DT.PluginData pluginData1 = new DT.PluginData();
46      pluginData1.PluginId = plugin1.Id;
47      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
48      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
49
50      plugin1.Id = dao.AddPlugin(plugin1);
51      pluginData1.PluginId = plugin1.Id;
52      pluginData1.Id = dao.AddPluginData(pluginData1);
53
54      job1.PluginsNeededIds.Add(plugin1.Id);
55
56      job1.Id = dao.AddJob(job1);
57      jobData1.JobId = job1.Id;
58      dao.AddJobData(jobData1);
59
60      DT.Job job1loaded = dao.GetJob(job1.Id);
61      Assert.AreEqual(job1.Id, job1loaded.Id);
62      Assert.AreEqual(job1.CoresNeeded, job1loaded.CoresNeeded);
63      Assert.AreEqual(job1.DateCreated.ToString(), job1loaded.DateCreated.ToString());
64      Assert.AreEqual(null, job1loaded.DateFinished);
65      Assert.IsTrue(job1.PluginsNeededIds.SequenceEqual(job1loaded.PluginsNeededIds));
66      Assert.AreEqual(job1.StateLog.Count, job1loaded.StateLog.Count);
67      Assert.AreEqual(job1.Command, job1loaded.Command);
68      Assert.IsTrue(Math.Abs((job1loaded.LastJobDataUpdate - jobData1.LastUpdate).TotalSeconds) < 1);
69      for (int i = 0; i < job1.StateLog.Count; i++) {
70        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
71        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
72        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
73        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
74        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
75        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
76      }
77     
78      job1 = job1loaded;
79
80      // test jobstates
81      job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
82      job1.StateLog.Add(new DT.StateLog { State = JobState.Calculating, DateTime = DateTime.Now }); Thread.Sleep(10);
83      job1.StateLog.Add(new DT.StateLog { State = JobState.Transferring, DateTime = DateTime.Now }); Thread.Sleep(10);
84      job1.StateLog.Add(new DT.StateLog { State = JobState.Finished, DateTime = DateTime.Now }); Thread.Sleep(10);
85      dao.UpdateJob(job1);
86
87      job1loaded = dao.GetJob(job1.Id);
88      for (int i = 0; i < job1.StateLog.Count; i++) {
89        Assert.AreEqual(job1.Id, job1loaded.StateLog[i].JobId);
90        Assert.AreEqual(job1.StateLog[i].State, job1loaded.StateLog[i].State);
91        Assert.AreEqual(job1.StateLog[i].SlaveId, job1loaded.StateLog[i].SlaveId);
92        Assert.AreEqual(job1.StateLog[i].UserId, job1loaded.StateLog[i].UserId);
93        Assert.AreEqual(job1.StateLog[i].Exception, job1loaded.StateLog[i].Exception);
94        Assert.IsTrue(Math.Abs((job1.StateLog[i].DateTime - job1loaded.StateLog[i].DateTime).TotalSeconds) < 1);
95      }
96
97      DT.JobData jobData1Loaded = dao.GetJobData(job1.Id);
98      Assert.AreEqual(jobData1.JobId, jobData1Loaded.JobId);
99      Assert.IsTrue(Math.Abs((jobData1.LastUpdate - jobData1Loaded.LastUpdate).TotalSeconds) < 1);
100      Assert.IsTrue(jobData1.Data.SequenceEqual(jobData1Loaded.Data));
101     
102      dao.DeleteJob(job1.Id);
103
104      Assert.AreEqual(null, dao.GetJob(job1.Id));
105      Assert.AreEqual(null, dao.GetJobData(job1.Id));
106    }
107
108    [TestMethod]
109    public void TestSlaveDao() {
110      IHiveDao dao = ServiceLocator.Instance.HiveDao;
111      DT.SlaveGroup slaveGroup = new DT.SlaveGroup() {
112        Name = "Test"
113      };
114      slaveGroup.Id = dao.AddSlaveGroup(slaveGroup);
115
116      DT.Slave slave = new DT.Slave() {
117        Id = Guid.NewGuid(),
118        Name = "Test",
119        OperatingSystem = null, //"Windows 3.11",
120        Cores = 2,
121        Memory = 1024,
122        FreeMemory = 512
123      };
124
125      Assert.AreEqual(slave.Id, dao.AddSlave(slave));
126
127      // update
128      slave.FreeMemory = 255;
129      slave.OperatingSystem = Environment.OSVersion.VersionString;
130      dao.UpdateSlave(slave);
131
132      DT.Slave slaveLoaded = dao.GetSlave(slave.Id);
133      Assert.AreEqual(slave.FreeMemory, slaveLoaded.FreeMemory);
134
135      dao.DeleteSlave(slave.Id);
136      dao.DeleteSlaveGroup(slaveGroup.Id);
137    }
138
139    [TestMethod]
140    public void TestPluginDao() {
141      IHiveDao dao = ServiceLocator.Instance.HiveDao;
142
143      DT.Plugin plugin1 = new DT.Plugin();
144      plugin1.DateCreated = DateTime.Now;
145      plugin1.IsLocal = false;
146      plugin1.Name = "Tests.MyPlugin";
147      plugin1.Version = new Version("1.0.0.0");
148      plugin1.UserId = Guid.Empty;
149
150      plugin1.Id = dao.AddPlugin(plugin1);
151
152      DT.Plugin plugin1loaded = dao.GetPlugin(plugin1.Id);
153      Assert.AreEqual(plugin1.Id, plugin1loaded.Id);
154      Assert.AreEqual(plugin1.Name, plugin1loaded.Name);
155      Assert.AreEqual(plugin1.Version, plugin1loaded.Version);
156      Assert.AreEqual(plugin1.UserId, plugin1loaded.UserId);
157      Assert.AreEqual(plugin1.DateCreated.ToString(), plugin1loaded.DateCreated.ToString());
158      Assert.AreEqual(plugin1.IsLocal, plugin1loaded.IsLocal);
159
160      DT.PluginData pluginData1 = new DT.PluginData();
161      pluginData1.PluginId = plugin1.Id;
162      pluginData1.FileName = "Tests.MyPlugin-1.0.dll";
163      pluginData1.Data = new byte[] { 0, 1, 2, 3, 4, 5 };
164
165      pluginData1.Id = dao.AddPluginData(pluginData1);
166
167      DT.PluginData pluginData1loaded = dao.GetPluginData(pluginData1.Id);
168      Assert.AreEqual(pluginData1.Id, pluginData1loaded.Id);
169
170      Assert.AreEqual(pluginData1.PluginId, pluginData1loaded.PluginId);
171      Assert.AreEqual(pluginData1.FileName, pluginData1loaded.FileName);
172      Assert.IsTrue(pluginData1.Data.SequenceEqual(pluginData1loaded.Data));
173
174      dao.DeletePluginData(pluginData1.Id);
175      dao.DeletePlugin(plugin1.Id);
176
177      Assert.AreEqual(null, dao.GetPlugin(plugin1.Id));
178      Assert.AreEqual(null, dao.GetPluginData(pluginData1.Id));
179    }
180
181    [TestMethod]
182    public void TestHiveExperimentDao() {
183      IHiveDao dao = ServiceLocator.Instance.HiveDao;
184
185      DT.HiveExperiment he = new DT.HiveExperiment();
186
187      DT.Job job = new DT.Job();
188      job.State = JobState.Offline;
189      job.StateLog.Add(new DT.StateLog { State = JobState.Offline, DateTime = DateTime.Now });
190      job.Id = dao.AddJob(job);
191
192      he.RootJobId = job.Id;
193      he.DateCreated = DateTime.Now;
194      he.Name = "TestExperiment";
195      he.OwnerUserId = Guid.NewGuid();
196      he.ResourceNames = "HEAL";
197
198      he.Id = dao.AddHiveExperiment(he);
199
200      DT.HiveExperimentPermission perm = new DT.HiveExperimentPermission();
201      perm.HiveExperimentId = he.Id;
202      perm.GrantedByUserId = he.OwnerUserId;
203      perm.GrantedUserId = Guid.NewGuid();
204      perm.Permission = Permission.Write;
205      dao.AddHiveExperimentPermission(perm);
206
207      DT.HiveExperiment heLoaded = dao.GetHiveExperiment(he.Id);
208      Assert.AreEqual(he.Id, heLoaded.Id);
209      Assert.AreEqual(he.Name, heLoaded.Name);
210      Assert.AreEqual(he.ResourceNames, heLoaded.ResourceNames);
211      Assert.AreEqual(he.RootJobId, heLoaded.RootJobId);
212      //Assert.AreEqual(he.LastAccessed, heLoaded.LastAccessed);
213      //Assert.AreEqual(he.DateCreated, heLoaded.DateCreated);
214
215      DT.Job jobLoaded = dao.GetJob(he.RootJobId);
216      Assert.AreEqual(job.Id, jobLoaded.Id);
217      Assert.AreEqual(job.State, jobLoaded.State);
218
219      DT.HiveExperimentPermission permLoaded = dao.GetHiveExperimentPermission(he.Id, perm.GrantedUserId);
220      Assert.AreEqual(perm.HiveExperimentId, permLoaded.HiveExperimentId);
221      Assert.AreEqual(perm.GrantedUserId, permLoaded.GrantedUserId);
222      Assert.AreEqual(perm.GrantedByUserId, permLoaded.GrantedByUserId);
223      Assert.AreEqual(perm.Permission, permLoaded.Permission);
224
225      dao.DeleteHiveExperiment(he.Id);
226      Assert.AreEqual(null, dao.GetHiveExperiment(he.Id));
227      Assert.AreEqual(null, dao.GetJob(he.RootJobId));
228      Assert.AreEqual(null, dao.GetHiveExperimentPermission(perm.HiveExperimentId, perm.GrantedUserId));
229
230    }
231  }
232}
Note: See TracBrowser for help on using the repository browser.