You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@couchdb.apache.org by va...@apache.org on 2022/10/17 20:51:33 UTC

[couchdb] 03/03: Add a few more prometheus tests

This is an automated email from the ASF dual-hosted git repository.

vatamane pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/couchdb.git

commit f3950c52057f52fe4c5d8fa00946c5c1ea5e176e
Author: Nick Vatamaniuc <va...@gmail.com>
AuthorDate: Mon Oct 17 16:05:17 2022 -0400

    Add a few more prometheus tests
---
 .../src/couch_prometheus_server.erl                | 39 +++++++++++++++++++++-
 1 file changed, 38 insertions(+), 1 deletion(-)

diff --git a/src/couch_prometheus/src/couch_prometheus_server.erl b/src/couch_prometheus/src/couch_prometheus_server.erl
index fbca9eb66..d13d11941 100644
--- a/src/couch_prometheus/src/couch_prometheus_server.erl
+++ b/src/couch_prometheus/src/couch_prometheus_server.erl
@@ -160,7 +160,7 @@ get_message_queue_stats() ->
 message_queue_len(undefined) ->
     0;
 message_queue_len(Pid) when is_pid(Pid) ->
-    case process_info(Pid, message_queue_len) of
+    case erlang:process_info(Pid, message_queue_len) of
         {message_queue_len, N} ->
             N;
         _ ->
@@ -197,3 +197,40 @@ update_refresh_timer() ->
     drain_refresh_messages(),
     RefreshTime = 1000 * config:get_integer("couch_prometheus", "interval", ?REFRESH_INTERVAL),
     erlang:send_after(RefreshTime, self(), refresh).
+
+-ifdef(TEST).
+
+-include_lib("couch/include/couch_eunit.hrl").
+
+system_stats_test() ->
+    lists:foreach(
+        fun(Line) ->
+            ?assert(is_binary(Line)),
+            ?assert((starts_with(<<"couchdb_">>, Line) orelse starts_with(<<"# TYPE ">>, Line)))
+        end,
+        get_system_stats()
+    ).
+
+starts_with(Prefix, Line) when is_binary(Prefix), is_binary(Line) ->
+    binary:longest_common_prefix([Prefix, Line]) > 0.
+
+message_queue_len_test() ->
+    self() ! refresh,
+    ?assert(message_queue_len(self()) >= 1),
+    ?assertEqual(0, message_queue_len(undefined)),
+    {Pid, Ref} = spawn_monitor(fun() -> ok end),
+    receive
+        {'DOWN', Ref, process, Pid, _} ->
+            ok
+    end,
+    ?assertEqual(0, message_queue_len(Pid)).
+
+drain_refresh_messages_test() ->
+    self() ! refresh,
+    {messages, Mq0} = erlang:process_info(self(), messages),
+    ?assert(lists:member(refresh, Mq0)),
+    drain_refresh_messages(),
+    {messages, Mq1} = erlang:process_info(self(), messages),
+    ?assert(not lists:member(refresh, Mq1)).
+
+-endif.