|
| 1 | +/* |
| 2 | + * Copyright 2025 Splunk Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.splunk.rum.integration.agent.common.attributes |
| 18 | + |
| 19 | +import com.cisco.android.common.utils.extensions.forEachFast |
| 20 | +import io.opentelemetry.api.common.AttributeKey |
| 21 | +import io.opentelemetry.api.common.Attributes |
| 22 | +import io.opentelemetry.api.common.AttributesBuilder |
| 23 | +import java.util.concurrent.CountDownLatch |
| 24 | +import java.util.concurrent.Executors |
| 25 | +import org.junit.Assert |
| 26 | +import org.junit.Test |
| 27 | + |
| 28 | +class MutableAttributesTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + fun `get and set with String key`() { |
| 32 | + val mutableAttributes = MutableAttributes() |
| 33 | + |
| 34 | + mutableAttributes["key"] = "value" |
| 35 | + Assert.assertEquals("value", mutableAttributes["key"]) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `modify value with String key`() { |
| 40 | + val mutableAttributes = MutableAttributes() |
| 41 | + |
| 42 | + mutableAttributes["key"] = "value" |
| 43 | + Assert.assertEquals("value", mutableAttributes["key"]) |
| 44 | + |
| 45 | + mutableAttributes["key"] = "value1" |
| 46 | + Assert.assertEquals("value1", mutableAttributes["key"]) |
| 47 | + |
| 48 | + mutableAttributes["key"] = 0L |
| 49 | + Assert.assertEquals(0L, mutableAttributes["key"]) |
| 50 | + } |
| 51 | + |
| 52 | + @Test |
| 53 | + fun `get and set with AttributeKey`() { |
| 54 | + val mutableAttributes = MutableAttributes() |
| 55 | + |
| 56 | + mutableAttributes[AttributeKey.stringKey("key")] = "value" |
| 57 | + Assert.assertEquals("value", mutableAttributes[AttributeKey.stringKey("key")]) |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + fun `get non-existent key returns null`() { |
| 62 | + val mutableAttributes = MutableAttributes() |
| 63 | + Assert.assertNull(mutableAttributes["key"]) |
| 64 | + Assert.assertNull(mutableAttributes[AttributeKey.stringKey("key")]) |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + fun `set multiple types`() { |
| 69 | + val mutableAttributes = MutableAttributes() |
| 70 | + |
| 71 | + mutableAttributes["stringKey"] = "hello" |
| 72 | + mutableAttributes["longKey"] = 123L |
| 73 | + mutableAttributes["doubleKey"] = 45.67 |
| 74 | + mutableAttributes["booleanKey"] = true |
| 75 | + |
| 76 | + Assert.assertEquals("hello", mutableAttributes["stringKey"]) |
| 77 | + Assert.assertEquals(123L, mutableAttributes["longKey"]) |
| 78 | + Assert.assertEquals(45.67, mutableAttributes["doubleKey"]) |
| 79 | + Assert.assertEquals(true, mutableAttributes["booleanKey"]) |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + fun `remove with String key`() { |
| 84 | + val mutableAttributes = MutableAttributes() |
| 85 | + |
| 86 | + mutableAttributes["key"] = "value" |
| 87 | + Assert.assertEquals("value", mutableAttributes["key"]) |
| 88 | + |
| 89 | + mutableAttributes.remove("key") |
| 90 | + Assert.assertNull(mutableAttributes["key"]) |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + fun `remove with AttributeKey`() { |
| 95 | + val mutableAttributes = MutableAttributes() |
| 96 | + |
| 97 | + mutableAttributes[AttributeKey.longKey("keyToRemove")] = 100L |
| 98 | + Assert.assertEquals(100L, mutableAttributes[AttributeKey.longKey("keyToRemove")]) |
| 99 | + |
| 100 | + mutableAttributes.remove(AttributeKey.longKey("keyToRemove")) |
| 101 | + Assert.assertNull(mutableAttributes[AttributeKey.longKey("keyToRemove")]) |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + fun `removeAll clears all attributes`() { |
| 106 | + val mutableAttributes = MutableAttributes() |
| 107 | + |
| 108 | + mutableAttributes["key1"] = "value1" |
| 109 | + mutableAttributes["key2"] = 123L |
| 110 | + Assert.assertEquals(2, mutableAttributes.size()) |
| 111 | + |
| 112 | + mutableAttributes.removeAll() |
| 113 | + |
| 114 | + Assert.assertTrue(mutableAttributes.isEmpty()) |
| 115 | + Assert.assertEquals(0, mutableAttributes.size()) |
| 116 | + Assert.assertNull(mutableAttributes["key1"]) |
| 117 | + Assert.assertNull(mutableAttributes["key2"]) |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + fun `setAll adds attributes from another Attributes instance`() { |
| 122 | + val mutableAttributes = MutableAttributes() |
| 123 | + mutableAttributes["existingKey"] = "existingValue" |
| 124 | + |
| 125 | + val attributesToAdd = Attributes.builder() |
| 126 | + .put("newKey1", "newValue1") |
| 127 | + .put(AttributeKey.longKey("newKey2"), 99L) |
| 128 | + .build() |
| 129 | + |
| 130 | + mutableAttributes.setAll(attributesToAdd) |
| 131 | + |
| 132 | + Assert.assertEquals("existingValue", mutableAttributes["existingKey"]) |
| 133 | + Assert.assertEquals("newValue1", mutableAttributes["newKey1"]) |
| 134 | + Assert.assertEquals(99L, mutableAttributes[AttributeKey.longKey("newKey2")]) |
| 135 | + Assert.assertEquals(3, mutableAttributes.size()) |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun `getAll returns current attributes`() { |
| 140 | + val mutableAttributes = MutableAttributes() |
| 141 | + |
| 142 | + mutableAttributes[AttributeKey.stringKey("key1")] = "value1" |
| 143 | + mutableAttributes["key2"] = true |
| 144 | + |
| 145 | + val allAttributes = mutableAttributes.getAll() |
| 146 | + Assert.assertEquals(2, allAttributes.size()) |
| 147 | + Assert.assertEquals("value1", allAttributes[AttributeKey.stringKey("key1")]) |
| 148 | + Assert.assertEquals(true, allAttributes[AttributeKey.booleanKey("key2")]) |
| 149 | + } |
| 150 | + |
| 151 | + @Test |
| 152 | + fun `update modifies attributes`() { |
| 153 | + val mutableAttributes = MutableAttributes() |
| 154 | + mutableAttributes["initialKey"] = "initialValue" |
| 155 | + |
| 156 | + mutableAttributes.update { |
| 157 | + put("updatedKey", "updatedValue") |
| 158 | + remove(AttributeKey.stringKey("initialKey")) |
| 159 | + put("anotherKey", 12345L) |
| 160 | + } |
| 161 | + |
| 162 | + Assert.assertNull(mutableAttributes["initialKey"]) |
| 163 | + Assert.assertEquals("updatedValue", mutableAttributes["updatedKey"]) |
| 164 | + Assert.assertEquals(12345L, mutableAttributes["anotherKey"]) |
| 165 | + Assert.assertEquals(2, mutableAttributes.size()) |
| 166 | + } |
| 167 | + |
| 168 | + @Test |
| 169 | + fun `forEach iterates over attributes`() { |
| 170 | + val mutableAttributes = MutableAttributes() |
| 171 | + |
| 172 | + mutableAttributes[AttributeKey.stringKey("name")] = "test" |
| 173 | + mutableAttributes[AttributeKey.longKey("count")] = 10L |
| 174 | + |
| 175 | + val collectedAttributes = mutableMapOf<AttributeKey<*>, Any>() |
| 176 | + mutableAttributes.forEach { key, value -> collectedAttributes[key] = value } |
| 177 | + |
| 178 | + Assert.assertEquals(2, collectedAttributes.size) |
| 179 | + Assert.assertEquals("test", collectedAttributes[AttributeKey.stringKey("name")]) |
| 180 | + Assert.assertEquals(10L, collectedAttributes[AttributeKey.longKey("count")]) |
| 181 | + } |
| 182 | + |
| 183 | + @Test |
| 184 | + fun `size returns correct number of attributes`() { |
| 185 | + val mutableAttributes = MutableAttributes() |
| 186 | + Assert.assertEquals(0, mutableAttributes.size()) |
| 187 | + mutableAttributes["a"] = "1" |
| 188 | + Assert.assertEquals(1, mutableAttributes.size()) |
| 189 | + mutableAttributes["b"] = "2" |
| 190 | + Assert.assertEquals(2, mutableAttributes.size()) |
| 191 | + mutableAttributes.remove("a") |
| 192 | + Assert.assertEquals(1, mutableAttributes.size()) |
| 193 | + } |
| 194 | + |
| 195 | + @Test |
| 196 | + fun `isEmpty checks if attributes are empty`() { |
| 197 | + val mutableAttributes = MutableAttributes() |
| 198 | + Assert.assertTrue(mutableAttributes.isEmpty()) |
| 199 | + mutableAttributes["a"] = "1" |
| 200 | + Assert.assertTrue(!mutableAttributes.isEmpty()) |
| 201 | + mutableAttributes.removeAll() |
| 202 | + Assert.assertTrue(mutableAttributes.isEmpty()) |
| 203 | + } |
| 204 | + |
| 205 | + @Test |
| 206 | + fun `asMap returns a map representation`() { |
| 207 | + val mutableAttributes = MutableAttributes() |
| 208 | + |
| 209 | + mutableAttributes[AttributeKey.stringKey("city")] = "Prague" |
| 210 | + mutableAttributes[AttributeKey.booleanKey("isActive")] = true |
| 211 | + |
| 212 | + val map = mutableAttributes.asMap() |
| 213 | + Assert.assertEquals(2, map.size) |
| 214 | + Assert.assertEquals("Prague", map[AttributeKey.stringKey("city")]) |
| 215 | + Assert.assertEquals(true, map[AttributeKey.booleanKey("isActive")]) |
| 216 | + } |
| 217 | + |
| 218 | + @Test |
| 219 | + fun `toBuilder creates a builder with current attributes`() { |
| 220 | + val mutableAttributes = MutableAttributes() |
| 221 | + mutableAttributes["name"] = "testBuilder" |
| 222 | + mutableAttributes["version"] = 1.0 |
| 223 | + |
| 224 | + val builder = mutableAttributes.toBuilder() |
| 225 | + val newAttributes = builder.put("extra", true).build() |
| 226 | + |
| 227 | + Assert.assertEquals("testBuilder", newAttributes[AttributeKey.stringKey("name")]) |
| 228 | + Assert.assertEquals(1.0, newAttributes[AttributeKey.doubleKey("version")]) |
| 229 | + Assert.assertEquals(true, newAttributes[AttributeKey.booleanKey("extra")]) |
| 230 | + Assert.assertNull(mutableAttributes["extra"]) |
| 231 | + Assert.assertEquals(2, mutableAttributes.size()) |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + fun `thread safety test - concurrent writes and reads`() { |
| 236 | + val mutableAttributes = MutableAttributes() |
| 237 | + |
| 238 | + val numberOfThreads = 10 |
| 239 | + val operationsPerThread = 100 |
| 240 | + val executor = Executors.newFixedThreadPool(numberOfThreads) |
| 241 | + val latch = CountDownLatch(numberOfThreads) |
| 242 | + |
| 243 | + val createKey = { threadIndex: Int, keyIndex: Int -> "thread_${threadIndex}_key_$keyIndex" } |
| 244 | + |
| 245 | + for (i in 0 until numberOfThreads) { |
| 246 | + executor.submit { |
| 247 | + try { |
| 248 | + for (j in 0 until operationsPerThread) { |
| 249 | + mutableAttributes[createKey(i, j)] = "value_$j" |
| 250 | + } |
| 251 | + |
| 252 | + for (j in 0 until operationsPerThread) { |
| 253 | + Assert.assertEquals("value_$j", mutableAttributes[createKey(i, j)]) |
| 254 | + } |
| 255 | + |
| 256 | + for (j in 0 until operationsPerThread) { |
| 257 | + mutableAttributes.remove("thread_${i}_key_$j") |
| 258 | + } |
| 259 | + } finally { |
| 260 | + latch.countDown() |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + latch.await() |
| 266 | + executor.shutdown() |
| 267 | + |
| 268 | + Assert.assertEquals(0, mutableAttributes.size()) |
| 269 | + } |
| 270 | + |
| 271 | + @Test |
| 272 | + fun `thread safety test - concurrent setAll and removeAll`() { |
| 273 | + val mutableAttributes = MutableAttributes() |
| 274 | + val numberOfThreads = 100 |
| 275 | + val executor = Executors.newFixedThreadPool(numberOfThreads) |
| 276 | + val latch = CountDownLatch(numberOfThreads) |
| 277 | + |
| 278 | + val attributesList = ArrayList<Attributes>(numberOfThreads) |
| 279 | + |
| 280 | + for (i in 0 until numberOfThreads) { |
| 281 | + executor.submit { |
| 282 | + try { |
| 283 | + val attributes = Attributes.builder() |
| 284 | + .put("key_${i}_0", "value") |
| 285 | + .put("key_${i}_1", 123) |
| 286 | + .put("key_${i}_2", true) |
| 287 | + .put("key_${i}_3", 123.45) |
| 288 | + .build() |
| 289 | + |
| 290 | + mutableAttributes.setAll(attributes) |
| 291 | + |
| 292 | + if (i % 10 == 0) { |
| 293 | + mutableAttributes.removeAll() |
| 294 | + synchronized(attributesList) { attributesList.clear() } |
| 295 | + } else { |
| 296 | + synchronized(attributesList) { attributesList += attributes } |
| 297 | + } |
| 298 | + } finally { |
| 299 | + latch.countDown() |
| 300 | + } |
| 301 | + } |
| 302 | + } |
| 303 | + |
| 304 | + latch.await() |
| 305 | + executor.shutdown() |
| 306 | + |
| 307 | + val attributes = mutableAttributes.getAll() |
| 308 | + |
| 309 | + val allAttributes = Attributes.builder() |
| 310 | + .putAll(attributesList) |
| 311 | + .build() |
| 312 | + |
| 313 | + Assert.assertEquals(allAttributes, attributes) |
| 314 | + } |
| 315 | + |
| 316 | + private fun AttributesBuilder.putAll(list: Collection<Attributes>): AttributesBuilder { |
| 317 | + list.forEachFast { putAll(it) } |
| 318 | + return this |
| 319 | + } |
| 320 | +} |
0 commit comments