File: settings.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (215 lines) | stat: -rw-r--r-- 7,690 bytes parent folder | download | duplicates (4)
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2019 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#define CAF_SUITE settings

#include "caf/settings.hpp"

#include "caf/test/dsl.hpp"

#include <string>

#include "caf/atom.hpp"
#include "caf/none.hpp"
#include "caf/optional.hpp"

using namespace caf;

namespace {

// TODO: switch to std::operator""s when switching to C++14
std::string operator"" _s(const char* str, size_t size) {
  return std::string(str, size);
}

struct fixture {
  settings x;

  void fill() {
    x["hello"] = "world";
    x["one"].as_dictionary()["two"].as_dictionary()["three"] = 4;
    auto& logger = x["logger"].as_dictionary();
    logger["component-blacklist"] = make_config_value_list(atom("caf"));
    logger["console"] = atom("none");
    logger["console-format"] = "%m";
    logger["console-verbosity"] = atom("trace");
    logger["file-format"] = "%r %c %p %a %t %C %M %F:%L %m%n";
    logger["inline-output"] = false;
    auto& middleman = x["middleman"].as_dictionary();
    middleman["app-identifiers"] = make_config_value_list("generic-caf-app");
    middleman["enable-automatic-connections"] = false;
    middleman["heartbeat-interval"] = 0;
    middleman["max-consecutive-reads"] = 50;
    middleman["workers"] = 3;
    auto& stream = x["stream"].as_dictionary();
    stream["credit-round-interval"] = timespan{10000000}; // 10ms;
    stream["desired-batch-complexity"] = timespan{50000}; // 50us;
    stream["max-batch-delay"] = timespan{5000000};        // 5ms;
  }
};

const config_value& unpack(const settings& x, string_view key) {
  auto i = x.find(key);
  if (i == x.end())
    CAF_FAIL("key not found in dictionary: " << key);
  return i->second;
}

template <class... Ts>
const config_value& unpack(const settings& x, string_view key,
                           const char* next_key, Ts... keys) {
  auto i = x.find(key);
  if (i == x.end())
    CAF_FAIL("key not found in dictionary: " << key);
  if (!holds_alternative<settings>(i->second))
    CAF_FAIL("value is not a dictionary: " << key);
  return unpack(get<settings>(i->second), {next_key, strlen(next_key)},
                keys...);
}

struct foobar {
  int foo = 0;
  int bar = 0;
};

} // namespace

namespace caf {

// Enable users to configure foobar's like this:
// my-value {
//   foo = 42
//   bar = 23
// }
template <>
struct config_value_access<foobar> {
  static bool is(const config_value& x) {
    auto dict = caf::get_if<config_value::dictionary>(&x);
    if (dict != nullptr) {
      return caf::get_if<int>(dict, "foo") != none
             && caf::get_if<int>(dict, "bar") != none;
    }
    return false;
  }

  static optional<foobar> get_if(const config_value* x) {
    foobar result;
    if (!is(*x))
      return none;
    const auto& dict = caf::get<config_value::dictionary>(*x);
    result.foo = caf::get<int>(dict, "foo");
    result.bar = caf::get<int>(dict, "bar");
    return result;
  }

  static foobar get(const config_value& x) {
    auto result = get_if(&x);
    if (!result)
      CAF_RAISE_ERROR("invalid type found");
    return std::move(*result);
  }
};

} // namespace caf

CAF_TEST_FIXTURE_SCOPE(settings_tests, fixture)

CAF_TEST(put) {
  put(x, "foo", "bar");
  put(x, "logger.console", atom("none"));
  put(x, "one.two.three", "four");
  CAF_CHECK_EQUAL(x.size(), 3u);
  CAF_CHECK(x.contains("foo"));
  CAF_CHECK(x.contains("logger"));
  CAF_CHECK(x.contains("one"));
  CAF_CHECK_EQUAL(unpack(x, "foo"), "bar"_s);
  CAF_CHECK_EQUAL(unpack(x, "logger", "console"), atom("none"));
  CAF_CHECK_EQUAL(unpack(x, "one", "two", "three"), "four"_s);
  put(x, "logger.console", atom("trace"));
  CAF_CHECK_EQUAL(unpack(x, "logger", "console"), atom("trace"));
}

CAF_TEST(put missing) {
  put_missing(x, "foo", "bar");
  put_missing(x, "logger.console", atom("none"));
  put_missing(x, "one.two.three", "four");
  CAF_CHECK_EQUAL(x.size(), 3u);
  CAF_CHECK(x.contains("foo"));
  CAF_CHECK(x.contains("logger"));
  CAF_CHECK(x.contains("one"));
  CAF_CHECK_EQUAL(unpack(x, "foo"), "bar"_s);
  CAF_CHECK_EQUAL(unpack(x, "logger", "console"), atom("none"));
  CAF_CHECK_EQUAL(unpack(x, "one", "two", "three"), "four"_s);
  put_missing(x, "logger.console", atom("trace"));
  CAF_CHECK_EQUAL(unpack(x, "logger", "console"), atom("none"));
}

CAF_TEST(put list) {
  put_list(x, "integers").emplace_back(42);
  CAF_CHECK(x.contains("integers"));
  CAF_CHECK_EQUAL(unpack(x, "integers"), make_config_value_list(42));
  put_list(x, "foo.bar").emplace_back("str");
  CAF_CHECK_EQUAL(unpack(x, "foo", "bar"), make_config_value_list("str"));
  put_list(x, "one.two.three").emplace_back(4);
  CAF_CHECK_EQUAL(unpack(x, "one", "two", "three"), make_config_value_list(4));
}

CAF_TEST(put dictionary) {
  put_dictionary(x, "logger").emplace("console", atom("none"));
  CAF_CHECK(x.contains("logger"));
  CAF_CHECK_EQUAL(unpack(x, "logger", "console"), atom("none"));
  put_dictionary(x, "foo.bar").emplace("value", 42);
  CAF_CHECK_EQUAL(unpack(x, "foo", "bar", "value"), 42);
  put_dictionary(x, "one.two.three").emplace("four", "five");
  CAF_CHECK_EQUAL(unpack(x, "one", "two", "three", "four"), "five"_s);
}

CAF_TEST(get and get_if) {
  fill();
  CAF_CHECK(get_if(&x, "hello") != nullptr);
  CAF_CHECK(get_if<atom_value>(&x, "hello") == none);
  CAF_REQUIRE(get_if<std::string>(&x, "hello") != none);
  CAF_CHECK(get<std::string>(x, "hello") == "world"_s);
  CAF_CHECK(get_if(&x, "logger.console") != nullptr);
  CAF_CHECK(get_if<std::string>(&x, "logger.console") == none);
  CAF_REQUIRE(get_if<atom_value>(&x, "logger.console") != none);
  CAF_CHECK(get<atom_value>(x, "logger.console") == atom("none"));
  CAF_CHECK(get_if(&x, "one.two.three") != nullptr);
  CAF_CHECK(get_if<std::string>(&x, "one.two.three") == none);
  CAF_REQUIRE(get_if<int>(&x, "one.two.three") != none);
  CAF_CHECK(get<int>(x, "one.two.three") == 4);
}

CAF_TEST(get_or) {
  fill();
  CAF_CHECK_EQUAL(get_or(x, "hello", "nobody"), "world"_s);
  CAF_CHECK_EQUAL(get_or(x, "hello", atom("nobody")), atom("nobody"));
  CAF_CHECK_EQUAL(get_or(x, "goodbye", "nobody"), "nobody"_s);
}

CAF_TEST(custom type) {
  put(x, "my-value.foo", 42);
  put(x, "my-value.bar", 24);
  CAF_REQUIRE(holds_alternative<foobar>(x, "my-value"));
  CAF_REQUIRE(get_if<foobar>(&x, "my-value") != caf::none);
  auto fb = get<foobar>(x, "my-value");
  CAF_CHECK_EQUAL(fb.foo, 42);
  CAF_CHECK_EQUAL(fb.bar, 24);
}

CAF_TEST_FIXTURE_SCOPE_END()