| 1 |
// Copyright 2021 gRPC authors. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
#include "src/core/lib/promise/exec_ctx_wakeup_scheduler.h" |
| 16 |
|
| 17 |
#include <stdlib.h> |
| 18 |
|
| 19 |
#include <memory> |
| 20 |
|
| 21 |
#include "absl/status/status.h" |
| 22 |
#include "gtest/gtest.h" |
| 23 |
|
| 24 |
#include "src/core/lib/promise/activity.h" |
| 25 |
#include "src/core/lib/promise/poll.h" |
| 26 |
|
| 27 |
namespace grpc_core { |
| 28 |
|
| 29 |
TEST(ExecCtxWakeupSchedulerTest, Works) { |
| 30 |
int state = 0; |
| 31 |
bool done = false; |
| 32 |
auto activity = MakeActivity( |
| 33 |
[&state]() mutable -> Poll<absl::Status> { |
| 34 |
++state; |
| 35 |
switch (state) { |
| 36 |
case 1: |
| 37 |
return Pending(); |
| 38 |
case 2: |
| 39 |
return absl::OkStatus(); |
| 40 |
default: |
| 41 |
abort(); |
| 42 |
} |
| 43 |
}, |
| 44 |
ExecCtxWakeupScheduler(), |
| 45 |
[&done](absl::Status status) { |
| 46 |
EXPECT_EQ(status, absl::OkStatus()); |
| 47 |
done = true; |
| 48 |
}); |
| 49 |
|
| 50 |
EXPECT_EQ(state, 1); |
| 51 |
EXPECT_FALSE(done); |
| 52 |
{ |
| 53 |
ExecCtx exec_ctx; |
| 54 |
EXPECT_FALSE(exec_ctx.HasWork()); |
| 55 |
activity->ForceWakeup(); |
| 56 |
EXPECT_TRUE(exec_ctx.HasWork()); |
| 57 |
EXPECT_EQ(state, 1); |
| 58 |
EXPECT_FALSE(done); |
| 59 |
} |
| 60 |
EXPECT_EQ(state, 2); |
| 61 |
EXPECT_TRUE(done); |
| 62 |
} |
| 63 |
|
| 64 |
} // namespace grpc_core |
| 65 |
|
| 66 |
int main(int argc, char** argv) { |
| 67 |
::testing::InitGoogleTest(&argc, argv); |
| 68 |
return RUN_ALL_TESTS(); |
| 69 |
} |