|
29 | 29 | Async::Utilization::Observer.open(schema, shm_path, segment_size, offset) |
30 | 30 | end |
31 | 31 |
|
| 32 | + let(:registry) {Async::Utilization::Registry.new} |
| 33 | + |
32 | 34 | before do |
33 | 35 | File.open(shm_path, "w+b") do |file| |
34 | 36 | file.truncate(file_size) |
35 | 37 | end |
36 | | - |
37 | | - # Reset the registry to ensure clean state between tests |
38 | | - registry = Async::Utilization::Registry.instance |
39 | | - registry.instance_variable_set(:@values, Hash.new(0)) |
40 | | - registry.instance_variable_set(:@metrics, {}) |
41 | | - registry.instance_variable_set(:@observer, nil) |
42 | 38 | end |
43 | 39 |
|
44 | 40 | it "can create a metric from a field name" do |
45 | | - metric = Async::Utilization.metric(:total_requests) |
| 41 | + metric = registry.metric(:total_requests) |
46 | 42 |
|
47 | 43 | expect(metric).to be_a(Async::Utilization::Metric) |
48 | 44 | expect(metric.name).to be == :total_requests |
49 | 45 | end |
50 | 46 |
|
51 | 47 | it "can increment a metric" do |
52 | | - Async::Utilization.observer = observer |
53 | | - metric = Async::Utilization.metric(:total_requests) |
| 48 | + registry.observer = observer |
| 49 | + metric = registry.metric(:total_requests) |
54 | 50 |
|
55 | 51 | value = metric.increment |
56 | 52 | expect(value).to be == 1 |
|
62 | 58 | end |
63 | 59 |
|
64 | 60 | it "can decrement a metric" do |
65 | | - Async::Utilization.observer = observer |
66 | | - metric = Async::Utilization.metric(:total_requests) |
| 61 | + registry.observer = observer |
| 62 | + metric = registry.metric(:total_requests) |
67 | 63 |
|
68 | 64 | metric.increment |
69 | 65 | metric.increment |
|
74 | 70 | end |
75 | 71 |
|
76 | 72 | it "can set a metric value" do |
77 | | - Async::Utilization.observer = observer |
78 | | - metric = Async::Utilization.metric(:total_requests) |
| 73 | + registry.observer = observer |
| 74 | + metric = registry.metric(:total_requests) |
79 | 75 |
|
80 | 76 | metric.set(42) |
81 | 77 | expect(metric.value).to be == 42 |
|
85 | 81 | end |
86 | 82 |
|
87 | 83 | it "can increment with auto-decrement block" do |
88 | | - Async::Utilization.observer = observer |
89 | | - metric = Async::Utilization.metric(:active_requests) |
| 84 | + registry.observer = observer |
| 85 | + metric = registry.metric(:active_requests) |
90 | 86 |
|
91 | 87 | metric.increment do |
92 | 88 | expect(metric.value).to be == 1 |
|
96 | 92 | end |
97 | 93 |
|
98 | 94 | it "decrements even if block raises an error" do |
99 | | - Async::Utilization.observer = observer |
100 | | - metric = Async::Utilization.metric(:active_requests) |
| 95 | + registry.observer = observer |
| 96 | + metric = registry.metric(:active_requests) |
101 | 97 |
|
102 | 98 | begin |
103 | 99 | metric.increment do |
|
111 | 107 | end |
112 | 108 |
|
113 | 109 | it "writes directly to shared memory when observer is set" do |
114 | | - Async::Utilization.observer = observer |
115 | | - metric = Async::Utilization.metric(:total_requests) |
| 110 | + registry.observer = observer |
| 111 | + metric = registry.metric(:total_requests) |
116 | 112 |
|
117 | 113 | metric.set(42) |
118 | 114 |
|
|
122 | 118 | end |
123 | 119 |
|
124 | 120 | it "invalidates cache when observer changes" do |
125 | | - Async::Utilization.observer = observer |
126 | | - metric = Async::Utilization.metric(:total_requests) |
| 121 | + registry.observer = observer |
| 122 | + metric = registry.metric(:total_requests) |
127 | 123 |
|
128 | 124 | # Set a value - cache should be built |
129 | 125 | metric.set(10) |
|
139 | 135 | new_observer = Async::Utilization::Observer.open(new_schema, new_shm_path, segment_size, 0) |
140 | 136 |
|
141 | 137 | # Change observer - cache should be invalidated |
142 | | - Async::Utilization.observer = new_observer |
| 138 | + registry.observer = new_observer |
143 | 139 |
|
144 | 140 | # Set a new value - cache should be rebuilt |
145 | 141 | metric.set(20) |
|
151 | 147 | end |
152 | 148 |
|
153 | 149 | it "works without an observer" do |
154 | | - metric = Async::Utilization.metric(:total_requests) |
| 150 | + metric = registry.metric(:total_requests) |
155 | 151 |
|
156 | 152 | # Should work fine without observer (uses fallback path) |
157 | 153 | metric.increment |
|
161 | 157 | expect(metric.value).to be == 5 |
162 | 158 |
|
163 | 159 | # Set observer and verify it works with fast path |
164 | | - Async::Utilization.observer = observer |
| 160 | + registry.observer = observer |
165 | 161 | metric.set(10) |
166 | 162 | expect(metric.value).to be == 10 |
167 | 163 | end |
168 | 164 |
|
169 | 165 | it "returns the same metric instance for the same field" do |
170 | | - metric1 = Async::Utilization.metric(:total_requests) |
171 | | - metric2 = Async::Utilization.metric(:total_requests) |
| 166 | + metric1 = registry.metric(:total_requests) |
| 167 | + metric2 = registry.metric(:total_requests) |
172 | 168 |
|
173 | 169 | expect(metric1).to be == metric2 |
174 | 170 | end |
175 | 171 |
|
176 | 172 | it "falls back to observer.set when write_direct fails" do |
177 | | - Async::Utilization.observer = observer |
178 | | - metric = Async::Utilization.metric(:total_requests) |
| 173 | + registry.observer = observer |
| 174 | + metric = registry.metric(:total_requests) |
179 | 175 |
|
180 | 176 | # Force cache to be invalid by invalidating it |
181 | 177 | metric.invalidate |
|
190 | 186 | end |
191 | 187 |
|
192 | 188 | it "handles write errors gracefully" do |
193 | | - Async::Utilization.observer = observer |
194 | | - metric = Async::Utilization.metric(:total_requests) |
| 189 | + registry.observer = observer |
| 190 | + metric = registry.metric(:total_requests) |
195 | 191 |
|
196 | 192 | # Set a value first to build the cache |
197 | 193 | metric.set(10) |
|
0 commit comments