| 134 | } |
| 135 | result := truncateQuery(string(long)) |
| 136 | if len(result) != maxQueryLength { |
| 137 | t.Errorf("Expected truncated length %d, got %d", maxQueryLength, len(result)) |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | func TestBoundQueryLogTextClonesAtValidUTF8Boundary(t *testing.T) { |
| 142 | long := strings.Repeat("x", maxQueryLength-1) + "é-tail" |
| 143 | result := boundQueryLogText(long) |
| 144 | expected := strings.Repeat("x", maxQueryLength-1) |
| 145 | if result != expected { |
| 146 | t.Fatalf("bounded query log text = %q, want maximal valid prefix of length %d", result, len(expected)) |
| 147 | } |
| 148 | if len(result) > maxQueryLength { |
| 149 | t.Fatalf("bounded length = %d, want <= %d", len(result), maxQueryLength) |
| 150 | } |
| 151 | if !utf8.ValidString(result) { |
| 152 | t.Fatalf("bounded query log text is not valid UTF-8: %q", result[len(result)-4:]) |
| 153 | } |
| 154 | if unsafe.StringData(result) == unsafe.StringData(long) { |
| 155 | t.Fatal("bounded query log text still aliases the oversized backing string") |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | func TestQueryLogNonBlocking(t *testing.T) { |
| 160 | // Create a logger with a tiny channel to test non-blocking behavior |
| 161 | ql := &QueryLogger{ |
| 162 | ch: make(chan QueryLogEntry, 1), |
| 163 | cfg: QueryLogConfig{BatchSize: 1000}, |