Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Persistence Test/HeuristicLab.Hive.Server.Core/3.2/DbTestApp.cs @ 2710

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

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

File size: 10.4 KB
Line 
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;
29using HeuristicLab.Hive.Server.DataAccess;
30using HeuristicLab.Hive.Contracts.BusinessObjects;
31using System.Diagnostics;
32using HeuristicLab.DataAccess.Interfaces;
33using System.IO;
34using HeuristicLab.Hive.Server.Core;
35using HeuristicLab.Core;
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 {
42    /*  private void TestClientAdapter() {
43        IClientAdapter clientAdapter =
44          ServiceLocator.GetClientAdapter();
45
46        ClientInfo client = new ClientInfo();
47        client.Login = DateTime.Now;
48        clientAdapter.Update(client);
49
50        ClientInfo clientRead =
51          clientAdapter.GetById(client.Id);
52        Debug.Assert(
53          clientRead != null &&
54          client.Id == clientRead.Id);
55
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);
64
65        ICollection<ClientInfo> clients =
66          clientAdapter.GetAll();
67        int count = clients.Count;
68
69        clientAdapter.Delete(client);
70
71        clients = clientAdapter.GetAll();
72        Debug.Assert(count - 1 == clients.Count);
73      }
74
75      private void TestClientGroupAdapter() {
76        ISessionFactory factory =
77          ServiceLocator.GetSessionFactory();
78
79        ISession session =
80          factory.GetSessionForCurrentThread();
81
82        ITransaction trans = null;
83
84        try {
85          IClientGroupAdapter clientGroupAdapter =
86          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
87
88          trans =
89            session.BeginTransaction();
90
91          ClientInfo client =
92            new ClientInfo();
93          client.Name = "Stefan";
94          client.Login = DateTime.Now;
95
96          ClientInfo client2 =
97            new ClientInfo();
98          client2.Name = "Martin";
99          client2.Login = DateTime.Now;
100
101          ClientInfo client3 =
102            new ClientInfo();
103          client3.Name = "Heinz";
104          client3.Login = DateTime.Now;
105
106          ClientGroup group =
107            new ClientGroup();
108
109          ClientGroup subGroup =
110            new ClientGroup();
111          subGroup.Resources.Add(client);
112
113          group.Resources.Add(client3);
114          group.Resources.Add(client2);
115          group.Resources.Add(subGroup);
116
117          clientGroupAdapter.Update(group);
118
119          ClientGroup read =
120            clientGroupAdapter.GetById(group.Id);
121
122          ICollection<ClientGroup> clientGroups =
123            clientGroupAdapter.GetAll();
124
125          IClientAdapter clientAdapter =
126            session.GetDataAdapter<ClientInfo, IClientAdapter>();
127
128          clientAdapter.Delete(client3);
129
130          read =
131             clientGroupAdapter.GetById(group.Id);
132
133          clientGroupAdapter.Delete(subGroup);
134
135          read =
136             clientGroupAdapter.GetById(group.Id);
137
138          clientGroups =
139            clientGroupAdapter.GetAll();
140
141          clientGroupAdapter.Delete(group);
142
143          clientGroups =
144            clientGroupAdapter.GetAll();
145
146          clientAdapter.Delete(client);
147          clientAdapter.Delete(client2);
148        }
149        finally {
150          if (trans != null)
151            trans.Rollback();
152
153          session.EndSession();
154        }
155      }
156
157      private void InsertTestClientGroups() {
158        ISessionFactory factory =
159          ServiceLocator.GetSessionFactory();
160
161        ISession session =
162          factory.GetSessionForCurrentThread();
163
164        ITransaction trans = null;
165
166        try {
167          IClientGroupAdapter clientGroupAdapter =
168          session.GetDataAdapter<ClientGroup, IClientGroupAdapter>();
169
170          trans =
171            session.BeginTransaction();
172
173          ClientInfo client =
174            new ClientInfo();
175          client.Name = "Stefan";
176          client.Login = DateTime.Now;
177
178          ClientInfo client2 =
179            new ClientInfo();
180          client2.Name = "Martin";
181          client2.Login = DateTime.Now;
182
183          ClientGroup group =
184            new ClientGroup();
185          group.Name = "Gruppe1";
186
187          ClientGroup subGroup =
188            new ClientGroup();
189          subGroup.Name = "Untergruppe1";
190          subGroup.Resources.Add(client);
191
192          group.Resources.Add(client2);
193          group.Resources.Add(subGroup);
194
195          clientGroupAdapter.Update(group);
196
197          trans.Commit();
198        }
199        finally {
200          session.EndSession();
201        }
202      }
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();
224      }
225
226      private void TestJobResultsAdapter() {
227        Job job = new Job();
228
229        ClientInfo client = new ClientInfo();
230        client.Login = DateTime.Now;
231
232        job.Client = client;
233
234        IJobResultsAdapter resultsAdapter =
235          ServiceLocator.GetJobResultsAdapter();
236
237        byte[] resultByte = {0x0f, 0x1f, 0x2f, 0x3f, 0x4f};
238
239        JobResult result = new JobResult();
240        result.Client = client;
241        result.Job = job;
242        result.Result = resultByte;
243
244        resultsAdapter.Update(result);
245
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);
253
254        int count =
255          resultsAdapter.GetAll().Count;
256
257        resultsAdapter.Delete(result);
258
259        ICollection<JobResult> allResults =
260          resultsAdapter.GetAll();
261
262        Debug.Assert(allResults.Count == count - 1);
263
264        IJobAdapter jboAdapter =
265          ServiceLocator.GetJobAdapter();
266        jboAdapter.Delete(job);
267        IClientAdapter clientAdapter =
268          ServiceLocator.GetClientAdapter();
269        clientAdapter.Delete(client);
270      }     
271
272      private void TestTransaction() {
273        ISessionFactory factory =
274          ServiceLocator.GetSessionFactory();
275
276        ISession session =
277          factory.GetSessionForCurrentThread();
278
279        IClientAdapter clientAdapter =
280          session.GetDataAdapter<ClientInfo, IClientAdapter>();
281
282        ITransaction trans =
283          session.BeginTransaction();
284
285        ClientInfo client = new ClientInfo();
286        client.Login = DateTime.Now;
287        clientAdapter.Update(client);
288
289        trans.Rollback();
290
291        session.EndSession();
292      }  */
293
294    private void TestJobStreaming() {
295      ISessionFactory factory =
296         ServiceLocator.GetSessionFactory();
297
298      ISession session =
299           factory.GetSessionForCurrentThread();
300
301      IJobAdapter jobAdapter =
302        session.GetDataAdapter<Job, IJobAdapter>();
303
304      Stream s = jobAdapter.GetSerializedJobStream(
305        new Guid("1b35f32b-d880-4c76-86af-4b4e283b30e6"), true);
306
307      int length = 0;
308
309      FileStream fs =
310        new FileStream(@"W:\\result.gz", FileMode.Create);
311
312      byte[] buffer = new byte[1024];
313      while((length = s.Read(buffer, 0, buffer.Length)) > 0)
314      {
315        fs.Write(buffer, 0, length);
316      }
317
318      fs.Close();
319      s.Close();
320
321      session.EndSession();
322    }
323
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
365    public override void Run() {
366      //TestClientGroupAdapter();
367      //InsertTestClientGroups();
368      //TestJobStreaming();
369      //TestJobResultStreaming();
370      TestJobResultDeserialization();
371    }     
372  }
373}
Note: See TracBrowser for help on using the repository browser.