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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.ServiceModel;
|
---|
26 | using HeuristicLab.Clients.Common;
|
---|
27 | using HeuristicLab.Services.Hive.Common;
|
---|
28 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
29 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
30 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Clients.Hive.Slave.Tests {
|
---|
33 | [TestClass]
|
---|
34 | public class SlaveTest {
|
---|
35 |
|
---|
36 | private static ServiceHost slaveComm;
|
---|
37 | private static SlaveCommListener listener;
|
---|
38 |
|
---|
39 | // Use ClassInitialize to run code before running the first test in the class
|
---|
40 | [ClassInitialize]
|
---|
41 | public static void MyClassInitialize(TestContext testContext) {
|
---|
42 | PluginLoader.pluginAssemblies.Any();
|
---|
43 | ServiceLocator.Instance = new MockServiceLocator();
|
---|
44 | slaveComm = new ServiceHost(typeof(SlaveCommunicationService));
|
---|
45 | slaveComm.Open();
|
---|
46 | listener = new SlaveCommListener();
|
---|
47 | listener.Open();
|
---|
48 | }
|
---|
49 |
|
---|
50 | [ClassCleanup]
|
---|
51 | public static void MyClassCleanup() {
|
---|
52 | listener.Close();
|
---|
53 | slaveComm.Close();
|
---|
54 | }
|
---|
55 |
|
---|
56 |
|
---|
57 | private List<List<MessageContainer>> CreateMsgsForSingleJob() {
|
---|
58 | List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
|
---|
59 | //get slave to fetch job
|
---|
60 | List<MessageContainer> msg = new List<MessageContainer>();
|
---|
61 | msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
|
---|
62 | allMsgs.Add(msg);
|
---|
63 |
|
---|
64 | //do nothing
|
---|
65 | msg = new List<MessageContainer>();
|
---|
66 | allMsgs.Add(msg);
|
---|
67 | msg = new List<MessageContainer>();
|
---|
68 | allMsgs.Add(msg);
|
---|
69 | msg = new List<MessageContainer>();
|
---|
70 | allMsgs.Add(msg);
|
---|
71 | msg = new List<MessageContainer>();
|
---|
72 | allMsgs.Add(msg);
|
---|
73 | msg = new List<MessageContainer>();
|
---|
74 | allMsgs.Add(msg);
|
---|
75 |
|
---|
76 | msg = new List<MessageContainer>();
|
---|
77 | msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
|
---|
78 | allMsgs.Add(msg);
|
---|
79 | return allMsgs;
|
---|
80 | }
|
---|
81 |
|
---|
82 | [TestMethod]
|
---|
83 | public void TestSingleJob() {
|
---|
84 | Job testJob = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
85 | List<Job> jobList = new List<Job>();
|
---|
86 | jobList.Add(testJob);
|
---|
87 | MockJob job = new MockJob(300, false);
|
---|
88 |
|
---|
89 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
90 | MockHiveService ms = (MockHiveService)service.Obj;
|
---|
91 | ((MockHiveService)service.Obj).Messages = CreateMsgsForSingleJob();
|
---|
92 | ((MockHiveService)service.Obj).updateJobs(jobList, job);
|
---|
93 |
|
---|
94 | HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
|
---|
95 | core.Start();
|
---|
96 |
|
---|
97 | Assert.AreEqual<int>(1, ms.ResultJobs.Count);
|
---|
98 | Assert.AreEqual<Guid>(testJob.Id, ms.ResultJobs[0].Id);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private List<List<MessageContainer>> CreateMsgsForShutdownSlaveWhileJobRunning() {
|
---|
103 | List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
|
---|
104 | //get slave to fetch job
|
---|
105 | List<MessageContainer> msg = new List<MessageContainer>();
|
---|
106 | msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
|
---|
107 | allMsgs.Add(msg);
|
---|
108 |
|
---|
109 | msg = new List<MessageContainer>();
|
---|
110 | allMsgs.Add(msg);
|
---|
111 |
|
---|
112 | msg = new List<MessageContainer>();
|
---|
113 | msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
|
---|
114 | allMsgs.Add(msg);
|
---|
115 | return allMsgs;
|
---|
116 | }
|
---|
117 |
|
---|
118 | [TestMethod]
|
---|
119 | public void TestShutdownSlaveWhileJobRunning() {
|
---|
120 | Job testJob = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
121 | List<Job> jobList = new List<Job>();
|
---|
122 | jobList.Add(testJob);
|
---|
123 | MockJob job = new MockJob(10000, false);
|
---|
124 |
|
---|
125 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
126 | MockHiveService ms = (MockHiveService)service.Obj;
|
---|
127 | ((MockHiveService)service.Obj).Messages = CreateMsgsForShutdownSlaveWhileJobRunning();
|
---|
128 | ((MockHiveService)service.Obj).updateJobs(jobList, job);
|
---|
129 |
|
---|
130 |
|
---|
131 | HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
|
---|
132 | core.Start();
|
---|
133 |
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 |
|
---|
138 | private List<List<MessageContainer>> CreateMsgsForTwoJobs() {
|
---|
139 | List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
|
---|
140 |
|
---|
141 | //get slave to fetch jobs
|
---|
142 | List<MessageContainer> msg = new List<MessageContainer>();
|
---|
143 | msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
|
---|
144 | allMsgs.Add(msg);
|
---|
145 |
|
---|
146 | msg = new List<MessageContainer>();
|
---|
147 | allMsgs.Add(msg);
|
---|
148 |
|
---|
149 | msg = new List<MessageContainer>();
|
---|
150 | msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
|
---|
151 | allMsgs.Add(msg);
|
---|
152 |
|
---|
153 |
|
---|
154 | msg = new List<MessageContainer>();
|
---|
155 | allMsgs.Add(msg);
|
---|
156 | msg = new List<MessageContainer>();
|
---|
157 | allMsgs.Add(msg);
|
---|
158 | msg = new List<MessageContainer>();
|
---|
159 | allMsgs.Add(msg);
|
---|
160 | msg = new List<MessageContainer>();
|
---|
161 | allMsgs.Add(msg);
|
---|
162 | msg = new List<MessageContainer>();
|
---|
163 | allMsgs.Add(msg);
|
---|
164 | msg = new List<MessageContainer>();
|
---|
165 | allMsgs.Add(msg);
|
---|
166 | msg = new List<MessageContainer>();
|
---|
167 | allMsgs.Add(msg);
|
---|
168 | msg = new List<MessageContainer>();
|
---|
169 | allMsgs.Add(msg);
|
---|
170 | msg = new List<MessageContainer>();
|
---|
171 | allMsgs.Add(msg);
|
---|
172 |
|
---|
173 | msg = new List<MessageContainer>();
|
---|
174 | msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
|
---|
175 | allMsgs.Add(msg);
|
---|
176 | return allMsgs;
|
---|
177 | }
|
---|
178 |
|
---|
179 | [TestMethod]
|
---|
180 | public void TestTwoJobs() {
|
---|
181 | Job testJob1 = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
182 | Job testJob2 = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
183 | List<Job> jobList = new List<Job>();
|
---|
184 | jobList.Add(testJob1);
|
---|
185 | jobList.Add(testJob2);
|
---|
186 | MockJob job = new MockJob(2000, false);
|
---|
187 |
|
---|
188 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
189 | MockHiveService ms = (MockHiveService)service.Obj;
|
---|
190 | ((MockHiveService)service.Obj).Messages = CreateMsgsForTwoJobs();
|
---|
191 | ((MockHiveService)service.Obj).updateJobs(jobList, job);
|
---|
192 |
|
---|
193 |
|
---|
194 | HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
|
---|
195 | core.Start();
|
---|
196 | Assert.AreEqual<int>(2, ms.ResultJobs.Count);
|
---|
197 |
|
---|
198 | }
|
---|
199 | }
|
---|
200 |
|
---|
201 |
|
---|
202 |
|
---|
203 |
|
---|
204 |
|
---|
205 |
|
---|
206 |
|
---|
207 |
|
---|
208 |
|
---|
209 | }
|
---|
210 | }
|
---|