[5055] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
[5137] | 4 | using HeuristicLab.Clients.Common;
|
---|
[5104] | 5 | using HeuristicLab.Services.Hive.Common;
|
---|
[5137] | 6 | using HeuristicLab.Services.Hive.Common.DataTransfer;
|
---|
[5104] | 7 | using HeuristicLab.Services.Hive.Common.ServiceContracts;
|
---|
[5137] | 8 | using Microsoft.VisualStudio.TestTools.UnitTesting;
|
---|
[5055] | 9 |
|
---|
| 10 | namespace HeuristicLab.Clients.Hive.Slave.Tests {
|
---|
| 11 | [TestClass]
|
---|
| 12 | public class SlaveTest {
|
---|
| 13 |
|
---|
| 14 | // Use ClassInitialize to run code before running the first test in the class
|
---|
| 15 | [ClassInitialize]
|
---|
| 16 | public static void MyClassInitialize(TestContext testContext) {
|
---|
[5137] | 17 | PluginLoader.pluginAssemblies.Any();
|
---|
[5062] | 18 | ServiceLocator.Instance = new MockServiceLocator();
|
---|
[5055] | 19 | }
|
---|
| 20 |
|
---|
[5137] | 21 | private List<List<MessageContainer>> CreateMsgsForSingleJob() {
|
---|
[5104] | 22 | List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
|
---|
| 23 | //get slave to fetch job
|
---|
| 24 | List<MessageContainer> msg = new List<MessageContainer>();
|
---|
| 25 | msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
|
---|
| 26 | allMsgs.Add(msg);
|
---|
| 27 |
|
---|
| 28 | //do nothing
|
---|
| 29 | msg = new List<MessageContainer>();
|
---|
| 30 | allMsgs.Add(msg);
|
---|
[5137] | 31 | msg = new List<MessageContainer>();
|
---|
| 32 | allMsgs.Add(msg);
|
---|
| 33 | msg = new List<MessageContainer>();
|
---|
| 34 | allMsgs.Add(msg);
|
---|
| 35 | msg = new List<MessageContainer>();
|
---|
| 36 | allMsgs.Add(msg);
|
---|
[5104] | 37 |
|
---|
| 38 | msg = new List<MessageContainer>();
|
---|
| 39 | msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
|
---|
| 40 | allMsgs.Add(msg);
|
---|
| 41 | return allMsgs;
|
---|
| 42 | }
|
---|
| 43 |
|
---|
[5055] | 44 | [TestMethod]
|
---|
[5137] | 45 | public void TestSingleJob() {
|
---|
| 46 | Job testJob = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
| 47 | List<Job> jobList = new List<Job>();
|
---|
| 48 | jobList.Add(testJob);
|
---|
| 49 | MockJob job = new MockJob(400, false);
|
---|
| 50 |
|
---|
| 51 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 52 | MockHiveService ms = (MockHiveService)service.Obj;
|
---|
| 53 | ((MockHiveService)service.Obj).Messages = CreateMsgsForSingleJob();
|
---|
| 54 | ((MockHiveService)service.Obj).updateJobs(jobList, job);
|
---|
| 55 |
|
---|
| 56 |
|
---|
| 57 | HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
|
---|
| 58 | core.Start();
|
---|
| 59 |
|
---|
| 60 | Assert.AreEqual<int>(1, ms.ResultJobs.Count);
|
---|
| 61 | Assert.AreEqual<Guid>(testJob.Id, ms.ResultJobs[0].Id);
|
---|
[5104] | 62 | }
|
---|
[5055] | 63 | }
|
---|
[5137] | 64 |
|
---|
| 65 | private List<List<MessageContainer>> CreateMsgsForShutdownSlaveWhileJobRunning() {
|
---|
| 66 | List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
|
---|
| 67 | //get slave to fetch job
|
---|
| 68 | List<MessageContainer> msg = new List<MessageContainer>();
|
---|
| 69 | msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
|
---|
| 70 | allMsgs.Add(msg);
|
---|
| 71 |
|
---|
| 72 | msg = new List<MessageContainer>();
|
---|
| 73 | allMsgs.Add(msg);
|
---|
| 74 |
|
---|
| 75 | msg = new List<MessageContainer>();
|
---|
| 76 | msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
|
---|
| 77 | allMsgs.Add(msg);
|
---|
| 78 | return allMsgs;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | [TestMethod]
|
---|
| 82 | public void TestShutdownSlaveWhileJobRunning() {
|
---|
| 83 | Job testJob = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
| 84 | List<Job> jobList = new List<Job>();
|
---|
| 85 | jobList.Add(testJob);
|
---|
| 86 | MockJob job = new MockJob(10000, false);
|
---|
| 87 |
|
---|
| 88 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 89 | MockHiveService ms = (MockHiveService)service.Obj;
|
---|
| 90 | ((MockHiveService)service.Obj).Messages = CreateMsgsForShutdownSlaveWhileJobRunning();
|
---|
| 91 | ((MockHiveService)service.Obj).updateJobs(jobList, job);
|
---|
| 92 |
|
---|
| 93 |
|
---|
| 94 | HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
|
---|
| 95 | core.Start();
|
---|
| 96 |
|
---|
| 97 | }
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 |
|
---|
| 101 | private List<List<MessageContainer>> CreateMsgsForTwoJobs() {
|
---|
| 102 | List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
|
---|
| 103 |
|
---|
| 104 | //get slave to fetch jobs
|
---|
| 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.AquireJob));
|
---|
| 114 | allMsgs.Add(msg);
|
---|
| 115 |
|
---|
| 116 |
|
---|
| 117 | msg = new List<MessageContainer>();
|
---|
| 118 | allMsgs.Add(msg);
|
---|
| 119 | msg = new List<MessageContainer>();
|
---|
| 120 | allMsgs.Add(msg);
|
---|
| 121 | msg = new List<MessageContainer>();
|
---|
| 122 | allMsgs.Add(msg);
|
---|
| 123 | msg = new List<MessageContainer>();
|
---|
| 124 | allMsgs.Add(msg);
|
---|
| 125 | msg = new List<MessageContainer>();
|
---|
| 126 | allMsgs.Add(msg);
|
---|
| 127 | msg = new List<MessageContainer>();
|
---|
| 128 | allMsgs.Add(msg);
|
---|
| 129 | msg = new List<MessageContainer>();
|
---|
| 130 | allMsgs.Add(msg);
|
---|
| 131 | msg = new List<MessageContainer>();
|
---|
| 132 | allMsgs.Add(msg);
|
---|
| 133 | msg = new List<MessageContainer>();
|
---|
| 134 | allMsgs.Add(msg);
|
---|
| 135 |
|
---|
| 136 | msg = new List<MessageContainer>();
|
---|
| 137 | msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
|
---|
| 138 | allMsgs.Add(msg);
|
---|
| 139 | return allMsgs;
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | [TestMethod]
|
---|
| 143 | public void TestTwoJobs() {
|
---|
| 144 | Job testJob1 = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
| 145 | Job testJob2 = new Job { Id = Guid.NewGuid(), JobState = JobState.Waiting, DateCreated = DateTime.Now, CoresNeeded = 1, MemoryNeeded = 0 };
|
---|
| 146 | List<Job> jobList = new List<Job>();
|
---|
| 147 | jobList.Add(testJob1);
|
---|
| 148 | jobList.Add(testJob2);
|
---|
| 149 | MockJob job = new MockJob(2000, false);
|
---|
| 150 |
|
---|
| 151 | using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
|
---|
| 152 | MockHiveService ms = (MockHiveService)service.Obj;
|
---|
| 153 | ((MockHiveService)service.Obj).Messages = CreateMsgsForTwoJobs();
|
---|
| 154 | ((MockHiveService)service.Obj).updateJobs(jobList, job);
|
---|
| 155 |
|
---|
| 156 |
|
---|
| 157 | HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
|
---|
| 158 | core.Start();
|
---|
| 159 | Assert.AreEqual<int>(2, ms.ResultJobs.Count);
|
---|
| 160 |
|
---|
| 161 | }
|
---|
| 162 | }
|
---|
| 163 |
|
---|
| 164 |
|
---|
| 165 |
|
---|
| 166 |
|
---|
| 167 |
|
---|
| 168 |
|
---|
| 169 |
|
---|
| 170 |
|
---|
| 171 |
|
---|
[5055] | 172 | }
|
---|
| 173 | }
|
---|