|
| 1 | +// Copyright (C) 2023-2025 RabbitMQ Core Team ([email protected]) |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +mod test_helpers; |
| 16 | + |
| 17 | +use rabbitmq_http_client::responses::cluster::{ |
| 18 | + NodeMemoryBreakdown, NodeMemoryFootprint, NodeMemoryTotals, |
| 19 | +}; |
| 20 | +use serde_json::json; |
| 21 | + |
| 22 | +#[test] |
| 23 | +fn test_unit_node_memory_footprint_with_breakdown() { |
| 24 | + let json = json!({ |
| 25 | + "memory": { |
| 26 | + "connection_readers": 1000, |
| 27 | + "connection_writers": 2000, |
| 28 | + "connection_channels": 3000, |
| 29 | + "connection_other": 4000, |
| 30 | + "queue_procs": 5000, |
| 31 | + "quorum_queue_procs": 6000, |
| 32 | + "stream_queue_procs": 7000, |
| 33 | + "stream_queue_replica_reader_procs": 8000, |
| 34 | + "stream_queue_coordinator_procs": 9000, |
| 35 | + "plugins": 10000, |
| 36 | + "metadata_store": 11000, |
| 37 | + "other_proc": 12000, |
| 38 | + "metrics": 13000, |
| 39 | + "mgmt_db": 14000, |
| 40 | + "mnesia": 15000, |
| 41 | + "quorum_ets": 16000, |
| 42 | + "metadata_store_ets": 17000, |
| 43 | + "other_ets": 18000, |
| 44 | + "binary": 19000, |
| 45 | + "msg_index": 20000, |
| 46 | + "code": 21000, |
| 47 | + "atom": 22000, |
| 48 | + "other_system": 23000, |
| 49 | + "allocated_unused": 24000, |
| 50 | + "reserved_unallocated": 25000, |
| 51 | + "strategy": "allocated", |
| 52 | + "total": { |
| 53 | + "rss": 100000, |
| 54 | + "allocated": 200000, |
| 55 | + "erlang": 150000 |
| 56 | + } |
| 57 | + } |
| 58 | + }); |
| 59 | + |
| 60 | + let footprint: NodeMemoryFootprint = serde_json::from_value(json).unwrap(); |
| 61 | + |
| 62 | + assert!(footprint.breakdown.is_some()); |
| 63 | + let breakdown = footprint.breakdown.unwrap(); |
| 64 | + |
| 65 | + assert_eq!(breakdown.connection_readers, 1000); |
| 66 | + assert_eq!(breakdown.connection_writers, 2000); |
| 67 | + assert_eq!(breakdown.connection_channels, 3000); |
| 68 | + assert_eq!(breakdown.connection_other, 4000); |
| 69 | + assert_eq!(breakdown.classic_queue_procs, 5000); |
| 70 | + assert_eq!(breakdown.quorum_queue_procs, 6000); |
| 71 | + assert_eq!(breakdown.stream_queue_procs, 7000); |
| 72 | + assert_eq!(breakdown.stream_queue_replica_reader_procs, 8000); |
| 73 | + assert_eq!(breakdown.stream_queue_coordinator_procs, 9000); |
| 74 | + assert_eq!(breakdown.plugins, 10000); |
| 75 | + assert_eq!(breakdown.metadata_store, 11000); |
| 76 | + assert_eq!(breakdown.other_procs, 12000); |
| 77 | + assert_eq!(breakdown.metrics, 13000); |
| 78 | + assert_eq!(breakdown.management_db, 14000); |
| 79 | + assert_eq!(breakdown.mnesia, 15000); |
| 80 | + assert_eq!(breakdown.quorum_queue_ets_tables, 16000); |
| 81 | + assert_eq!(breakdown.metadata_store_ets_tables, 17000); |
| 82 | + assert_eq!(breakdown.other_ets_tables, 18000); |
| 83 | + assert_eq!(breakdown.binary_heap, 19000); |
| 84 | + assert_eq!(breakdown.message_indices, 20000); |
| 85 | + assert_eq!(breakdown.code, 21000); |
| 86 | + assert_eq!(breakdown.atom_table, 22000); |
| 87 | + assert_eq!(breakdown.other_system, 23000); |
| 88 | + assert_eq!(breakdown.allocated_but_unused, 24000); |
| 89 | + assert_eq!(breakdown.reserved_but_unallocated, 25000); |
| 90 | + assert_eq!(breakdown.calculation_strategy, "allocated"); |
| 91 | + |
| 92 | + assert_eq!(breakdown.total.rss, 100000); |
| 93 | + assert_eq!(breakdown.total.allocated, 200000); |
| 94 | + assert_eq!(breakdown.total.used_by_runtime, 150000); |
| 95 | +} |
| 96 | + |
| 97 | +#[test] |
| 98 | +fn test_unit_node_memory_footprint_not_available() { |
| 99 | + let json = json!({ |
| 100 | + "memory": "not_available" |
| 101 | + }); |
| 102 | + |
| 103 | + let footprint: NodeMemoryFootprint = serde_json::from_value(json).unwrap(); |
| 104 | + |
| 105 | + assert!(footprint.breakdown.is_none()); |
| 106 | +} |
| 107 | + |
| 108 | +#[test] |
| 109 | +fn test_unit_node_memory_footprint_invalid_string() { |
| 110 | + let json = json!({ |
| 111 | + "memory": "some_other_string" |
| 112 | + }); |
| 113 | + |
| 114 | + let result: Result<NodeMemoryFootprint, _> = serde_json::from_value(json); |
| 115 | + |
| 116 | + // Should fail to deserialize when the string is not "not_available" |
| 117 | + assert!(result.is_err()); |
| 118 | +} |
| 119 | + |
| 120 | +#[test] |
| 121 | +fn test_unit_node_memory_breakdown_grand_total() { |
| 122 | + let total = NodeMemoryTotals { |
| 123 | + rss: 100000, |
| 124 | + allocated: 200000, |
| 125 | + used_by_runtime: 150000, |
| 126 | + }; |
| 127 | + |
| 128 | + let breakdown = NodeMemoryBreakdown { |
| 129 | + connection_readers: 1000, |
| 130 | + connection_writers: 2000, |
| 131 | + connection_channels: 3000, |
| 132 | + connection_other: 4000, |
| 133 | + classic_queue_procs: 5000, |
| 134 | + quorum_queue_procs: 6000, |
| 135 | + stream_queue_procs: 7000, |
| 136 | + stream_queue_replica_reader_procs: 8000, |
| 137 | + stream_queue_coordinator_procs: 9000, |
| 138 | + plugins: 10000, |
| 139 | + metadata_store: 11000, |
| 140 | + other_procs: 12000, |
| 141 | + metrics: 13000, |
| 142 | + management_db: 14000, |
| 143 | + mnesia: 15000, |
| 144 | + quorum_queue_ets_tables: 16000, |
| 145 | + metadata_store_ets_tables: 17000, |
| 146 | + other_ets_tables: 18000, |
| 147 | + binary_heap: 19000, |
| 148 | + message_indices: 20000, |
| 149 | + code: 21000, |
| 150 | + atom_table: 22000, |
| 151 | + other_system: 23000, |
| 152 | + allocated_but_unused: 24000, |
| 153 | + reserved_but_unallocated: 25000, |
| 154 | + calculation_strategy: "allocated".to_string(), |
| 155 | + total, |
| 156 | + }; |
| 157 | + |
| 158 | + // grand_total should return the maximum of all totals |
| 159 | + assert_eq!(breakdown.grand_total(), 200000); |
| 160 | +} |
| 161 | + |
| 162 | +#[test] |
| 163 | +fn test_unit_node_memory_totals_max() { |
| 164 | + let totals = NodeMemoryTotals { |
| 165 | + rss: 100000, |
| 166 | + allocated: 200000, |
| 167 | + used_by_runtime: 150000, |
| 168 | + }; |
| 169 | + |
| 170 | + // max should return the maximum value |
| 171 | + assert_eq!(totals.max(), 200000); |
| 172 | + |
| 173 | + let totals2 = NodeMemoryTotals { |
| 174 | + rss: 300000, |
| 175 | + allocated: 200000, |
| 176 | + used_by_runtime: 150000, |
| 177 | + }; |
| 178 | + |
| 179 | + assert_eq!(totals2.max(), 300000); |
| 180 | +} |
0 commit comments