1 /*******************************************************************************
2 
3     Copyright:
4         Copyright (c) 2017 dunnhumby Germany GmbH.
5         All rights reserved.
6 
7     License:
8         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
9         Alternatively, this file may be distributed under the terms of the Tango
10         3-Clause BSD License (see LICENSE_BSD.txt for details).
11 
12 *******************************************************************************/
13 
14 module ocean.task.util.TaskPoolSerializer_test;
15 
16 import ocean.task.util.TaskPoolSerializer;
17 
18 import ocean.core.Test;
19 import ocean.io.Stdout;
20 import ocean.io.device.MemoryDevice;
21 import ocean.task.Scheduler;
22 import ocean.task.Task;
23 import ocean.task.TaskPool;
24 import ocean.task.util.Timer;
25 import ocean.util.serialize.contiguous.Contiguous;
26 import ocean.util.serialize.contiguous.Deserializer;
27 import ocean.util.serialize.contiguous.Serializer;
28 import Util = ocean.util.serialize.contiguous.Util;
29 
30 unittest
31 {
32     static struct TaskArgs
33     {
34         int i;
35     }
36 
37     static class TestTask : Task
38     {
39         static int result;
40 
41         public Contiguous!(TaskArgs) args;
42 
43         override public void run ( )
44         {
45             wait(1);
46             result += args.ptr.i;
47         }
48 
49         public void copyArguments( TaskArgs data )
50         {
51             Util.copy(data, this.args);
52         }
53 
54         public void serialize ( ref void[] buffer )
55         {
56             Serializer.serialize(*this.args.ptr, buffer);
57         }
58 
59         public void deserialize ( void[] buffer )
60         {
61             Deserializer.deserialize(buffer, this.args);
62         }
63     }
64 
65     initScheduler(SchedulerConfiguration.init);
66     TaskPoolSerializer serializer = new TaskPoolSerializer();
67 
68     auto pool = new TaskPool!(TestTask);
69     pool.start(TaskArgs(1));
70     pool.start(TaskArgs(2));
71     pool.start(TaskArgs(3));
72     pool.start(TaskArgs(4));
73     pool.start(TaskArgs(5));
74 
75     auto storage = new MemoryDevice;
76     serializer.dump(pool, storage);
77 
78     //Run the event loop and reset the pool and result so we have a clean slate.
79     theScheduler.eventLoop();
80     TestTask.result = 0;
81     pool.clear();
82 
83     storage.seek(0);
84     size_t items = serializer.load(pool, storage);
85     theScheduler.eventLoop();
86 
87     test!("==")(items, 5, "Incorrect number of items loaded");
88     test!("==")(TestTask.result, 15, "Wrong result for restored tasks.");
89 }