1 /*******************************************************************************
2 
3     Test-suite for ocean.util.log.Stats.
4 
5     Copyright:
6         Copyright (c) 2009-2016 dunnhumby Germany GmbH.
7         All rights reserved.
8 
9     License:
10         Boost Software License Version 1.0. See LICENSE_BOOST.txt for details.
11         Alternatively, this file may be distributed under the terms of the Tango
12         3-Clause BSD License (see LICENSE_BSD.txt for details).
13 
14 *******************************************************************************/
15 
16 module ocean.util.log.Stats_slowtest;
17 
18 import ocean.core.Test,
19        ocean.util.log.Stats;
20 
21 import ocean.meta.types.Qualifiers;
22 import ocean.io.device.TempFile;
23 
24 unittest
25 {
26     class MyStatsLog : StatsLog
27     {
28         this ( Config config ) { super(config); }
29 
30         void test()
31         {
32             .test!("==")(this.buffer[], `x:10`[]);
33         }
34     }
35 
36     class Stats
37     {
38         int x;
39     }
40 
41     scope temp_file = new TempFile;
42 
43     auto logger = new MyStatsLog(new StatsLog.Config(temp_file.toString()));
44 
45     auto stats = new Stats();
46     stats.x = 10;
47 
48     logger.add(stats);
49     logger.test();
50 }
51 
52 unittest
53 {
54     scope temp_file = new TempFile;
55 
56     class TestLogger : StatsLog
57     {
58         this ()
59         {
60             super(new StatsLog.Config(temp_file.toString()));
61         }
62 
63         void test()
64         {
65             .test!("==")(
66                 this.buffer[],
67                 `workers_hired:420 workers_injured:0`
68                 ~ ` production_line/samsung/phone_built:10000`
69                 ~ ` production_line/samsung/laptop_built:200`
70                 ~ ` production_line/apple/phone_built:800`
71                 ~ ` production_line/apple/laptop_built:100`);
72         }
73     }
74 
75     auto logger = new TestLogger();
76 
77     // Stats about a factory
78     struct FactoryStats
79     {
80         ulong workers_hired;
81         ulong workers_injured;
82     }
83 
84     // Stats about a single production line
85     struct ProductionLineStats
86     {
87         ulong phone_built;
88         ulong laptop_built;
89     }
90 
91     FactoryStats stats;
92     stats.workers_hired = 420;
93 
94     static struct Entry
95     {
96         istring name;
97         ProductionLineStats stats;
98     }
99 
100     Entry[] entries = [
101         Entry("samsung", ProductionLineStats(10_000, 200)),
102         Entry("apple", ProductionLineStats(800, 100))
103     ];
104 
105     // log everything
106     logger.add(stats);
107 
108     foreach (entry; entries)
109         logger.addObject!("production_line")(entry.name, entry.stats);
110 
111     logger.test();
112 }