File: FutureTest.cpp

package info (click to toggle)
watchman 4.9.0-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 9,992 kB
  • sloc: cpp: 27,459; python: 6,538; java: 3,404; php: 3,257; ansic: 2,803; javascript: 1,116; makefile: 671; ruby: 364; sh: 124; xml: 102; lisp: 4
file content (150 lines) | stat: -rw-r--r-- 3,749 bytes parent folder | download | duplicates (3)
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
/* Copyright 2017-present Facebook, Inc.
 * Licensed under the Apache License, Version 2.0 */
#include "watchman_system.h"
#include <algorithm>
#include <cctype>
#include <string>
#include <thread>
#include "Future.h"
#include "ThreadPool.h"
#include "thirdparty/tap.h"

using namespace watchman;

void test_promise() {
  Promise<bool> p;

  auto f = p.getFuture();
  try {
    p.getFuture();
    ok(false, "should throw");
  } catch (const std::logic_error& exc) {
    ok(!strcmp(exc.what(), "Future already obtained"), "can't getFuture twice");
  }
  ok(!f.isReady(), "not yet ready");

  p.setValue(true);
  try {
    p.setValue(false);
    ok(false, "should throw");
  } catch (const std::logic_error& exc) {
    ok(!strcmp(exc.what(), "Promise already fulfilled"),
       "can't setValue twice");
  }

  ok(f.isReady(), "now ready");
  ok(f.get() == true, "got our true value");

  Promise<std::string> s;
  s.setException(std::make_exception_ptr(std::runtime_error("boo")));
  auto f2 = s.getFuture();
  ok(f2.result().hasError(), "holds an error");
  try {
    f2.get();
  } catch (const std::runtime_error& exc) {
    ok(!strcmp(exc.what(), "boo"), "has boo string");
  }
}

void test_thread() {
  Promise<std::string> p;

  std::thread thr([&p] {
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    p.setValue("done");
  });

  auto f = p.getFuture();
  ok(f.get() == "done", "done in thread");
  thr.join();
}

void test_then() {
  Promise<std::string> p;
  auto f = p.getFuture().then([](Result<std::string>&& result) {
    ok(result.value() == "noice", "got the value we wanted");
    return true;
  });

  p.setValue("noice");

  ok(f.get() == true, "resolved to a boolean future result");

  Promise<std::string> p2;
  auto f1 = p2.getFuture();
  p2.setValue("woot");
  auto f2 = f1.then([](Result<std::string>&& result) {
    auto& str = result.value();
    std::transform(
        str.begin(), str.end(), str.begin(), [](std::string::value_type c) {
          return std::toupper(c);
        });
    return str;
  });
  ok(f2.get() == "WOOT",
     "callback applied after initial promise was fulfilled");

  Promise<std::string> p3;
  std::thread thr([&p3] {
    std::this_thread::sleep_for(std::chrono::milliseconds(10));
    p3.setValue("done");
  });
  auto f3 = p3.getFuture().then(
      [](Result<std::string>&& result) { return result.value().append("!"); });
  ok(f3.get() == "done!", ".then worked across threads");
  thr.join();

  auto f4 = makeFuture<std::string>("foo").then([](
      Result<std::string>&& result) { return makeFuture(std::move(result)); });
  ok(f4.get() == "foo", "unwrapped future in .then chain");
}

void test_collect() {
  std::vector<Future<bool>> futures;
  Promise<bool> p1, p2;

  futures.emplace_back(p1.getFuture());
  futures.emplace_back(p2.getFuture());

  auto f = collectAll(futures.begin(), futures.end());

  ok(!f.isReady(), "none ready yet");
  p1.setValue(true);
  ok(!f.isReady(), "none ready yet");
  p2.setValue(false);
  ok(f.isReady(), "now ready");

  f.then([](Result<std::vector<Result<bool>>>&& result) {
    auto& vec = result.value();
    ok(vec[0].value(), "p1 result was true");
    ok(!vec[1].value(), "p2 result was false");
  });
}

void test_via() {
  ThreadPool pool;
  pool.start(1, 1024);

  Promise<bool> barrier;

  auto f = makeFuture().via(&pool).then([&barrier](Result<Unit>&&) {
    diag("waiting for barrier");
    barrier.getFuture().wait();
    return 42;
  });

  ok(!f.isReady(), "hasn't run in the thread yet");
  barrier.setValue(true);

  ok(f.get() == 42, "came back on the other side");
}

int main() {
  plan_tests(20);
  test_promise();
  test_thread();
  test_then();
  test_collect();
  test_via();
  return exit_status();
}