aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorCraig Tiller <ctiller@google.com>2024-01-02 09:45:45 -0800
committerCopybara-Service <copybara-worker@google.com>2024-01-02 09:59:38 -0800
commit75e5ebcb14a7a16e1cb5994853e2a6215dbf97e8 (patch)
tree50145b942427cdbbef3d3d6d1af89a3f11e76a13 /test
parent1383c4629bcc9c74da5133286734ab28e88eabd3 (diff)
downloadgrpc-grpc-75e5ebcb14a7a16e1cb5994853e2a6215dbf97e8.tar.gz
[promises] Add a switch primitive (#35424)
Remove the old `switch` library - this used to be an implementation detail of `Seq`, `TrySeq` - but has become unused. Add a new user facing primitive `Switch` that fills a similar role to `switch` in C++ - selecting a promise to execute based on a primitive discriminator - much like `If` allows selection based on a boolean discriminator now. A future change will optimize this to actually lower the `Switch` into an actual `switch` statement, but for right now I want to get the functionality in. Closes #35424 COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/35424 from ctiller:switchy 5308a914c689b79e5e83f218ae0cd7e88bc0034e PiperOrigin-RevId: 595140965
Diffstat (limited to 'test')
-rw-r--r--test/core/promise/BUILD11
-rw-r--r--test/core/promise/switch_test.cc69
2 files changed, 80 insertions, 0 deletions
diff --git a/test/core/promise/BUILD b/test/core/promise/BUILD
index b4c4ce855c..a035820d5a 100644
--- a/test/core/promise/BUILD
+++ b/test/core/promise/BUILD
@@ -198,6 +198,17 @@ grpc_cc_test(
)
grpc_cc_test(
+ name = "switch_test",
+ srcs = ["switch_test.cc"],
+ external_deps = ["gtest"],
+ language = "c++",
+ tags = ["promise_test"],
+ uses_event_engine = False,
+ uses_polling = False,
+ deps = ["//src/core:switch"],
+)
+
+grpc_cc_test(
name = "loop_test",
srcs = ["loop_test.cc"],
external_deps = ["gtest"],
diff --git a/test/core/promise/switch_test.cc b/test/core/promise/switch_test.cc
new file mode 100644
index 0000000000..ed18c1113e
--- /dev/null
+++ b/test/core/promise/switch_test.cc
@@ -0,0 +1,69 @@
+// Copyright 2023 gRPC authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/core/lib/promise/switch.h"
+
+#include "gtest/gtest.h"
+
+namespace grpc_core {
+
+TEST(SwitchTest, JustDefault) {
+ EXPECT_EQ(Switch(42, Default([] { return 1; }))(), Poll<int>(1));
+}
+
+TEST(SwitchTest, ThreeCases) {
+ auto test_switch = [](int d) {
+ return Switch(d, Case(1, [] { return 25; }), Case(2, [] { return 95; }),
+ Case(3, [] { return 68; }), Default([] { return 52; }));
+ };
+ EXPECT_EQ(test_switch(0)(), Poll<int>(52));
+ EXPECT_EQ(test_switch(1)(), Poll<int>(25));
+ EXPECT_EQ(test_switch(2)(), Poll<int>(95));
+ EXPECT_EQ(test_switch(3)(), Poll<int>(68));
+ EXPECT_EQ(test_switch(4)(), Poll<int>(52));
+}
+
+TEST(SwitchTest, Pending) {
+ auto test_switch = [](int d) {
+ return Switch(d, Case(42, []() -> Poll<int> { return Pending{}; }),
+ Case(1, [] { return 25; }), Case(2, [] { return 95; }),
+ Case(3, [] { return 68; }), Default([] { return 52; }));
+ };
+ EXPECT_EQ(test_switch(0)(), Poll<int>(52));
+ EXPECT_EQ(test_switch(1)(), Poll<int>(25));
+ EXPECT_EQ(test_switch(2)(), Poll<int>(95));
+ EXPECT_EQ(test_switch(3)(), Poll<int>(68));
+ EXPECT_EQ(test_switch(4)(), Poll<int>(52));
+ EXPECT_EQ(test_switch(42)(), Poll<int>(Pending{}));
+}
+
+TEST(SwitchTest, ThreeCasesFromEnum) {
+ enum class X : uint8_t { A, B, C };
+
+ auto test_switch = [](X d) {
+ return Switch(d, Case(X::A, [] { return 25; }),
+ Case(X::B, [] { return 95; }), Case(X::C, [] { return 68; }),
+ Default([] { return 52; }));
+ };
+ EXPECT_EQ(test_switch(X::A)(), Poll<int>(25));
+ EXPECT_EQ(test_switch(X::B)(), Poll<int>(95));
+ EXPECT_EQ(test_switch(X::C)(), Poll<int>(68));
+}
+
+} // namespace grpc_core
+
+int main(int argc, char** argv) {
+ ::testing::InitGoogleTest(&argc, argv);
+ return RUN_ALL_TESTS();
+}