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