Free cookie consent management tool by TermsFeed Policy Generator

Changeset 5104


Ignore:
Timestamp:
12/14/10 16:07:42 (13 years ago)
Author:
ascheibe
Message:

added some more functionality to the MockService #1233

Location:
branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockHiveService.cs

    r5062 r5104  
    66using HeuristicLab.Services.Hive.Common.DataTransfer;
    77using HeuristicLab.Services.Hive.Common;
     8using HeuristicLab.PluginInfrastructure.Manager;
     9using System.IO;
     10using System.Reflection;
     11using HeuristicLab.PluginInfrastructure;
    812
    913namespace HeuristicLab.Clients.Hive.Slave.Tests {
     
    1115    private static List<Job> jobs;
    1216    private static List<JobData> jobDatas;
    13     private static List<HiveExperiment> hiveExperiments;
     17    private static List<HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment> hiveExperiments;   
     18    private static List<Plugin> plugins = null;
     19    private static IEnumerable<PluginDescription> pDescs = null;
     20    private static int cnt = 0;
     21
     22    public List<List<MessageContainer>> Messages { get; set; }
     23    public static string ServerPluginCachePath { get; set; }
     24    public static string ServerConfigFilePath { get; set; }
    1425
    1526    static MockHiveService() {
    16      
    17       byte[] data = PersistenceUtil.Serialize(new MockJob(5000));
     27      ServerPluginCachePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCacheServer");
     28      ServerConfigFilePath = Path.Combine(ServerPluginCachePath, "HeuristicLab 3.3.exe.config");
     29
     30      byte[] data = PersistenceUtil.Serialize(new MockJob(4000));
     31      if (plugins == null)
     32        plugins = ReadPluginsFromServerCache();
    1833
    1934      jobs = new List<Job>();
     
    2540      jobDatas = new List<JobData>();
    2641      foreach (var job in jobs) {
    27         jobDatas.Add(new JobData() { JobId = job.Id, Data = data });
    28       }
    29 
    30       hiveExperiments = new List<HiveExperiment>();
    31       hiveExperiments.Add(new HiveExperiment() { Id = Guid.NewGuid(), Name = "Hive Exp 1", Description = "", RootJobId = jobs[0].Id });
     42        job.PluginsNeededIds = new List<Guid>();
     43        job.PluginsNeededIds.AddRange(plugins.Select(a => a.Id));
     44        jobDatas.Add(new JobData() { JobId = job.Id, Data = data });       
     45      }
     46     
     47      hiveExperiments = new List<HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment>();
     48      hiveExperiments.Add(new HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment() { Id = Guid.NewGuid(), Name = "Hive Exp 1", Description = "", RootJobId = jobs[0].Id });
     49    }
     50
     51    private static List<Plugin> ReadPluginsFromServerCache() {
     52      PluginManager pm = new PluginManager(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "PluginCacheServer"));
     53      pm.DiscoverAndCheckPlugins();
     54      pDescs = pm.Plugins;
     55      plugins = new List<Plugin>();
     56      foreach (PluginDescription d in pDescs) {
     57        Plugin p = new Plugin();
     58        p.Name = d.Name;
     59        p.Version = d.Version;
     60        p.Id = Guid.NewGuid();
     61        plugins.Add(p);
     62      }
     63      return plugins;
    3264    }
    3365
     
    5082    }
    5183
     84    public PluginData GetConfigurationFile() {
     85      byte[] cf = File.ReadAllBytes(ServerConfigFilePath);
     86      PluginData pd = new PluginData();
     87      pd.Data = cf;
     88      return pd;
     89    }
     90
    5291    public Job GetJob(Guid jobId) {
    5392      return jobs.Where(j => j.Id == jobId).SingleOrDefault();
     
    71110
    72111    public void UpdateJob(Job jobDto, JobData jobDataDto) {
    73       // do nothing
     112      Console.WriteLine("Update Job called!");
    74113    }
    75114
     
    96135
    97136    #region HiveExperiment Methods
    98     public HiveExperiment GetHiveExperiment(Guid id) {
     137    public HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment GetHiveExperiment(Guid id) {
    99138      return hiveExperiments.Where(he => he.Id == id).SingleOrDefault();
    100139    }
    101140
    102     public IEnumerable<HiveExperiment> GetHiveExperiments() {
     141    public IEnumerable<HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment> GetHiveExperiments() {
    103142      return hiveExperiments;
    104143    }
    105144
    106     public Guid AddHiveExperiment(HiveExperiment hiveExperimentDto) {
     145    public Guid AddHiveExperiment(HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment hiveExperimentDto) {
    107146      hiveExperimentDto.Id = Guid.NewGuid();
    108147      hiveExperiments.Add(hiveExperimentDto);
     
    110149    }
    111150
    112     public void UpdateHiveExperiment(HiveExperiment hiveExperimentDto) {
     151    public void UpdateHiveExperiment(HeuristicLab.Services.Hive.Common.DataTransfer.HiveExperiment hiveExperimentDto) {
    113152      // do nothing
    114153    }
     
    126165    public void GoodBye() {
    127166      // do nothing
    128     }
    129 
     167    }   
     168   
    130169    public List<MessageContainer> Heartbeat(Heartbeat heartbeat) {
    131       // todo
    132       return new List<MessageContainer>();
    133     }
     170      if (Messages != null && cnt < Messages.Count) {
     171        return Messages[cnt++];
     172      } else {
     173        return new List<MessageContainer>();
     174      }
     175    }
     176     
    134177    #endregion
    135178
     
    141184
    142185    public IEnumerable<Plugin> GetPlugins() {
    143       // todo
    144       return new List<Plugin>();
     186      if (plugins == null)
     187        plugins = ReadPluginsFromServerCache();
     188      return plugins;             
    145189    }
    146190
    147191    public IEnumerable<PluginData> GetPluginDatas(List<Guid> pluginIds) {
    148       // todo
    149       return new List<PluginData>();
     192      List<PluginData> pluginDatas = new List<PluginData>();
     193
     194      foreach (Guid guid in pluginIds) {
     195        Plugin plugin = plugins.Find(p => p.Id == guid);
     196        if (plugin != null) {
     197          PluginDescription desc = pDescs.First(p => p.Name == plugin.Name && p.Version == plugin.Version);
     198          if (desc != null) {
     199            foreach (IPluginFile file in desc.Files) {
     200              PluginData data = new PluginData();
     201              data.PluginId = guid;
     202              data.Data = File.ReadAllBytes(file.Name);
     203              pluginDatas.Add(data);
     204            }
     205          }         
     206        }     
     207      }
     208      return pluginDatas;
    150209    }
    151210    #endregion
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockJob.cs

    r5062 r5104  
    9797        this.ExecutionTime = DateTime.Now - start;
    9898      } while (ExecutionTime.TotalMilliseconds < ms);
    99       this.ExecutionState = Core.ExecutionState.Stopped;
     99      Stop();
     100      OnJobStopped();
    100101    }
    101102
     
    148149      if (handler != null) handler(this, EventArgs.Empty);
    149150    }
     151                                                     
     152    public event EventHandler JobFailed;
     153    protected virtual void OnJobFailed() {
     154      EventHandler handler = JobFailed;
     155      if (handler != null) handler(this, EventArgs.Empty);
     156    }
     157
     158    public event EventHandler JobStopped;
     159    protected virtual void OnJobStopped() {
     160      EventHandler handler = JobStopped;
     161      if (handler != null) handler(this, EventArgs.Empty);
     162    }
     163
    150164    #endregion
    151165  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/Mocks/MockServiceLocator.cs

    r5062 r5104  
    99  internal class MockServiceLocator : IServiceLocator {
    1010
     11    private MockHiveService service;
     12
    1113    public Disposable<IHiveService> GetService() {
    12       return new Disposable<IHiveService>(new MockHiveService());
     14      if (service == null)
     15        service = new MockHiveService();
     16
     17      return new Disposable<IHiveService>(service);
    1318    }
    1419  }
  • branches/HeuristicLab.Hive-3.4/sources/HeuristicLab.Clients.Hive.Slave.Tests/SlaveTest.cs

    r5062 r5104  
    44using System.Linq;
    55using Microsoft.VisualStudio.TestTools.UnitTesting;
     6using HeuristicLab.Clients.Hive.Salve;
     7using HeuristicLab.Services.Hive.Common;
     8using HeuristicLab.Services.Hive.Common.ServiceContracts;
     9using HeuristicLab.Clients.Common;
    610
    711namespace HeuristicLab.Clients.Hive.Slave.Tests {
     
    1216    [ClassInitialize]
    1317    public static void MyClassInitialize(TestContext testContext) {
    14       PluginLoader.pluginAssemblies.Any();
     18      PluginLoader.pluginAssemblies.Any();     
    1519      ServiceLocator.Instance = new MockServiceLocator();
     20    }
     21
     22    public List<List<MessageContainer>> CreateMsgs() {
     23      List<List<MessageContainer>> allMsgs = new List<List<MessageContainer>>();
     24      //get slave to fetch job
     25      List<MessageContainer> msg = new List<MessageContainer>();
     26      msg.Add(new MessageContainer(MessageContainer.MessageType.AquireJob));
     27      allMsgs.Add(msg);
     28
     29      //do nothing
     30      msg = new List<MessageContainer>();
     31      allMsgs.Add(msg);
     32
     33      msg = new List<MessageContainer>();
     34      msg.Add(new MessageContainer(MessageContainer.MessageType.ShutdownSlave));
     35      allMsgs.Add(msg);
     36      return allMsgs;
    1637    }
    1738
    1839    [TestMethod]
    1940    public void TestMethod1() {
    20       SlaveDummy slave = new SlaveDummy();
     41      using (Disposable<IHiveService> service = ServiceLocator.Instance.GetService()) {
     42        ((MockHiveService)service.Obj).Messages = CreateMsgs();
     43      }
    2144     
    22       slave.SayHello();
     45      HeuristicLab.Clients.Hive.Salve.Core core = new Salve.Core();
     46      core.Start();
    2347    }
    2448  }
Note: See TracChangeset for help on using the changeset viewer.