|
76 | 76 | ProtocolVersion, |
77 | 77 | ServerCredentials, |
78 | 78 | ) |
79 | | -from glide.constants import OK, TEncodable, TResult |
| 79 | +from glide.constants import OK, TEncodable, TFunctionStatsResponse, TResult |
80 | 80 | from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient |
81 | 81 | from glide.routes import ( |
82 | 82 | AllNodes, |
|
91 | 91 | from tests.conftest import create_client |
92 | 92 | from tests.utils.utils import ( |
93 | 93 | check_function_list_response, |
| 94 | + check_function_stats_response, |
94 | 95 | check_if_server_version_lt, |
95 | 96 | compare_maps, |
96 | 97 | convert_bytes_to_string_object, |
@@ -8087,6 +8088,153 @@ async def test_function_delete_with_routing( |
8087 | 8088 | await glide_client.function_delete(lib_name) |
8088 | 8089 | assert "Library not found" in str(e) |
8089 | 8090 |
|
| 8091 | + @pytest.mark.parametrize("cluster_mode", [False]) |
| 8092 | + @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) |
| 8093 | + async def test_function_stats(self, glide_client: GlideClient): |
| 8094 | + min_version = "7.0.0" |
| 8095 | + if await check_if_server_version_lt(glide_client, min_version): |
| 8096 | + return pytest.mark.skip(reason=f"Redis version required >= {min_version}") |
| 8097 | + |
| 8098 | + lib_name = "functionStats" |
| 8099 | + func_name = lib_name |
| 8100 | + assert await glide_client.function_flush(FlushMode.SYNC) == OK |
| 8101 | + |
| 8102 | + # function $funcName returns first argument |
| 8103 | + code = generate_lua_lib_code(lib_name, {func_name: "return args[1]"}, False) |
| 8104 | + assert await glide_client.function_load(code, True) == lib_name.encode() |
| 8105 | + |
| 8106 | + response = await glide_client.function_stats() |
| 8107 | + check_function_stats_response(response, [], 1, 1) |
| 8108 | + |
| 8109 | + code = generate_lua_lib_code( |
| 8110 | + lib_name + "_2", |
| 8111 | + {func_name + "_2": "return 'OK'", func_name + "_3": "return 42"}, |
| 8112 | + False, |
| 8113 | + ) |
| 8114 | + assert ( |
| 8115 | + await glide_client.function_load(code, True) == (lib_name + "_2").encode() |
| 8116 | + ) |
| 8117 | + |
| 8118 | + response = await glide_client.function_stats() |
| 8119 | + check_function_stats_response(response, [], 2, 3) |
| 8120 | + |
| 8121 | + assert await glide_client.function_flush(FlushMode.SYNC) == OK |
| 8122 | + |
| 8123 | + response = await glide_client.function_stats() |
| 8124 | + check_function_stats_response(response, [], 0, 0) |
| 8125 | + |
| 8126 | + @pytest.mark.parametrize("cluster_mode", [True]) |
| 8127 | + @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) |
| 8128 | + async def test_function_stats_cluster(self, glide_client: GlideClusterClient): |
| 8129 | + min_version = "7.0.0" |
| 8130 | + if await check_if_server_version_lt(glide_client, min_version): |
| 8131 | + return pytest.mark.skip(reason=f"Redis version required >= {min_version}") |
| 8132 | + |
| 8133 | + lib_name = "functionStats_without_route" |
| 8134 | + func_name = lib_name |
| 8135 | + assert await glide_client.function_flush(FlushMode.SYNC) == OK |
| 8136 | + |
| 8137 | + # function $funcName returns first argument |
| 8138 | + code = generate_lua_lib_code(lib_name, {func_name: "return args[1]"}, False) |
| 8139 | + assert await glide_client.function_load(code, True) == lib_name.encode() |
| 8140 | + |
| 8141 | + response = await glide_client.function_stats() |
| 8142 | + for node_response in response.values(): |
| 8143 | + check_function_stats_response( |
| 8144 | + cast(TFunctionStatsResponse, node_response), [], 1, 1 |
| 8145 | + ) |
| 8146 | + |
| 8147 | + code = generate_lua_lib_code( |
| 8148 | + lib_name + "_2", |
| 8149 | + {func_name + "_2": "return 'OK'", func_name + "_3": "return 42"}, |
| 8150 | + False, |
| 8151 | + ) |
| 8152 | + assert ( |
| 8153 | + await glide_client.function_load(code, True) == (lib_name + "_2").encode() |
| 8154 | + ) |
| 8155 | + |
| 8156 | + response = await glide_client.function_stats() |
| 8157 | + for node_response in response.values(): |
| 8158 | + check_function_stats_response( |
| 8159 | + cast(TFunctionStatsResponse, node_response), [], 2, 3 |
| 8160 | + ) |
| 8161 | + |
| 8162 | + assert await glide_client.function_flush(FlushMode.SYNC) == OK |
| 8163 | + |
| 8164 | + response = await glide_client.function_stats() |
| 8165 | + for node_response in response.values(): |
| 8166 | + check_function_stats_response( |
| 8167 | + cast(TFunctionStatsResponse, node_response), [], 0, 0 |
| 8168 | + ) |
| 8169 | + |
| 8170 | + @pytest.mark.parametrize("cluster_mode", [True]) |
| 8171 | + @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) |
| 8172 | + @pytest.mark.parametrize("single_route", [True, False]) |
| 8173 | + async def test_function_stats_with_routing( |
| 8174 | + self, glide_client: GlideClusterClient, single_route: bool |
| 8175 | + ): |
| 8176 | + min_version = "7.0.0" |
| 8177 | + if await check_if_server_version_lt(glide_client, min_version): |
| 8178 | + return pytest.mark.skip(reason=f"Redis version required >= {min_version}") |
| 8179 | + |
| 8180 | + route = ( |
| 8181 | + SlotKeyRoute(SlotType.PRIMARY, get_random_string(10)) |
| 8182 | + if single_route |
| 8183 | + else AllPrimaries() |
| 8184 | + ) |
| 8185 | + lib_name = "functionStats_with_route_" + str(single_route) |
| 8186 | + func_name = lib_name |
| 8187 | + assert await glide_client.function_flush(FlushMode.SYNC, route) == OK |
| 8188 | + |
| 8189 | + # function $funcName returns first argument |
| 8190 | + code = generate_lua_lib_code(lib_name, {func_name: "return args[1]"}, False) |
| 8191 | + assert await glide_client.function_load(code, True, route) == lib_name.encode() |
| 8192 | + |
| 8193 | + response = await glide_client.function_stats(route) |
| 8194 | + if single_route: |
| 8195 | + check_function_stats_response( |
| 8196 | + cast(TFunctionStatsResponse, response), [], 1, 1 |
| 8197 | + ) |
| 8198 | + else: |
| 8199 | + for node_response in response.values(): |
| 8200 | + check_function_stats_response( |
| 8201 | + cast(TFunctionStatsResponse, node_response), [], 1, 1 |
| 8202 | + ) |
| 8203 | + |
| 8204 | + code = generate_lua_lib_code( |
| 8205 | + lib_name + "_2", |
| 8206 | + {func_name + "_2": "return 'OK'", func_name + "_3": "return 42"}, |
| 8207 | + False, |
| 8208 | + ) |
| 8209 | + assert ( |
| 8210 | + await glide_client.function_load(code, True, route) |
| 8211 | + == (lib_name + "_2").encode() |
| 8212 | + ) |
| 8213 | + |
| 8214 | + response = await glide_client.function_stats(route) |
| 8215 | + if single_route: |
| 8216 | + check_function_stats_response( |
| 8217 | + cast(TFunctionStatsResponse, response), [], 2, 3 |
| 8218 | + ) |
| 8219 | + else: |
| 8220 | + for node_response in response.values(): |
| 8221 | + check_function_stats_response( |
| 8222 | + cast(TFunctionStatsResponse, node_response), [], 2, 3 |
| 8223 | + ) |
| 8224 | + |
| 8225 | + assert await glide_client.function_flush(FlushMode.SYNC, route) == OK |
| 8226 | + |
| 8227 | + response = await glide_client.function_stats(route) |
| 8228 | + if single_route: |
| 8229 | + check_function_stats_response( |
| 8230 | + cast(TFunctionStatsResponse, response), [], 0, 0 |
| 8231 | + ) |
| 8232 | + else: |
| 8233 | + for node_response in response.values(): |
| 8234 | + check_function_stats_response( |
| 8235 | + cast(TFunctionStatsResponse, node_response), [], 0, 0 |
| 8236 | + ) |
| 8237 | + |
8090 | 8238 | @pytest.mark.parametrize("cluster_mode", [True]) |
8091 | 8239 | @pytest.mark.parametrize("protocol", [ProtocolVersion.RESP2, ProtocolVersion.RESP3]) |
8092 | 8240 | async def test_fcall_with_key(self, glide_client: GlideClusterClient): |
|
0 commit comments