brpc_controller_unittest.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Licensed to the Apache Software Foundation (ASF) under one
  2. // or more contributor license agreements. See the NOTICE file
  3. // distributed with this work for additional information
  4. // regarding copyright ownership. The ASF licenses this file
  5. // to you under the Apache License, Version 2.0 (the
  6. // "License"); you may not use this file except in compliance
  7. // with the License. You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing,
  12. // software distributed under the License is distributed on an
  13. // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  14. // KIND, either express or implied. See the License for the
  15. // specific language governing permissions and limitations
  16. // under the License.
  17. // brpc - A framework to host and access services throughout Baidu.
  18. // Date: Sun Jul 13 15:04:18 CST 2014
  19. #include <gtest/gtest.h>
  20. #include <google/protobuf/stubs/common.h>
  21. #include "butil/logging.h"
  22. #include "butil/time.h"
  23. #include "butil/macros.h"
  24. #include "brpc/socket.h"
  25. #include "brpc/server.h"
  26. #include "brpc/channel.h"
  27. #include "brpc/controller.h"
  28. class ControllerTest : public ::testing::Test{
  29. protected:
  30. ControllerTest() {};
  31. virtual ~ControllerTest(){};
  32. virtual void SetUp() {};
  33. virtual void TearDown() {};
  34. };
  35. void MyCancelCallback(bool* cancel_flag) {
  36. *cancel_flag = true;
  37. }
  38. TEST_F(ControllerTest, notify_on_failed) {
  39. brpc::SocketId id = 0;
  40. ASSERT_EQ(0, brpc::Socket::Create(brpc::SocketOptions(), &id));
  41. brpc::Controller cntl;
  42. cntl._current_call.peer_id = id;
  43. ASSERT_FALSE(cntl.IsCanceled());
  44. bool cancel = false;
  45. cntl.NotifyOnCancel(brpc::NewCallback(&MyCancelCallback, &cancel));
  46. // Trigger callback
  47. brpc::Socket::SetFailed(id);
  48. usleep(20000); // sleep a while to wait for the canceling which will be
  49. // happening in another thread.
  50. ASSERT_TRUE(cancel);
  51. ASSERT_TRUE(cntl.IsCanceled());
  52. }
  53. TEST_F(ControllerTest, notify_on_destruction) {
  54. brpc::SocketId id = 0;
  55. ASSERT_EQ(0, brpc::Socket::Create(brpc::SocketOptions(), &id));
  56. brpc::Controller* cntl = new brpc::Controller;
  57. cntl->_current_call.peer_id = id;
  58. ASSERT_FALSE(cntl->IsCanceled());
  59. bool cancel = false;
  60. cntl->NotifyOnCancel(brpc::NewCallback(&MyCancelCallback, &cancel));
  61. // Trigger callback
  62. delete cntl;
  63. ASSERT_TRUE(cancel);
  64. }
  65. #if ! BRPC_WITH_GLOG
  66. static bool endsWith(const std::string& s1, const butil::StringPiece& s2) {
  67. if (s1.size() < s2.size()) {
  68. return false;
  69. }
  70. return memcmp(s1.data() + s1.size() - s2.size(), s2.data(), s2.size()) == 0;
  71. }
  72. static bool startsWith(const std::string& s1, const butil::StringPiece& s2) {
  73. if (s1.size() < s2.size()) {
  74. return false;
  75. }
  76. return memcmp(s1.data(), s2.data(), s2.size()) == 0;
  77. }
  78. DECLARE_bool(log_as_json);
  79. TEST_F(ControllerTest, SessionKV) {
  80. FLAGS_log_as_json = false;
  81. logging::StringSink sink1;
  82. auto oldSink = logging::SetLogSink(&sink1);
  83. {
  84. brpc::Controller cntl;
  85. cntl.set_log_id(123); // not working now
  86. // set
  87. cntl.SessionKV().Set("Apple", 1234567);
  88. cntl.SessionKV().Set("Baidu", "Building");
  89. // get
  90. auto v1 = cntl.SessionKV().Get("Apple");
  91. ASSERT_TRUE(v1);
  92. ASSERT_EQ("1234567", *v1);
  93. auto v2 = cntl.SessionKV().Get("Baidu");
  94. ASSERT_TRUE(v2);
  95. ASSERT_EQ("Building", *v2);
  96. // override
  97. cntl.SessionKV().Set("Baidu", "NewStuff");
  98. v2 = cntl.SessionKV().Get("Baidu");
  99. ASSERT_TRUE(v2);
  100. ASSERT_EQ("NewStuff", *v2);
  101. cntl.SessionKV().Set("Cisco", 33.33);
  102. CLOGW(&cntl) << "My WARNING Log";
  103. ASSERT_TRUE(endsWith(sink1, "] My WARNING Log")) << sink1;
  104. ASSERT_TRUE(startsWith(sink1, "W")) << sink1;
  105. sink1.clear();
  106. cntl.set_request_id("abcdEFG-456");
  107. CLOGE(&cntl) << "My ERROR Log";
  108. ASSERT_TRUE(endsWith(sink1, "] @rid=abcdEFG-456 My ERROR Log")) << sink1;
  109. ASSERT_TRUE(startsWith(sink1, "E")) << sink1;
  110. sink1.clear();
  111. FLAGS_log_as_json = true;
  112. }
  113. ASSERT_TRUE(endsWith(sink1, R"(,"@rid":"abcdEFG-456","M":"Session ends.","Baidu":"NewStuff","Cisco":"33.330000","Apple":"1234567"})")) << sink1;
  114. ASSERT_TRUE(startsWith(sink1, R"({"L":"I",)")) << sink1;
  115. logging::SetLogSink(oldSink);
  116. }
  117. #endif