From e31e175e477bc976d8f193c5b0b7d66ebc3ff10d Mon Sep 17 00:00:00 2001 From: lafirest Date: Fri, 17 Sep 2021 14:22:02 +0800 Subject: [PATCH] fix(schema): fix some time unit in schema --- apps/emqx/src/emqx_schema.erl | 8 +++++--- .../src/mqtt/emqx_connector_mqtt_schema.erl | 2 +- apps/emqx_gateway/src/coap/emqx_coap_api.erl | 6 +++--- apps/emqx_gateway/test/emqx_coap_api_SUITE.erl | 2 +- 4 files changed, 10 insertions(+), 8 deletions(-) diff --git a/apps/emqx/src/emqx_schema.erl b/apps/emqx/src/emqx_schema.erl index 27688a868..a4cea319c 100644 --- a/apps/emqx/src/emqx_schema.erl +++ b/apps/emqx/src/emqx_schema.erl @@ -1191,7 +1191,7 @@ default_ciphers(psk) -> keys(Parent, Conf) -> [binary_to_list(B) || B <- maps:keys(conf_get(Parent, Conf, #{}))]. --spec ceiling(float()) -> integer(). +-spec ceiling(number()) -> integer(). ceiling(X) -> T = erlang:trunc(X), case (X - T) of @@ -1218,13 +1218,15 @@ to_duration(Str) -> to_duration_s(Str) -> case hocon_postprocess:duration(Str) of - I when is_integer(I) -> {ok, ceiling(I / 1000)}; + I when is_number(I) -> {ok, ceiling(I / 1000)}; _ -> {error, Str} end. +-spec to_duration_ms(Input) -> {ok, integer()} | {error, Input} + when Input :: string() | binary(). to_duration_ms(Str) -> case hocon_postprocess:duration(Str) of - I when is_integer(I) -> {ok, ceiling(I)}; + I when is_number(I) -> {ok, ceiling(I)}; _ -> {error, Str} end. diff --git a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl index a00b76b97..14e550f0c 100644 --- a/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl +++ b/apps/emqx_connector/src/mqtt/emqx_connector_mqtt_schema.erl @@ -34,7 +34,7 @@ fields("config") -> , {username, hoconsc:mk(string())} , {password, hoconsc:mk(string())} , {clean_start, hoconsc:mk(boolean(), #{default => true})} - , {keepalive, hoconsc:mk(integer(), #{default => 300})} + , {keepalive, hoconsc:mk(emqx_schema:duration_s(), #{default => "300s"})} , {retry_interval, hoconsc:mk(emqx_schema:duration_ms(), #{default => "30s"})} , {max_inflight, hoconsc:mk(integer(), #{default => 32})} , {replayq, hoconsc:mk(hoconsc:ref(?MODULE, "replayq"))} diff --git a/apps/emqx_gateway/src/coap/emqx_coap_api.erl b/apps/emqx_gateway/src/coap/emqx_coap_api.erl index 4d0e8aff8..3eed9b802 100644 --- a/apps/emqx_gateway/src/coap/emqx_coap_api.erl +++ b/apps/emqx_gateway/src/coap/emqx_coap_api.erl @@ -52,8 +52,8 @@ request(post, #{body := Body, bindings := Bindings}) -> CT = maps:get(<<"content_type">>, Body, <<"text/plain">>), Token = maps:get(<<"token">>, Body, <<>>), Payload = maps:get(<<"payload">>, Body, <<>>), - WaitTime = maps:get(<<"timeout">>, Body, ?DEF_WAIT_TIME), - + BinWaitTime = maps:get(<<"timeout">>, Body, <<"10s">>), + {ok, WaitTime} = emqx_schema:to_duration_ms(BinWaitTime), Payload2 = parse_payload(CT, Payload), ReqType = erlang:binary_to_atom(Method), @@ -83,7 +83,7 @@ request_parameters() -> request_properties() -> properties([ {token, string, "message token, can be empty"} , {method, string, "request method type", ["get", "put", "post", "delete"]} - , {timeout, integer, "timespan for response"} + , {timeout, string, "timespan for response", "10s"} , {content_type, string, "payload type", [<<"text/plain">>, <<"application/json">>, <<"application/octet-stream">>]} , {payload, string, "payload"}]). diff --git a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl index 83521f5cd..a2d57145f 100644 --- a/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl +++ b/apps/emqx_gateway/test/emqx_coap_api_SUITE.erl @@ -73,7 +73,7 @@ t_send_request_api(_) -> Payload = <<"simple echo this">>, Req = #{token => Token, payload => Payload, - timeout => 10, + timeout => <<"10s">>, content_type => <<"text/plain">>, method => <<"get">>}, Auth = emqx_mgmt_api_test_util:auth_header_(),