-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathtest_helper.rb
112 lines (88 loc) · 2.05 KB
/
test_helper.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# frozen_string_literal: true
$LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
require "prometheus_exporter"
require "minitest/autorun"
require "ostruct"
require "redis"
module TestingMod
class FakeConnection
def call_pipelined(_, _, _)
end
def call(_, _)
end
def connected?
true
end
def revalidate
end
def read_timeout=(v)
end
def write_timeout=(v)
end
end
def connect(_config)
FakeConnection.new
end
end
module RedisValidationMiddleware
def self.reset!
@@call_calls = 0
@@call_pipelined_calls = 0
end
def self.call_calls
@@call_calls || 0
end
def self.call_pipelined_calls
@@call_pipelined_calls || 0
end
def call(command, _config)
@@call_calls ||= 0
@@call_calls += 1
super
end
def call_pipelined(command, _config)
@@call_pipelined_calls ||= 0
@@call_pipelined_calls += 1
super
end
end
RedisClient::Middlewares.prepend(TestingMod)
RedisClient.register(RedisValidationMiddleware)
class TestHelper
def self.wait_for(time, &blk)
(time / 0.001).to_i.times do
return true if blk.call
sleep 0.001
end
false
end
end
module ClockHelper
def stub_monotonic_clock(at = 0.0, advance: nil, &blk)
Process.stub(:clock_gettime, at + advance.to_f, Process::CLOCK_MONOTONIC, &blk)
end
end
module CollectorHelper
def setup
PrometheusExporter::Metric::Base.default_prefix = ""
end
def max_metric_age
@_max_age ||= get_max_metric_age
end
def collector_metric_lines
collector.metrics.map(&:metric_text).join("\n").split("\n")
end
def assert_collector_metric_lines(expected)
assert_equal(expected, collector_metric_lines)
end
private
def get_max_metric_age
klass = @collector.class
unless klass.const_defined?(:MAX_METRIC_AGE)
raise "Collector class #{@collector.class.name} must set MAX_METRIC_AGE constant!"
end
klass.const_get(:MAX_METRIC_AGE)
end
end
# Allow stubbing process monotonic clock from any class in the suite
Minitest::Test.send(:include, ClockHelper)