1/*
2 * Copyright 2015-present Facebook, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <folly/futures/Future.h>
18#include <folly/portability/GTest.h>
19
20using namespace folly;
21
22TEST(WillEqual, basic) {
23 // both p1 and p2 already fulfilled
24 {
25 Promise<int> p1;
26 Promise<int> p2;
27 p1.setValue(27);
28 p2.setValue(27);
29 auto f1 = p1.getFuture();
30 auto f2 = p2.getFuture();
31 EXPECT_TRUE(f1.willEqual(f2).get());
32 }
33 {
34 Promise<int> p1;
35 Promise<int> p2;
36 p1.setValue(27);
37 p2.setValue(36);
38 auto f1 = p1.getFuture();
39 auto f2 = p2.getFuture();
40 EXPECT_FALSE(f1.willEqual(f2).get());
41 }
42 // both p1 and p2 not yet fulfilled
43 {
44 Promise<int> p1;
45 Promise<int> p2;
46 auto f1 = p1.getFuture();
47 auto f2 = p2.getFuture();
48 auto f3 = f1.willEqual(f2);
49 p1.setValue(27);
50 p2.setValue(27);
51 EXPECT_TRUE(std::move(f3).get());
52 }
53 {
54 Promise<int> p1;
55 Promise<int> p2;
56 auto f1 = p1.getFuture();
57 auto f2 = p2.getFuture();
58 auto f3 = f1.willEqual(f2);
59 p1.setValue(27);
60 p2.setValue(36);
61 EXPECT_FALSE(std::move(f3).get());
62 }
63 // p1 already fulfilled, p2 not yet fulfilled
64 {
65 Promise<int> p1;
66 Promise<int> p2;
67 p1.setValue(27);
68 auto f1 = p1.getFuture();
69 auto f2 = p2.getFuture();
70 auto f3 = f1.willEqual(f2);
71 p2.setValue(27);
72 EXPECT_TRUE(std::move(f3).get());
73 }
74 {
75 Promise<int> p1;
76 Promise<int> p2;
77 p1.setValue(27);
78 auto f1 = p1.getFuture();
79 auto f2 = p2.getFuture();
80 auto f3 = f1.willEqual(f2);
81 p2.setValue(36);
82 EXPECT_FALSE(std::move(f3).get());
83 }
84 // p2 already fulfilled, p1 not yet fulfilled
85 {
86 Promise<int> p1;
87 Promise<int> p2;
88 p2.setValue(27);
89 auto f1 = p1.getFuture();
90 auto f2 = p2.getFuture();
91 auto f3 = f1.willEqual(f2);
92 p1.setValue(27);
93 EXPECT_TRUE(std::move(f3).get());
94 }
95 {
96 Promise<int> p1;
97 Promise<int> p2;
98 p2.setValue(36);
99 auto f1 = p1.getFuture();
100 auto f2 = p2.getFuture();
101 auto f3 = f1.willEqual(f2);
102 p1.setValue(27);
103 EXPECT_FALSE(std::move(f3).get());
104 }
105}
106