Browse Source

test(license): fix test cases after changed to cache local conn count

zmstone 1 year ago
parent
commit
7e4e2f7099

+ 28 - 13
apps/emqx_license/test/emqx_license_SUITE.erl

@@ -74,20 +74,22 @@ t_check_exceeded(_Config) ->
             "10"
         ]
     ),
-    #{} = emqx_license_checker:update(License),
+    #{} = update(License),
 
-    ok = lists:foreach(
+    Pids = lists:map(
         fun(_) ->
             {ok, C} = emqtt:start_link(),
-            {ok, _} = emqtt:connect(C)
+            {ok, _} = emqtt:connect(C),
+            C
         end,
         lists:seq(1, 12)
     ),
-
+    sync_cache(),
     ?assertEqual(
         {stop, {error, ?RC_QUOTA_EXCEEDED}},
         emqx_license:check(#{}, #{})
-    ).
+    ),
+    ok = lists:foreach(fun(Pid) -> emqtt:stop(Pid) end, Pids).
 
 t_check_ok(_Config) ->
     {_, License} = mk_license(
@@ -103,20 +105,21 @@ t_check_ok(_Config) ->
             "10"
         ]
     ),
-    #{} = emqx_license_checker:update(License),
+    #{} = update(License),
 
-    ok = lists:foreach(
-        fun(_) ->
-            {ok, C} = emqtt:start_link(),
-            {ok, _} = emqtt:connect(C)
+    Pids = lists:map(
+        fun(I) ->
+            {ok, C} = emqtt:start_link([{proto_ver, v5}]),
+            ?assertMatch({I, {ok, _}}, {I, emqtt:connect(C)}),
+            C
         end,
         lists:seq(1, 11)
     ),
-
     ?assertEqual(
         {ok, #{}},
         emqx_license:check(#{}, #{})
-    ).
+    ),
+    ok = lists:foreach(fun(Pid) -> emqtt:stop(Pid) end, Pids).
 
 t_check_expired(_Config) ->
     {_, License} = mk_license(
@@ -135,7 +138,7 @@ t_check_expired(_Config) ->
             "10"
         ]
     ),
-    #{} = emqx_license_checker:update(License),
+    #{} = update(License),
 
     ?assertEqual(
         {stop, {error, ?RC_QUOTA_EXCEEDED}},
@@ -190,3 +193,15 @@ mk_license(Fields) ->
         emqx_license_test_lib:public_key_pem()
     ),
     {EncodedLicense, License}.
+
+update(License) ->
+    Result = emqx_license_checker:update(License),
+    sync_cache(),
+    Result.
+
+sync_cache() ->
+    %% force refresh the cache
+    _ = whereis(emqx_license_resources) ! update_resources,
+    %% force sync with the process
+    _ = sys:get_state(whereis(emqx_license_resources)),
+    ok.

+ 2 - 2
apps/emqx_license/test/emqx_license_resources_SUITE.erl

@@ -57,8 +57,8 @@ t_connection_count(_Config) ->
     meck:expect(
         emqx_license_proto_v2,
         remote_connection_counts,
-        fun(_Nodes) ->
-            [{ok, 5}, {error, some_error}]
+        fun(Nodes) ->
+            [{ok, 5}, {error, some_error}] ++ meck:passthrough([Nodes])
         end
     ),