@@ -768,6 +768,15 @@ func TestDefaultBackendOptions(t *testing.T) {
768768 assert .Equal (t , 5 * time .Second , opts .DialTimeout )
769769}
770770
771+ func TestNewRedisBackend_UsesRESP2 (t * testing.T ) {
772+ backend := NewRedisBackend ("127.0.0.1:6379" , "test" )
773+ t .Cleanup (func () {
774+ assert .NoError (t , backend .Close ())
775+ })
776+
777+ assert .Equal (t , respProtocolV2 , backend .client .Options ().Protocol )
778+ }
779+
771780// ========== Pipeline error handling tests ==========
772781
773782func TestPipeline_TransportError (t * testing.T ) {
@@ -781,6 +790,45 @@ func TestPipeline_TransportError(t *testing.T) {
781790 assert .Len (t , results , 3 )
782791}
783792
793+ func TestDualWriter_Script_CachesEvalForEvalSHAFallback (t * testing.T ) {
794+ primary := newMockBackend ("primary" )
795+ primary .doFunc = makeCmd ("OK" , nil )
796+
797+ secondary := newMockBackend ("secondary" )
798+ script := "return ARGV[1]"
799+ sha := scriptSHA (script )
800+ var calls int
801+ secondary .doFunc = func (ctx context.Context , args ... any ) * redis.Cmd {
802+ calls ++
803+ cmd := redis .NewCmd (ctx , args ... )
804+ switch calls {
805+ case 1 :
806+ assert .Equal (t , []byte ("EVALSHA" ), args [0 ])
807+ assert .Equal (t , []byte (sha ), args [1 ])
808+ cmd .SetErr (testRedisErr ("NOSCRIPT No matching script. Please use EVAL." ))
809+ case 2 :
810+ assert .Equal (t , []byte ("EVAL" ), args [0 ])
811+ assert .Equal (t , []byte (script ), args [1 ])
812+ cmd .SetVal ("OK" )
813+ default :
814+ t .Fatalf ("unexpected secondary call %d" , calls )
815+ }
816+ return cmd
817+ }
818+
819+ metrics := newTestMetrics ()
820+ d := NewDualWriter (primary , secondary , ProxyConfig {Mode : ModeRedisOnly , SecondaryTimeout : time .Second }, metrics , newTestSentry (), testLogger )
821+
822+ _ , err := d .Script (context .Background (), "EVAL" , [][]byte {[]byte ("EVAL" ), []byte (script ), []byte ("0" ), []byte ("value" )})
823+ assert .NoError (t , err )
824+
825+ d .cfg .Mode = ModeDualWrite
826+ d .writeSecondary ("EVALSHA" , []any {[]byte ("EVALSHA" ), []byte (sha ), []byte ("0" ), []byte ("value" )})
827+
828+ assert .Equal (t , 2 , calls )
829+ assert .InDelta (t , 0 , testutil .ToFloat64 (metrics .SecondaryWriteErrors ), 0.001 )
830+ }
831+
784832// ========== writeRedisValue tests ==========
785833
786834// testRedisErr satisfies the redis.Error interface for testing.
0 commit comments