MCPcopy
hub / github.com/grafana/tempo / TestTokenForID

Function TestTokenForID

pkg/util/traceid_test.go:204–240  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

202}
203
204func TestTokenForID(t *testing.T) {
205 h := NewTokenHasher()
206 buf := make([]byte, 4)
207
208 t.Run("unique token per kind", func(t *testing.T) {
209 spanIDs := [][]byte{
210 {0x60, 0xd8, 0xa9, 0xbd},
211 {0x8e, 0xf6, 0x37, 0x90, 0x22, 0x57, 0xb7, 0x43},
212 {0x18, 0xcc, 0xd9, 0x6d, 0x70, 0xc1, 0xbd, 0xf9},
213 }
214 for _, spanID := range spanIDs {
215 seen := map[uint64]struct{}{}
216 for kind := int32(0); kind < 8; kind++ {
217 tok := TokenForID(h, buf, kind, spanID)
218 _, exists := seen[tok]
219 assert.False(t, exists, "token must be unique per span kind")
220 seen[tok] = struct{}{}
221 }
222 }
223 })
224
225 t.Run("all bytes contribute", func(t *testing.T) {
226 // IDs differing only beyond byte 8 must not collide.
227 base := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08}
228 longer := []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0xff}
229 assert.NotEqual(t, TokenForID(h, buf, 0, base), TokenForID(h, buf, 0, longer))
230 })
231
232 t.Run("hasher reuse", func(t *testing.T) {
233 // Reusing h and buf across calls must give stable results.
234 spanID := []byte{0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff, 0x11, 0x22}
235 tok := TokenForID(h, buf, 0, spanID)
236 for i := 0; i < 10; i++ {
237 assert.Equal(t, tok, TokenForID(h, buf, 0, spanID))
238 }
239 })
240}
241
242func BenchmarkTokenForID(b *testing.B) {
243 h := NewTokenHasher()

Callers

nothing calls this directly

Calls 4

NewTokenHasherFunction · 0.85
TokenForIDFunction · 0.85
RunMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected