Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Hive.Server.Core/3.2/DbTestApp.cs @ 2269

Last change on this file since 2269 was 2122, checked in by svonolfe, 15 years ago

Fixed issue related to the streaming of results (#680)

File size: 10.4 KB
RevLine 
[826]1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 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.Text;
25using HeuristicLab.PluginInfrastructure;
26using System.Net;
27using HeuristicLab.Hive.Contracts;
28using HeuristicLab.Hive.Contracts.Interfaces;
[1377]29using HeuristicLab.Hive.Server.DataAccess;
[826]30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using System.Diagnostics;
[1377]32using HeuristicLab.DataAccess.Interfaces;
[2117]33using System.IO;
[2122]34using HeuristicLab.Hive.Server.Core;
35using HeuristicLab.Core;
[826]36
37namespace HeuristicLab.Hive.Server {
38  [ClassInfo(Name = "Hive DB Test App",
39      Description = "Test Application for the Hive DataAccess Layer",
40      AutoRestart = true)]
41  class HiveDbTestApplication : ApplicationBase {
[2117]42    /*  private void TestClientAdapter() {
43        IClientAdapter clientAdapter =
44          ServiceLocator.GetClientAdapter();
[826]45
[2117]46        ClientInfo client = new ClientInfo();
47        client.Login = DateTime.Now;
48        clientAdapter.Update(client);
[826]49
[2117]50        ClientInfo clientRead =
51          clientAdapter.GetById(client.Id);
52        Debug.Assert(
53          clientRead != null &&
54          client.Id == clientRead.Id);
[826]55
[2117]56        client.CpuSpeedPerCore = 2000;
57        clientAdapter.Update(client);
58        clientRead =
59          clientAdapter.GetById(client.Id);
60        Debug.Assert(
61         clientRead != null &&
62         client.Id == clientRead.Id &&
63         clientRead.CpuSpeedPerCore == 2000);
[826]64
[2117]65        ICollection<ClientInfo> clients =
66          clientAdapter.GetAll();
67        int count = clients.Count;
[826]68
[2117]69        clientAdapter.Delete(client);
[826]70
[2117]71        clients = clientAdapter.GetAll();
72        Debug.Assert(count - 1 == clients.Count);
73      }
[905]74
[2117]75      private void TestClientGroupAdapter() {
76        ISessionFactory factory =
77          ServiceLocator.GetSessionFactory();
[965]78
[2117]79        ISession session =
80          factory.GetSessionForCurrentThread();
[965]81
[2117]82        ITransaction trans = null;
[965]83
[2117]84        try {
85          IClientGroupAdapter clientGroupAdapter =
86          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
[965]87
[2117]88          trans =
89            session.BeginTransaction();
[965]90
[2117]91          ClientInfo client =
92            new ClientInfo();
93          client.Name = "Stefan";
94          client.Login = DateTime.Now;
[965]95
[2117]96          ClientInfo client2 =
97            new ClientInfo();
98          client2.Name = "Martin";
99          client2.Login = DateTime.Now;
[965]100
[2117]101          ClientInfo client3 =
102            new ClientInfo();
103          client3.Name = "Heinz";
104          client3.Login = DateTime.Now;
[965]105
[2117]106          ClientGroup group =
107            new ClientGroup();
[965]108
[2117]109          ClientGroup subGroup =
110            new ClientGroup();
111          subGroup.Resources.Add(client);
[965]112
[2117]113          group.Resources.Add(client3);
114          group.Resources.Add(client2);
115          group.Resources.Add(subGroup);
[965]116
[2117]117          clientGroupAdapter.Update(group);
[965]118
[2117]119          ClientGroup read =
120            clientGroupAdapter.GetById(group.Id);
[965]121
[2117]122          ICollection<ClientGroup> clientGroups =
123            clientGroupAdapter.GetAll();
[965]124
[2117]125          IClientAdapter clientAdapter =
126            session.GetDataAdapter<ClientInfo, IClientAdapter>();
[965]127
[2117]128          clientAdapter.Delete(client3);
[965]129
[2117]130          read =
131             clientGroupAdapter.GetById(group.Id);
[965]132
[2117]133          clientGroupAdapter.Delete(subGroup);
[965]134
[2117]135          read =
136             clientGroupAdapter.GetById(group.Id);
[1515]137
[2117]138          clientGroups =
139            clientGroupAdapter.GetAll();
[1515]140
[2117]141          clientGroupAdapter.Delete(group);
[1515]142
[2117]143          clientGroups =
144            clientGroupAdapter.GetAll();
[1515]145
[2117]146          clientAdapter.Delete(client);
147          clientAdapter.Delete(client2);
148        }
149        finally {
150          if (trans != null)
151            trans.Rollback();
[1515]152
[2117]153          session.EndSession();
154        }
[1515]155      }
[965]156
[2117]157      private void InsertTestClientGroups() {
158        ISessionFactory factory =
159          ServiceLocator.GetSessionFactory();
[1656]160
[2117]161        ISession session =
162          factory.GetSessionForCurrentThread();
[1656]163
[2117]164        ITransaction trans = null;
[1656]165
[2117]166        try {
167          IClientGroupAdapter clientGroupAdapter =
168          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
[1656]169
[2117]170          trans =
171            session.BeginTransaction();
[1656]172
[2117]173          ClientInfo client =
174            new ClientInfo();
175          client.Name = "Stefan";
176          client.Login = DateTime.Now;
[1656]177
[2117]178          ClientInfo client2 =
179            new ClientInfo();
180          client2.Name = "Martin";
181          client2.Login = DateTime.Now;
[1656]182
[2117]183          ClientGroup group =
184            new ClientGroup();
185          group.Name = "Gruppe1";
[1656]186
[2117]187          ClientGroup subGroup =
188            new ClientGroup();
189          subGroup.Name = "Untergruppe1";
190          subGroup.Resources.Add(client);
[1656]191
[2117]192          group.Resources.Add(client2);
193          group.Resources.Add(subGroup);
[1656]194
[2117]195          clientGroupAdapter.Update(group);
[1656]196
[2117]197          trans.Commit();
198        }
199        finally {
200          session.EndSession();
201        }
[1656]202      }
[2117]203
204      private void TestJobAdapter() {
205        IJobAdapter jobAdapter =
206          ServiceLocator.GetJobAdapter();
207        IClientAdapter clientAdapter =
208          ServiceLocator.GetClientAdapter();
209
210        Job job = new Job();
211
212        ClientInfo client = new ClientInfo();
213        client.Login = DateTime.Now;
214
215        job.Client = client;
216        jobAdapter.Update(job);
217
218        ICollection<Job> jobs = jobAdapter.GetAll();
219
220        jobAdapter.Delete(job);
221        clientAdapter.Delete(client);
222
223        jobs = jobAdapter.GetAll();
[1656]224      }
225
[2117]226      private void TestJobResultsAdapter() {
227        Job job = new Job();
[971]228
[2117]229        ClientInfo client = new ClientInfo();
230        client.Login = DateTime.Now;
[971]231
[2117]232        job.Client = client;
[971]233
[2117]234        IJobResultsAdapter resultsAdapter =
235          ServiceLocator.GetJobResultsAdapter();
[971]236
[2117]237        byte[] resultByte = {0x0f, 0x1f, 0x2f, 0x3f, 0x4f};
[971]238
[2117]239        JobResult result = new JobResult();
240        result.Client = client;
241        result.Job = job;
242        result.Result = resultByte;
[971]243
[2117]244        resultsAdapter.Update(result);
[971]245
[2117]246        JobResult read =
247          resultsAdapter.GetById(result.Id);
248        Debug.Assert(
249          read.Id == result.Id &&
250          result.Client.Id == read.Client.Id &&
251          result.Job.Id == read.Job.Id &&
252          result.Result == result.Result);
[1005]253
[2117]254        int count =
255          resultsAdapter.GetAll().Count;
[1005]256
[2117]257        resultsAdapter.Delete(result);
[1005]258
[2117]259        ICollection<JobResult> allResults =
260          resultsAdapter.GetAll();
[1005]261
[2117]262        Debug.Assert(allResults.Count == count - 1);
[1005]263
[2117]264        IJobAdapter jboAdapter =
265          ServiceLocator.GetJobAdapter();
266        jboAdapter.Delete(job);
267        IClientAdapter clientAdapter =
268          ServiceLocator.GetClientAdapter();
269        clientAdapter.Delete(client);
270      }     
[1005]271
[2117]272      private void TestTransaction() {
273        ISessionFactory factory =
274          ServiceLocator.GetSessionFactory();
[1005]275
[2117]276        ISession session =
277          factory.GetSessionForCurrentThread();
[1005]278
[2117]279        IClientAdapter clientAdapter =
280          session.GetDataAdapter<ClientInfo, IClientAdapter>();
[1005]281
[2117]282        ITransaction trans =
283          session.BeginTransaction();
[1005]284
[2117]285        ClientInfo client = new ClientInfo();
286        client.Login = DateTime.Now;
287        clientAdapter.Update(client);
[1005]288
[2117]289        trans.Rollback();
[1025]290
[2117]291        session.EndSession();
292      }  */
[1005]293
[2117]294    private void TestJobStreaming() {
[1468]295      ISessionFactory factory =
[2117]296         ServiceLocator.GetSessionFactory();
[995]297
[1515]298      ISession session =
[2117]299           factory.GetSessionForCurrentThread();
[995]300
[2117]301      IJobAdapter jobAdapter =
302        session.GetDataAdapter<Job, IJobAdapter>();
[1005]303
[2117]304      Stream s = jobAdapter.GetSerializedJobStream(
[2122]305        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
[1468]306
[2122]307      int length = 0;
308
309      FileStream fs =
310        new FileStream(@"W:\\result.gz", FileMode.Create);
311
[2117]312      byte[] buffer = new byte[1024];
[2122]313      while((length = s.Read(buffer, 0, buffer.Length)) > 0)
[2117]314      {
[2122]315        fs.Write(buffer, 0, length);
[2117]316      }
[1468]317
[2122]318      fs.Close();
[2117]319      s.Close();
[1468]320
321      session.EndSession();
[1515]322    }
323
[2122]324    private void TestJobResultStreaming() {
325      ISessionFactory factory =
326         ServiceLocator.GetSessionFactory();
327
328      ISession session =
329           factory.GetSessionForCurrentThread();
330
331      IJobResultsAdapter jobResultsAdapter =
332        session.GetDataAdapter<JobResult, IJobResultsAdapter>();
333
334      Stream s = jobResultsAdapter.GetSerializedJobResultStream(
335        new Guid("c20b11a9-cde1-4d7f-8499-23dedb5a65ed"), true);
336
337      int length = 0;
338
339      FileStream fs =
340        new FileStream(@"W:\\result.gz", FileMode.Create);
341
342      byte[] buffer = new byte[1024];
343      while ((length = s.Read(buffer, 0, buffer.Length)) > 0) {
344        fs.Write(buffer, 0, length);
345      }
346
347      fs.Close();
348      s.Close();
349
350      session.EndSession();
351    }
352
353    private void TestJobResultDeserialization() {
354      ExecutionEngineFacade executionEngineFacade =
355        new ExecutionEngineFacade();
356
357     ResponseObject<SerializedJobResult> response =
358       executionEngineFacade.GetLastSerializedResult(
359       new Guid("56ce20bc-067b-424d-a7df-67aaace7c850"), false);
360
361       IStorable restoredJob =
362         PersistenceManager.RestoreFromGZip(response.Obj.SerializedJobResultData);
363    }
364
[1515]365    public override void Run() {
[2117]366      //TestClientGroupAdapter();
[1656]367      //InsertTestClientGroups();
[2122]368      //TestJobStreaming();
369      //TestJobResultStreaming();
370      TestJobResultDeserialization();
[1468]371    }     
[826]372  }
373}
Note: See TracBrowser for help on using the repository browser.