brpc_extension_unittest.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 <sys/types.h>
  20. #include <sys/socket.h>
  21. #include <map>
  22. #include <gtest/gtest.h>
  23. #include "butil/time.h"
  24. #include "butil/macros.h"
  25. #include "brpc/extension.h"
  26. class ExtensionTest : public ::testing::Test{
  27. protected:
  28. ExtensionTest(){
  29. };
  30. virtual ~ExtensionTest(){};
  31. virtual void SetUp() {
  32. };
  33. virtual void TearDown() {
  34. };
  35. };
  36. inline brpc::Extension<const int>* ConstIntExtension() {
  37. return brpc::Extension<const int>::instance();
  38. }
  39. inline brpc::Extension<int>* IntExtension() {
  40. return brpc::Extension<int>::instance();
  41. }
  42. const int g_foo = 10;
  43. const int g_bar = 20;
  44. TEST_F(ExtensionTest, basic) {
  45. ConstIntExtension()->Register("foo", NULL);
  46. ConstIntExtension()->Register("foo", &g_foo);
  47. ConstIntExtension()->Register("bar", &g_bar);
  48. int* val1 = new int(0xbeef);
  49. int* val2 = new int(0xdead);
  50. ASSERT_EQ(0, IntExtension()->Register("hello", val1));
  51. ASSERT_EQ(-1, IntExtension()->Register("hello", val1));
  52. IntExtension()->Register("there", val2);
  53. ASSERT_EQ(val1, IntExtension()->Find("hello"));
  54. ASSERT_EQ(val2, IntExtension()->Find("there"));
  55. std::ostringstream os;
  56. IntExtension()->List(os, ':');
  57. ASSERT_EQ("hello:there", os.str());
  58. os.str("");
  59. ConstIntExtension()->List(os, ':');
  60. ASSERT_EQ("bar:foo", os.str());
  61. }