| 120 | } |
| 121 | |
| 122 | void future_test::test_future_promise() |
| 123 | { |
| 124 | /** |
| 125 | * Note: As we currently have no exception |
| 126 | * propagation support, we do not test |
| 127 | * exceptions here. However, the logic there |
| 128 | * is basically identical to that of the value |
| 129 | * setting. |
| 130 | */ |
| 131 | auto [p1, f1, s1] = prepare<int>(); |
| 132 | test_eq("refcount in basic case", s1->refs(), 2); |
| 133 | |
| 134 | p1.set_value(1); |
| 135 | test("simple case valid", f1.valid()); |
| 136 | test_eq("simple case get", f1.get(), 1); |
| 137 | |
| 138 | auto [p2, f2, s2] = prepare<int>(); |
| 139 | std::thread t2{ |
| 140 | [&p2](){ |
| 141 | std::this_thread::sleep_for(20ms); |
| 142 | p2.set_value(42); |
| 143 | } |
| 144 | }; |
| 145 | |
| 146 | test_eq("parallel get waits and has correct value", f2.get(), 42); |
| 147 | |
| 148 | auto [p3, f3, s3] = prepare<int>(); |
| 149 | std::thread t3{ |
| 150 | [&p3](){ |
| 151 | std::this_thread::sleep_for(20ms); |
| 152 | p3.set_value(42); |
| 153 | } |
| 154 | }; |
| 155 | |
| 156 | f3.wait(); |
| 157 | test("after wait value is set", s3->is_set()); |
| 158 | test_eq("after wait value is correct", s3->get(), 42); |
| 159 | |
| 160 | auto [p4, f4, s4] = prepare<int>(); |
| 161 | std::thread t4{ |
| 162 | [&p4](){ |
| 163 | /* p4.set_value_at_thread_exit(42); */ |
| 164 | } |
| 165 | }; |
| 166 | std::this_thread::sleep_for(10ms); // Let the value be set inside state. |
| 167 | |
| 168 | /* test("shared state marked as ready at thread exit", s4->is_set()); */ |
| 169 | /* test_eq("value set inside state while in thread", s4->get(), 42); */ |
| 170 | /* test_eq("value set at thread exit", f4.get(), 42); */ |
| 171 | |
| 172 | mock::clear(); |
| 173 | std::aux::shared_state<std::test::mock>* s5{}; |
| 174 | { |
| 175 | std::promise<std::test::mock> p5{}; |
| 176 | s5 = p5.__state(); |
| 177 | test_eq("refcount with just promise", s5->refs(), 1); |
| 178 | { |
| 179 | auto f5 = p5.get_future(); |