Verify HashForTraceID doesn't collide within reasonable numbers, and estimate the hash collision rate if it does.
(t *testing.T)
| 32 | |
| 33 | // Verify HashForTraceID doesn't collide within reasonable numbers, and estimate the hash collision rate if it does. |
| 34 | func TestHashForCollisionRate(t *testing.T) { |
| 35 | var ( |
| 36 | n = 1_000_000 |
| 37 | tokens = map[uint64]struct{}{} |
| 38 | IDs = make([][]byte, 0, n) |
| 39 | ) |
| 40 | |
| 41 | for i := 0; i < n; i++ { |
| 42 | traceID := make([]byte, 16) |
| 43 | _, err := rand.Read(traceID) |
| 44 | require.NoError(t, err) |
| 45 | |
| 46 | IDs = append(IDs, traceID) |
| 47 | tokens[HashForTraceID(traceID)] = struct{}{} |
| 48 | } |
| 49 | |
| 50 | // Ensure no duplicate span IDs accidentally generated |
| 51 | sort.Slice(IDs, func(i, j int) bool { |
| 52 | return bytes.Compare(IDs[i], IDs[j]) == -1 |
| 53 | }) |
| 54 | for i := 1; i < len(IDs); i++ { |
| 55 | if bytes.Equal(IDs[i-1], IDs[i]) { |
| 56 | panic("same trace ID was generated, oops") |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | missing := n - len(tokens) |
| 61 | require.Zerof(t, missing, "missing 1 out of every %.2f trace ids", float32(n)/float32(missing)) |
| 62 | } |
nothing calls this directly
no test coverage detected