diff --git a/test/router_ics_gateway_location_worker_SUITE.erl b/test/router_ics_gateway_location_worker_SUITE.erl index 08a3a5c60..431ae1d12 100644 --- a/test/router_ics_gateway_location_worker_SUITE.erl +++ b/test/router_ics_gateway_location_worker_SUITE.erl @@ -16,6 +16,12 @@ main_test/1 ]). +-record(location, { + gateway :: libp2p_crypto:pubkey_bin(), + timestamp :: non_neg_integer(), + h3_index :: h3:index() +}). + %%-------------------------------------------------------------------- %% COMMON TEST CALLBACK FUNCTIONS %%-------------------------------------------------------------------- @@ -70,18 +76,33 @@ end_per_testcase(TestCase, Config) -> %%-------------------------------------------------------------------- main_test(_Config) -> - timer:sleep(5000), + #{public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), + PubKeyBin1 = libp2p_crypto:pubkey_to_bin(PubKey1), + ExpectedIndex = h3:from_string("8828308281fffff"), + + Before = erlang:system_time(millisecond), + %% Let worker start test_utils:wait_until(fun() -> - erlang:is_pid(erlang:whereis(router_ics_gateway_location_worker)) + try router_ics_gateway_location_worker:get(PubKeyBin1) of + {ok, ExpectedIndex} -> true; + _ -> false + catch + _:_ -> + false + end end), - #{public := PubKey1} = libp2p_crypto:generate_keys(ecc_compact), - PubKeyBin1 = libp2p_crypto:pubkey_to_bin(PubKey1), + [LocationRec] = ets:lookup(router_ics_gateway_location_worker_ets, PubKeyBin1), - ?assertEqual( - {ok, h3:from_string("8828308281fffff")}, router_ics_gateway_location_worker:get(PubKeyBin1) - ), + ?assertEqual(PubKeyBin1, LocationRec#location.gateway), + ?assertEqual(ExpectedIndex, LocationRec#location.h3_index), + + Timestamp = LocationRec#location.timestamp, + Now = erlang:system_time(millisecond), + + ?assert(Timestamp > Before), + ?assert(Timestamp =< Now), [{location, Req1}] = rcv_loop([]), ?assertEqual(PubKeyBin1, Req1#iot_config_gateway_location_req_v1_pb.gateway),