MCPcopy
hub / github.com/redis/go-redis / TestDigestBytesHelperMatchesRedis

Function TestDigestBytesHelperMatchesRedis

digest_test.go:328–380  ·  view source on GitHub ↗

TestDigestBytesHelperMatchesRedis validates that helper.DigestBytes produces the same digest as Redis DIGEST command for binary data

(t *testing.T)

Source from the content-addressed store, hash-verified

326// TestDigestBytesHelperMatchesRedis validates that helper.DigestBytes produces
327// the same digest as Redis DIGEST command for binary data
328func TestDigestBytesHelperMatchesRedis(t *testing.T) {
329 skipIfRedisBelow84(t)
330
331 ctx := context.Background()
332 client := redis.NewClient(&redis.Options{
333 Addr: "localhost:6379",
334 })
335 defer client.Close()
336
337 if err := client.Ping(ctx).Err(); err != nil {
338 t.Skipf("Redis not available: %v", err)
339 }
340
341 testCases := []struct {
342 name string
343 value []byte
344 }{
345 {"simple_bytes", []byte("hello world")},
346 {"empty_bytes", []byte{}},
347 {"binary_data", []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD}},
348 {"jpeg_header", []byte{0xFF, 0xD8, 0xFF, 0xE0}},
349 {"null_bytes", []byte{0x00, 0x00, 0x00, 0x00}},
350 }
351
352 for _, tc := range testCases {
353 t.Run(tc.name, func(t *testing.T) {
354 key := "helper-bytes-test-" + tc.name
355
356 // Set value in Redis
357 err := client.Set(ctx, key, tc.value, 0).Err()
358 if err != nil {
359 t.Fatalf("Failed to set value: %v", err)
360 }
361
362 // Get digest from Redis
363 redisDigest := client.Digest(ctx, key).Val()
364
365 // Calculate digest using helper
366 helperDigest := helper.DigestBytes(tc.value)
367
368 // Compare
369 if redisDigest != helperDigest {
370 t.Errorf("Digest mismatch for binary data %v:\n Redis: 0x%016x\n Helper: 0x%016x",
371 tc.value, redisDigest, helperDigest)
372 } else {
373 t.Logf("✓ %s: Redis and helper digests match (0x%016x)", tc.name, redisDigest)
374 }
375
376 // Cleanup
377 client.Del(ctx, key)
378 })
379 }
380}
381
382// TestDigestHelperWithSetIFDEQ validates end-to-end optimistic locking using
383// client-side digest calculation

Callers

nothing calls this directly

Calls 10

DigestBytesFunction · 0.92
skipIfRedisBelow84Function · 0.85
CloseMethod · 0.65
ErrMethod · 0.65
PingMethod · 0.65
SetMethod · 0.65
DigestMethod · 0.65
DelMethod · 0.65
RunMethod · 0.45
ValMethod · 0.45

Tested by

no test coverage detected