-
-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathfutures.cpp
151 lines (130 loc) · 4.32 KB
/
futures.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
//
// lager - library for functional interactive c++ programs
// Copyright (C) 2017 Juan Pedro Bolivar Puente
//
// This file is part of lager.
//
// lager is free software: you can redistribute it and/or modify
// it under the terms of the MIT License, as detailed in the LICENSE
// file located at the root of this source code distribution,
// or here: <https://github.com/arximboldi/lager/blob/master/LICENSE>
//
#include <catch2/catch.hpp>
#include <lager/event_loop/queue.hpp>
#include <lager/store.hpp>
#include "../example/counter/counter.hpp"
TEST_CASE("future then callback is called after reducer")
{
auto queue = lager::queue_event_loop{};
auto store =
lager::make_store<counter::action>(counter::model{},
lager::with_queue_event_loop{queue},
lager::with_futures);
auto called = 0;
store.dispatch(counter::increment_action{}).then([&] {
CHECK(store->value == 1);
++called;
});
CHECK(called == 0);
queue.step();
CHECK(called == 1);
}
TEST_CASE("future then callback is called after effects")
{
auto effect1 = [&](auto&& ctx) -> lager::future { return ctx.dispatch(1); };
auto effect2 = [&](auto&& ctx) -> lager::future { return ctx.dispatch(2); };
auto queue = lager::queue_event_loop{};
auto store = lager::make_store<int>(
0,
lager::with_queue_event_loop{queue},
lager::with_futures,
lager::with_reducer([&](int s, int a) -> lager::result<int, int> {
if (a == 0)
return {s,
lager::sequence(lager::effect<int>{effect1},
lager::effect<int>{effect2})};
else
return s + a;
}));
auto called = 0;
store.dispatch(0).then([&] {
CHECK(*store == 3);
++called;
});
CHECK(called == 0);
queue.step();
CHECK(called == 1);
}
TEST_CASE("future effect callback chaining")
{
auto effect1 = [&](auto&& ctx) -> lager::future { return ctx.dispatch(1); };
auto effect2 = [&](auto&& ctx) -> lager::future { return ctx.dispatch(2); };
auto queue = lager::queue_event_loop{};
auto store = lager::make_store<int>(
0,
lager::with_queue_event_loop{queue},
lager::with_futures,
lager::with_reducer([&](int s, int a) -> lager::result<int, int> {
if (a == 0)
return {s, effect1};
else if (a == 1)
return {s + a, effect2};
else
return s + a;
}));
auto called = 0;
store.dispatch(0).then([&] {
CHECK(*store == 3);
++called;
});
CHECK(called == 0);
queue.step();
CHECK(called == 1);
}
TEST_CASE("future effect callback chaining and stupid effect")
{
auto effect0 = [&](auto&& ctx) -> void {};
auto effect1 = [&](auto&& ctx) -> lager::future { return ctx.dispatch(1); };
auto effect2 = [&](auto&& ctx) -> lager::future { return ctx.dispatch(2); };
auto queue = lager::queue_event_loop{};
auto store = lager::make_store<int>(
0,
lager::with_queue_event_loop{queue},
lager::with_futures,
lager::with_reducer([&](int s, int a) -> lager::result<int, int> {
if (a == 0)
return {s,
lager::sequence(lager::effect<int>{effect1},
lager::effect<int>{effect0})};
else if (a == 1)
return {s + a, effect2};
else
return s + a;
}));
auto called = 0;
store.dispatch(0).then([&] {
CHECK(*store == 3);
++called;
});
CHECK(called == 0);
queue.step();
CHECK(called == 1);
}
TEST_CASE("combining future")
{
auto queue = lager::queue_event_loop{};
auto store =
lager::make_store<counter::action>(counter::model{},
lager::with_queue_event_loop{queue},
lager::with_futures);
auto called = 0;
store.dispatch(counter::increment_action{})
.also(store.dispatch(counter::increment_action{}))
.then([&] {
CHECK(store->value == 2);
++called;
});
CHECK(called == 0);
queue.step();
CHECK(called == 1);
}