Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/SlaveTest.cs @ 6368

Last change on this file since 6368 was 6357, checked in by cneumuel, 13 years ago

#1233

  • refactoring of slave core
  • created JobManager, which is responsible for managing jobs without knowing anything about the service. this class is easier testable than slave core
  • lots of cleanup
  • created console test project for slave
File size: 6.8 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Clients.Hive.SlaveCore.ConsoleClient;
26using Microsoft.VisualStudio.TestTools.UnitTesting;
27
28namespace HeuristicLab.Clients.Hive.SlaveCore.Tests {
29  [TestClass]
30  public class SlaveTest {
31    private static SlaveCommListener listener;
32
33    // Use ClassInitialize to run code before running the first test in the class
34    [ClassInitialize]
35    public static void MyClassInitialize(TestContext testContext) {
36      ServiceLocator.Instance = new MockServiceLocator();
37      /* listener = new SlaveCommListener();
38       listener.Open();*/
39    }
40
41    [ClassCleanup]
42    public static void MyClassCleanup() {
43      //listener.Close();
44    }
45
46
47    private List<List<MessageContainer>> CreateMsgsForSingleJob() {
48      List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
49      //get slave to fetch job
50      List<MessageContainer> msg = new List<MessageContainer>();
51      msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob));
52      allMsgs.Add(msg);
53
54      //do nothing
55      msg = new List<MessageContainer>();
56      allMsgs.Add(msg);
57      msg = new List<MessageContainer>();
58      allMsgs.Add(msg);
59      msg = new List<MessageContainer>();
60      allMsgs.Add(msg);
61      msg = new List<MessageContainer>();
62      allMsgs.Add(msg);
63      msg = new List<MessageContainer>();
64      allMsgs.Add(msg);
65
66      msg = new List<MessageContainer>();
67      msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
68      allMsgs.Add(msg);
69      return allMsgs;
70    }
71
72    [TestMethod]
73    public void TestSingleJob() {
74      Job testJob = new Job { Id = Guid.NewGuid(), CoresNeeded = 1, MemoryNeeded = 0 };
75      testJob.State = JobState.Waiting;
76      List<Job> jobList = new List<Job>();
77      jobList.Add(testJob);
78      MockJob job = new MockJob(300, false);
79
80      ServiceLocator.Instance.CallHiveService(service => {
81        MockHiveService ms = (MockHiveService)service;
82        ((MockHiveService)service).Messages = CreateMsgsForSingleJob();
83        ((MockHiveService)service).UpdateJobs(jobList, job);
84
85        HeuristicLab.Clients.Hive.SlaveCore.Core core = new SlaveCore.Core();
86        core.Start();
87
88        Assert.AreEqual<int>(1, ms.ResultJobs.Count);
89        Assert.AreEqual<Guid>(testJob.Id, ms.ResultJobs[0].Id);
90        core.Shutdown();
91      });
92    }
93
94    private List<List<MessageContainer>> CreateMsgsForShutdownSlaveWhileJobRunning() {
95      List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
96      //get slave to fetch job
97      List<MessageContainer> msg = new List<MessageContainer>();
98      msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob));
99      allMsgs.Add(msg);
100
101      msg = new List<MessageContainer>();
102      allMsgs.Add(msg);
103
104      msg = new List<MessageContainer>();
105      msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
106      allMsgs.Add(msg);
107      return allMsgs;
108    }
109
110    [TestMethod]
111    public void TestShutdownSlaveWhileJobRunning() {
112      Job testJob = new Job { Id = Guid.NewGuid(), CoresNeeded = 1, MemoryNeeded = 0 };
113      testJob.State = JobState.Waiting;
114      List<Job> jobList = new List<Job>();
115      jobList.Add(testJob);
116      MockJob job = new MockJob(10000, false);
117
118      ServiceLocator.Instance.CallHiveService(service => {
119        MockHiveService ms = (MockHiveService)service;
120        ((MockHiveService)service).Messages = CreateMsgsForShutdownSlaveWhileJobRunning();
121        ((MockHiveService)service).UpdateJobs(jobList, job);
122
123
124        HeuristicLab.Clients.Hive.SlaveCore.Core core = new SlaveCore.Core();
125        core.Start();
126      });
127    }
128
129
130    private List<List<MessageContainer>> CreateMsgsForTwoJobs() {
131      List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
132
133      //get slave to fetch jobs
134      List<MessageContainer> msg = new List<MessageContainer>();
135      msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob));
136      allMsgs.Add(msg);
137
138      msg = new List<MessageContainer>();
139      allMsgs.Add(msg);
140
141      msg = new List<MessageContainer>();
142      msg.Add(new MessageContainer(MessageContainer.MessageType.CalculateJob));
143      allMsgs.Add(msg);
144
145
146      msg = new List<MessageContainer>();
147      allMsgs.Add(msg);
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
165      msg = new List<MessageContainer>();
166      msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
167      allMsgs.Add(msg);
168      return allMsgs;
169    }
170
171    [TestMethod]
172    public void TestTwoJobs() {
173      Job testJob1 = new Job { Id = Guid.NewGuid(), CoresNeeded = 1, MemoryNeeded = 0 };
174      testJob1.State = JobState.Waiting;
175      Job testJob2 = new Job { Id = Guid.NewGuid(), CoresNeeded = 1, MemoryNeeded = 0 };
176      testJob2.State = JobState.Waiting;
177      List<Job> jobList = new List<Job>();
178      jobList.Add(testJob1);
179      jobList.Add(testJob2);
180      MockJob job = new MockJob(2000, false);
181
182      ServiceLocator.Instance.CallHiveService(service => {
183        MockHiveService ms = (MockHiveService)service;
184        ((MockHiveService)service).Messages = CreateMsgsForTwoJobs();
185        ((MockHiveService)service).UpdateJobs(jobList, job);
186
187
188        HeuristicLab.Clients.Hive.SlaveCore.Core core = new SlaveCore.Core();
189        core.Start();
190        Assert.AreEqual<int>(2, ms.ResultJobs.Count);
191        core.Shutdown();
192      });
193    }
194  }
195}
Note: See TracBrowser for help on using the repository browser.