LeechCraft  0.6.70-6645-gcd10d7e
Modular cross-platform feature rich live environment.
futures.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "futures.h"
31 #include <thread>
32 #include <chrono>
33 #include <QEventLoop>
34 #include <QtTest>
35 #include <futures.h>
36 
38 
39 namespace LeechCraft
40 {
41 namespace Util
42 {
43  namespace
44  {
45  auto MkWaiter ()
46  {
47  return [] (int msecs)
48  {
49  return QtConcurrent::run ([msecs]
50  {
51  std::this_thread::sleep_for (std::chrono::milliseconds (msecs));
52  return msecs * 2;
53  });
54  };
55  }
56  }
57 
58  void FuturesTest::testSequencer ()
59  {
60  QEventLoop loop;
61  int res = 0;
62  Sequence (nullptr, MkWaiter () (25))
63  .Then (MkWaiter ())
64  .Then (MkWaiter ())
65  .Then (MkWaiter ())
66  .Then ([&loop, &res] (int cnt)
67  {
68  res = cnt;
69  loop.quit ();
70  });
71 
72  loop.exec ();
73 
74  QCOMPARE (res, 400);
75  }
76 
77  void FuturesTest::testHeterogeneousTypes ()
78  {
79  struct Bar {};
80  struct Baz {};
81 
82  QEventLoop loop;
83  bool executed = false;
84  Sequence (nullptr, MkWaiter () (50)) >>
85  [] (int) { return MakeReadyFuture<Bar> ({}); } >>
86  [] (Bar) { return MakeReadyFuture<Baz> ({}); } >>
87  [&executed, &loop] (Baz)
88  {
89  executed = true;
90  loop.quit ();
91  };
92 
93  loop.exec ();
94 
95  QCOMPARE (executed, true);
96  }
97 
98  void FuturesTest::testDestruction ()
99  {
100  QEventLoop loop;
101  bool executed = false;
102 
103  {
104  QObject obj;
105  Sequence (&obj, MakeReadyFuture (0)) >>
106  [&executed, &loop] (int)
107  {
108  executed = true;
109  loop.quit ();
110  };
111  }
112 
113  QTimer::singleShot (100, &loop, SLOT (quit ()));
114 
115  loop.exec ();
116 
117  QCOMPARE (executed, false);
118  }
119 
120  void FuturesTest::testDestructionHandler ()
121  {
122  const auto finished = 1;
123  const auto destructed = 2;
124 
125  QEventLoop loop;
126  bool executed = false;
127  int value = 0;
128 
129  QFuture<int> future;
130  {
131  QObject obj;
132  future = Sequence (&obj, MkWaiter () (100))
133  .DestructionValue ([destructed] { return destructed; }) >>
134  [=] (int) { return MakeReadyFuture (finished); };
135  }
136  Sequence (nullptr, future) >>
137  [&executed, &value, &loop] (int val)
138  {
139  value = val;
140  executed = true;
141  loop.quit ();
142  };
143 
144  QTimer::singleShot (10, &loop, SLOT (quit ()));
145 
146  loop.exec ();
147 
148  QCOMPARE (executed, true);
149  QCOMPARE (value, destructed);
150  }
151 
152  void FuturesTest::testNoDestrHandler ()
153  {
154  struct Bar {};
155  struct Baz {};
156 
157  QEventLoop loop;
158  bool executed = false;
159  Sequence (nullptr, MkWaiter () (50))
160  .DestructionValue ([&executed] { executed = true; }) >>
161  [] (int) { return MakeReadyFuture<Bar> ({}); } >>
162  [] (Bar) { return MakeReadyFuture<Baz> ({}); } >>
163  [&loop] (Baz) { loop.quit (); };
164 
165  loop.exec ();
166 
167  QCOMPARE (executed, false);
168  }
169 
170  void FuturesTest::testNoDestrHandlerSetBuildable ()
171  {
172  const auto finished = 1;
173 
174  QEventLoop loop;
175  bool executed = false;
176  int value = 0;
177 
178  QFuture<int> future = Sequence (nullptr, MkWaiter () (10)) >>
179  [=] (int) { return MakeReadyFuture (finished); };
180  Sequence (nullptr, future) >>
181  [&executed, &value, &loop] (int val)
182  {
183  value = val;
184  executed = true;
185  loop.quit ();
186  };
187 
188  loop.exec ();
189 
190  QCOMPARE (executed, true);
191  QCOMPARE (value, finished);
192  }
193 }
194 }
QFuture< T > MakeReadyFuture(const T &t)
Creates a ready future holding the given value.
Definition: futures.h:674
detail::SequenceProxy< detail::SequencerRetType_t< QFuture< T > >, QFuture< T >, detail::EmptyDestructionTag > Sequence(QObject *parent, const QFuture< T > &future)
Creates a sequencer that allows chaining multiple futures.
Definition: futures.h:657