test_config.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*************************************************************************
  2. * Copyright (C) [2019] by Cambricon, Inc. All rights reserved
  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. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  15. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. * THE SOFTWARE.
  19. *************************************************************************/
  20. #include <gtest/gtest.h>
  21. #include <stdio.h>
  22. #include <fstream>
  23. #include <string>
  24. #include "cnstream_config.hpp"
  25. #include "test_base.hpp"
  26. namespace cnstream {
  27. TEST(CoreConfig, ParseByJSONFile) {
  28. class TestConfig : public CNConfigBase {
  29. public:
  30. bool ParseByJSONStr(const std::string& jstr) {return true;}
  31. } test_config;
  32. auto config_file = CreateTempFile("test_config");
  33. EXPECT_TRUE(test_config.ParseByJSONFile(config_file.second));
  34. EXPECT_TRUE(test_config.config_root_dir.empty());
  35. // wrong path
  36. EXPECT_FALSE(test_config.ParseByJSONFile("wrong_file_path"));
  37. unlink(config_file.second.c_str());
  38. close(config_file.first);
  39. }
  40. TEST(CoreConfig, ProfilerConfig) {
  41. ProfilerConfig config;
  42. std::string jstr = "{ \"enable_profiling\": true, \"enable_tracing\": true, \"trace_event_capacity\": 1}";
  43. std::string wrong_jstr0 = "{ \"enable_profiling\": true, \"enable_tracing\": true, \"trace_event_capacity\":";
  44. std::string wrong_jstr1 = "{ \"enable_profiling\": \"ds\", \"enable_tracing\": true, \"trace_event_capacity\": 1}";
  45. std::string wrong_jstr2 = "{ \"enable_profiling\": true, \"enable_tracing\": \"ss\", \"trace_event_capacity\": 1}";
  46. std::string wrong_jstr3 = "{ \"enable_profiling\": true, \"enable_tracing\": true, \"trace_event_capacity\": \"f\"}";
  47. std::string wrong_jstr4 = "{ \"enable_profiling\": true, \"abc\": true}";
  48. EXPECT_FALSE(config.ParseByJSONStr(wrong_jstr0));
  49. EXPECT_FALSE(config.ParseByJSONStr(wrong_jstr1));
  50. EXPECT_FALSE(config.ParseByJSONStr(wrong_jstr2));
  51. EXPECT_FALSE(config.ParseByJSONStr(wrong_jstr3));
  52. EXPECT_FALSE(config.ParseByJSONStr(wrong_jstr4));
  53. EXPECT_TRUE(config.ParseByJSONStr(jstr));
  54. EXPECT_TRUE(config.enable_profiling);
  55. EXPECT_TRUE(config.enable_tracing);
  56. EXPECT_EQ(1, config.trace_event_capacity);
  57. }
  58. TEST(CoreConfig, CNModuleConfig) {
  59. CNModuleConfig config;
  60. // case1: wrong json format
  61. std::string jstr = "{\"parallelism\" : 1,}";
  62. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  63. // case2: no class_name
  64. jstr = "{\"parallelism\" : 1}";
  65. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  66. // case3: class_name with wrong format
  67. jstr = "{\"class_name\" : 3}";
  68. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  69. // case4: parallelism with wrong fromat
  70. jstr = "{\"class_name\" : \"test_class_name\","
  71. "\"parallelism\" : \"wrong_format\"}";
  72. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  73. // case5: max input queue size with wrong fromat
  74. jstr = "{\"class_name\" : \"test_class_name\","
  75. "\"max_input_queue_size\" : \"wrong_format\"}";
  76. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  77. // case6: next modules not an array type
  78. jstr = "{\"class_name\" : \"test_class_name\","
  79. "\"next_modules\" : \"wrong_format\"}";
  80. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  81. // case7: next modules not a string array
  82. jstr = "{\"class_name\" : \"test_class_name\","
  83. "\"next_modules\" : [1, \"test_next_module\"]}";
  84. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  85. // case8: custom_params not an object type
  86. jstr = "{\"class_name\" : \"test_class_name\","
  87. "\"custom_params\" : \"wrong_type\"}";
  88. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  89. // case9: success
  90. jstr = "{\"class_name\" : \"test_class_name\","
  91. "\"parallelism\" : 15,"
  92. "\"max_input_queue_size\" : 30,"
  93. "\"next_modules\" : [\"next_module1\", \"next_module2\"],"
  94. "\"custom_params\" : {\"param1\" : 20, \"param2\" : \"param2_value\"}"
  95. "}";
  96. config.config_root_dir = "test_root_dir";
  97. EXPECT_TRUE(config.ParseByJSONStr(jstr));
  98. EXPECT_EQ(config.className, "test_class_name");
  99. EXPECT_EQ(config.parallelism, 15);
  100. EXPECT_EQ(config.maxInputQueueSize, 30);
  101. EXPECT_EQ(config.next.size(), 2);
  102. EXPECT_NE(config.next.find("next_module1"), config.next.end());
  103. EXPECT_NE(config.next.find("next_module2"), config.next.end());
  104. EXPECT_EQ(config.parameters.size(), 3);
  105. EXPECT_EQ(config.parameters["param1"], "20");
  106. EXPECT_EQ(config.parameters["param2"], "param2_value");
  107. EXPECT_EQ(config.config_root_dir, config.parameters[CNS_JSON_DIR_PARAM_NAME]);
  108. }
  109. TEST(CoreConfig, CNSubgraphConfig) {
  110. CNSubgraphConfig config;
  111. // case1: wrong json format
  112. std::string jstr = "{,}";
  113. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  114. // case2: no config path
  115. jstr = "{}";
  116. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  117. // case3: config path with wrong format
  118. jstr = "{\"config_path\": 123}";
  119. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  120. // case4: next modules not an array type
  121. jstr = "{\"config_path\": \"test_config_path\","
  122. "\"next_modules\" : \"wrong_format\"}";
  123. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  124. // case5: next modules not a string array
  125. jstr = "{\"config_path\": \"test_config_path\","
  126. "\"next_modules\" : [1, \"test_next_module\"]}";
  127. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  128. // case6: success
  129. jstr = "{\"config_path\": \"test_config_path\","
  130. "\"next_modules\" : [\"next_module1\", \"next_module2\"]"
  131. "}";
  132. config.config_root_dir = "test_root_dir/";
  133. EXPECT_TRUE(config.ParseByJSONStr(jstr));
  134. EXPECT_EQ("test_root_dir/test_config_path", config.config_path);
  135. EXPECT_EQ("test_root_dir/test_config_path", config.config_path);
  136. EXPECT_EQ(config.next.size(), 2);
  137. EXPECT_NE(config.next.find("next_module1"), config.next.end());
  138. EXPECT_NE(config.next.find("next_module2"), config.next.end());
  139. }
  140. TEST(CoreConfig, CNGraphConfig) {
  141. CNGraphConfig config;
  142. // case1: wrong json format
  143. std::string jstr = "{,}";
  144. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  145. // case2: wrong profiler config failed
  146. jstr = "{\"profiler_config\" : { \"enable_profiling\": \"ds\", \"enable_tracing\": true,"
  147. " \"trace_event_capacity\": 1}}";
  148. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  149. // case3: wrong subgraph config
  150. jstr = "{\"subgraph:test_subgraph\" : {}}";
  151. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  152. // case4: wrong module config
  153. jstr = "{\"test_module\" : {}}";
  154. EXPECT_FALSE(config.ParseByJSONStr(jstr));
  155. // case5: success
  156. jstr = "{"
  157. "\"profiler_config\" : {"
  158. "\"enable_profiling\" : true,"
  159. "\"enable_tracing\" : true"
  160. "},"
  161. "\"node1\" : {"
  162. "\"class_name\" : \"test_class\","
  163. "\"parallelism\" : 2,"
  164. "\"max_input_queue_size\" : 15,"
  165. "\"next_modules\" : [\"subgraph:node2\"]"
  166. "},"
  167. "\"subgraph:node2\" : {"
  168. "\"config_path\" : \"test_config_path\""
  169. "}"
  170. "}";
  171. EXPECT_TRUE(config.ParseByJSONStr(jstr));
  172. EXPECT_EQ(1, config.module_configs.size());
  173. EXPECT_EQ(1, config.subgraph_configs.size());
  174. EXPECT_TRUE(config.profiler_config.enable_profiling);
  175. EXPECT_TRUE(config.profiler_config.enable_tracing);
  176. }
  177. } // namespace cnstream