TestDigestHelperWithSetIFDEQ validates end-to-end optimistic locking using client-side digest calculation
(t *testing.T)
| 382 | // TestDigestHelperWithSetIFDEQ validates end-to-end optimistic locking using |
| 383 | // client-side digest calculation |
| 384 | func TestDigestHelperWithSetIFDEQ(t *testing.T) { |
| 385 | skipIfRedisBelow84(t) |
| 386 | |
| 387 | ctx := context.Background() |
| 388 | client := redis.NewClient(&redis.Options{ |
| 389 | Addr: "localhost:6379", |
| 390 | }) |
| 391 | defer client.Close() |
| 392 | |
| 393 | if err := client.Ping(ctx).Err(); err != nil { |
| 394 | t.Skipf("Redis not available: %v", err) |
| 395 | } |
| 396 | |
| 397 | key := "helper-setifdeq-test" |
| 398 | client.Del(ctx, key) |
| 399 | |
| 400 | initialValue := "version-1" |
| 401 | err := client.Set(ctx, key, initialValue, 0).Err() |
| 402 | if err != nil { |
| 403 | t.Fatalf("Failed to set initial value: %v", err) |
| 404 | } |
| 405 | |
| 406 | clientDigest := helper.DigestString(initialValue) |
| 407 | t.Logf("Client-calculated digest for %q: 0x%016x", initialValue, clientDigest) |
| 408 | |
| 409 | // Use client-side digest for SetIFDEQ |
| 410 | newValue := "version-2" |
| 411 | result := client.SetIFDEQ(ctx, key, newValue, clientDigest, 0) |
| 412 | if err := result.Err(); err != nil { |
| 413 | t.Errorf("SetIFDEQ with client-calculated digest failed: %v", err) |
| 414 | } else { |
| 415 | t.Logf("✓ SetIFDEQ with client-calculated digest succeeded") |
| 416 | } |
| 417 | |
| 418 | // Verify value was updated |
| 419 | val, err := client.Get(ctx, key).Result() |
| 420 | if err != nil { |
| 421 | t.Fatalf("Failed to get value: %v", err) |
| 422 | } |
| 423 | if val != newValue { |
| 424 | t.Errorf("Value not updated: got %q, want %q", val, newValue) |
| 425 | } |
| 426 | |
| 427 | // Now try with wrong client-calculated digest (should fail) |
| 428 | wrongDigest := helper.DigestString("wrong-value") |
| 429 | result = client.SetIFDEQ(ctx, key, "version-3", wrongDigest, 0) |
| 430 | if result.Err() != redis.Nil { |
| 431 | t.Errorf("SetIFDEQ with wrong client digest should fail, got: %v", result.Err()) |
| 432 | } else { |
| 433 | t.Logf("✓ SetIFDEQ with wrong client-calculated digest correctly failed") |
| 434 | } |
| 435 | |
| 436 | client.Del(ctx, key) |
| 437 | } |
| 438 | |
| 439 | // TestDigestHelperWithDelExArgs validates conditional delete using |
| 440 | // client-side digest calculation |
nothing calls this directly
no test coverage detected