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.TaskPool_test;
15 
16 import ocean.task.TaskPool;
17 import ocean.task.Task;
18 import ocean.task.Scheduler;
19 import ocean.core.Test;
20 import ocean.task.util.Timer;
21 
22 unittest
23 {
24     // Test that the recycle method of custom tasks is called
25     static class RecycleTask : Task
26     {
27         static size_t recycle_count;
28 
29         public void copyArguments ( )
30         {
31 
32         }
33 
34         override public void recycle ( )
35         {
36             recycle_count++;
37         }
38 
39         override public void run ( )
40         {
41 
42         }
43     }
44 
45     auto pool = new TaskPool!(RecycleTask);
46     initScheduler(SchedulerConfiguration.init);
47 
48     pool.start();
49     pool.start();
50 
51     theScheduler.eventLoop();
52     test!("==")(RecycleTask.recycle_count, 2,
53         "RecycleTask.recycle was not called the correct number of times");
54 }
55 
56 unittest
57 {
58     // Test for waiting until all running tasks of a task pool finish executing.
59 
60     static class AwaitTask : Task
61     {
62         static int value;
63 
64         public void copyArguments ( )
65         {
66         }
67 
68         override public void run ( )
69         {
70             .wait(1); // so that the task gets suspended
71             value++;
72         }
73     }
74 
75     class MainTask : Task
76     {
77         TaskPool!(AwaitTask) my_task_pool;
78 
79         public this ( )
80         {
81             this.my_task_pool = new TaskPool!(AwaitTask);
82         }
83 
84         override protected void run ( )
85         {
86             static immutable NUM_START_CALLS = 6;
87 
88             for (uint i; i < NUM_START_CALLS; i++)
89                 this.my_task_pool.start();
90 
91             this.my_task_pool.awaitRunningTasks();
92 
93             test!("==")(AwaitTask.value, NUM_START_CALLS);
94         }
95     }
96 
97     initScheduler(SchedulerConfiguration.init);
98     theScheduler.schedule(new MainTask);
99     theScheduler.eventLoop();
100 }
101 
102 unittest
103 {
104     // Test that 'awaitRunningTasks()' cannot be called from a task that itself
105     // belongs to the pool.
106 
107     static class AwaitTask : Task
108     {
109         void delegate () dg;
110 
111         public void copyArguments ( scope void delegate () dg )
112         {
113             this.dg = dg;
114         }
115 
116         override public void run ( )
117         {
118             testThrown!(Exception)(this.dg());
119         }
120     }
121 
122     auto pool = new TaskPool!(AwaitTask);
123     initScheduler(SchedulerConfiguration.init);
124 
125     pool.start(
126         {
127             // This delegate should throw as it will attempt to call the task
128             // pool's awaitRunningTasks() method from within a task that itself
129             // belongs to the pool.
130             pool.awaitRunningTasks();
131         }
132     );
133 }