|
1 | 1 | #include "envoy/grpc/async_client.h" |
| 2 | +#include "envoy/redis/async_client.h" |
2 | 3 |
|
3 | 4 | #include "source/common/http/message_impl.h" |
4 | 5 | #include "source/extensions/filters/http/wasm/wasm_filter.h" |
@@ -795,6 +796,115 @@ TEST_P(WasmHttpFilterTest, RedisCall) { |
795 | 796 | EXPECT_NE(callbacks, nullptr); |
796 | 797 | } |
797 | 798 |
|
| 799 | +#if defined(HIGRESS) |
| 800 | +// Unit test for AsyncClientConfig parameter parsing |
| 801 | +TEST(RedisAsyncClientConfigTest, ParseBufferParamsFromQueryString) { |
| 802 | + // Test with all parameters specified |
| 803 | + { |
| 804 | + std::map<std::string, std::string> params = { |
| 805 | + {"db", "1"}, |
| 806 | + {"buffer_flush_timeout", "5"}, |
| 807 | + {"max_buffer_size_before_flush", "2048"} |
| 808 | + }; |
| 809 | + Redis::AsyncClientConfig config("testuser", "testpass", 1000, std::move(params)); |
| 810 | + |
| 811 | + EXPECT_EQ(config.auth_username_, "testuser"); |
| 812 | + EXPECT_EQ(config.auth_password_, "testpass"); |
| 813 | + EXPECT_EQ(config.op_timeout_.count(), 1000); |
| 814 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 5); |
| 815 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 2048); |
| 816 | + EXPECT_EQ(config.params_.at("db"), "1"); |
| 817 | + } |
| 818 | + |
| 819 | + // Test with only buffer_flush_timeout specified (max_buffer uses default) |
| 820 | + { |
| 821 | + std::map<std::string, std::string> params = { |
| 822 | + {"buffer_flush_timeout", "1"} |
| 823 | + }; |
| 824 | + Redis::AsyncClientConfig config("admin", "123456", 2000, std::move(params)); |
| 825 | + |
| 826 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 1); |
| 827 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 1024); // default value |
| 828 | + } |
| 829 | + |
| 830 | + // Test with only max_buffer_size_before_flush specified (timeout uses default) |
| 831 | + { |
| 832 | + std::map<std::string, std::string> params = { |
| 833 | + {"max_buffer_size_before_flush", "512"} |
| 834 | + }; |
| 835 | + Redis::AsyncClientConfig config("admin", "123456", 2000, std::move(params)); |
| 836 | + |
| 837 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 3); // default value |
| 838 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 512); |
| 839 | + } |
| 840 | + |
| 841 | + // Test with no buffer params (both use defaults) |
| 842 | + { |
| 843 | + std::map<std::string, std::string> params = { |
| 844 | + {"db", "0"} |
| 845 | + }; |
| 846 | + Redis::AsyncClientConfig config("user", "pass", 500, std::move(params)); |
| 847 | + |
| 848 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 3); // default 3ms |
| 849 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 1024); // default 1024 bytes |
| 850 | + } |
| 851 | + |
| 852 | + // Test with invalid buffer_flush_timeout (should use default) |
| 853 | + { |
| 854 | + std::map<std::string, std::string> params = { |
| 855 | + {"buffer_flush_timeout", "invalid_number"} |
| 856 | + }; |
| 857 | + Redis::AsyncClientConfig config("user", "pass", 500, std::move(params)); |
| 858 | + |
| 859 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 3); // default due to parse error |
| 860 | + } |
| 861 | + |
| 862 | + // Test with invalid max_buffer_size_before_flush (should use default) |
| 863 | + { |
| 864 | + std::map<std::string, std::string> params = { |
| 865 | + {"max_buffer_size_before_flush", "not_a_number"} |
| 866 | + }; |
| 867 | + Redis::AsyncClientConfig config("user", "pass", 500, std::move(params)); |
| 868 | + |
| 869 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 1024); // default due to parse error |
| 870 | + } |
| 871 | + |
| 872 | + // Test with zero values (edge case - disable buffering) |
| 873 | + { |
| 874 | + std::map<std::string, std::string> params = { |
| 875 | + {"buffer_flush_timeout", "0"}, |
| 876 | + {"max_buffer_size_before_flush", "0"} |
| 877 | + }; |
| 878 | + Redis::AsyncClientConfig config("user", "pass", 500, std::move(params)); |
| 879 | + |
| 880 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 0); |
| 881 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 0); |
| 882 | + } |
| 883 | + |
| 884 | + // Test with very large values (within uint32 range) |
| 885 | + { |
| 886 | + std::map<std::string, std::string> params = { |
| 887 | + {"buffer_flush_timeout", "10000"}, |
| 888 | + {"max_buffer_size_before_flush", "1048576"} // 1MB |
| 889 | + }; |
| 890 | + Redis::AsyncClientConfig config("user", "pass", 500, std::move(params)); |
| 891 | + |
| 892 | + EXPECT_EQ(config.buffer_flush_timeout_.count(), 10000); |
| 893 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 1048576); |
| 894 | + } |
| 895 | + |
| 896 | + // Test with value exceeding uint32 max (should use default) |
| 897 | + { |
| 898 | + std::map<std::string, std::string> params = { |
| 899 | + {"max_buffer_size_before_flush", "99999999999999"} // exceeds uint32::max |
| 900 | + }; |
| 901 | + Redis::AsyncClientConfig config("user", "pass", 500, std::move(params)); |
| 902 | + |
| 903 | + EXPECT_EQ(config.max_buffer_size_before_flush_, 1024); // default due to overflow |
| 904 | + } |
| 905 | +} |
| 906 | +#endif |
| 907 | + |
798 | 908 | TEST_P(WasmHttpFilterTest, DisableClearRouteCache) { |
799 | 909 | if (std::get<1>(GetParam()) == "rust") { |
800 | 910 | // This feature is not supported in rust test code |
|
0 commit comments