|
| 1 | +package reconnectable |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/alicebob/miniredis/v2" |
| 11 | + "github.com/redis/go-redis/v9" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRefreshClientWhenServerCrash(t *testing.T) { |
| 17 | + // Step 1: Start the mock Redis server |
| 18 | + srv, err := miniredis.Run() |
| 19 | + if err != nil { |
| 20 | + t.Fatalf("could not start mock Redis server: %v", err) |
| 21 | + } |
| 22 | + defer srv.Close() |
| 23 | + |
| 24 | + // Step 2: Create the Redis client with the mock server's address |
| 25 | + cfg := &redis.UniversalOptions{ |
| 26 | + Addrs: []string{srv.Addr()}, |
| 27 | + } |
| 28 | + rc := New(cfg) |
| 29 | + oldClient := rc.UniversalClient |
| 30 | + |
| 31 | + // Step 3: Simulate server failure by stopping the Redis server |
| 32 | + srv.Close() |
| 33 | + |
| 34 | + // Step 4: Perform concurrent Set operations that should fail due to the server being down |
| 35 | + var wg sync.WaitGroup |
| 36 | + for i := 1; i <= 100; i++ { |
| 37 | + wg.Add(1) |
| 38 | + go func(i int) { |
| 39 | + defer wg.Done() |
| 40 | + rc.Set(context.Background(), fmt.Sprint(i), "1", 0) // This should fail due to the server being down |
| 41 | + }(i) |
| 42 | + } |
| 43 | + wg.Wait() |
| 44 | + |
| 45 | + // Step 5: Ensure the client was refreshed after the error (server down) |
| 46 | + newClient := rc.UniversalClient |
| 47 | + |
| 48 | + // Check that the client refresh was triggered and the refresh time is updated |
| 49 | + require.NotNil(t, rc.lastRefreshTime) |
| 50 | + lastRefresh := rc.lastRefreshTime.Load().(time.Time) |
| 51 | + assert.True(t, time.Since(lastRefresh) < time.Second, "Refresh time should be updated") |
| 52 | + |
| 53 | + // Verify that the client refresh process was completed (no ongoing refresh) |
| 54 | + assert.False(t, rc.refreshInProgress.Load()) |
| 55 | + |
| 56 | + // Ensure that a new client instance was created |
| 57 | + assert.NotEqual(t, oldClient, newClient, "UniversalClient should be updated") |
| 58 | + |
| 59 | + // Step 6: Restart the Redis server to simulate recovery |
| 60 | + srv.Start() |
| 61 | + |
| 62 | + // Step 7: Verify that the client can now successfully ping the server |
| 63 | + require.NoError(t, rc.Ping(context.Background()).Err(), "Ping should succeed after client refresh and server recovery") |
| 64 | + |
| 65 | + // Verify that the refresh time hasn't been updated since the recovery (no new refresh required) |
| 66 | + assert.True(t, lastRefresh == rc.lastRefreshTime.Load().(time.Time), "Refresh time shouldn't be updated") |
| 67 | + |
| 68 | + // Ensure that the client is still the refreshed one (not the old client) |
| 69 | + assert.Equal(t, newClient, rc.UniversalClient, "UniversalClient should still be the updated one") |
| 70 | +} |
| 71 | + |
| 72 | +func BenchmarkSetCommandWithWrappedClient(b *testing.B) { |
| 73 | + srv, err := miniredis.Run() |
| 74 | + if err != nil { |
| 75 | + b.Fatalf("could not start mock Redis server: %v", err) |
| 76 | + } |
| 77 | + defer srv.Close() |
| 78 | + |
| 79 | + cfg := &redis.UniversalOptions{ |
| 80 | + Addrs: []string{srv.Addr()}, |
| 81 | + } |
| 82 | + |
| 83 | + rc := New(cfg) |
| 84 | + b.ResetTimer() |
| 85 | + |
| 86 | + b.Run("SetCommand", func(b *testing.B) { |
| 87 | + for i := 0; i < b.N; i++ { |
| 88 | + err := rc.Set(context.Background(), "key", "value", 0).Err() |
| 89 | + if err != nil { |
| 90 | + b.Fatalf("Failed to set key: %v", err) |
| 91 | + } |
| 92 | + } |
| 93 | + }) |
| 94 | +} |
| 95 | + |
| 96 | +func BenchmarkGetCommandWithWrappedClient(b *testing.B) { |
| 97 | + srv, err := miniredis.Run() |
| 98 | + if err != nil { |
| 99 | + b.Fatalf("could not start mock Redis server: %v", err) |
| 100 | + } |
| 101 | + defer srv.Close() |
| 102 | + |
| 103 | + cfg := &redis.UniversalOptions{ |
| 104 | + Addrs: []string{srv.Addr()}, |
| 105 | + } |
| 106 | + |
| 107 | + rc := New(cfg) |
| 108 | + rc.Set(context.Background(), "key", "value", 0) |
| 109 | + |
| 110 | + b.ResetTimer() |
| 111 | + |
| 112 | + b.Run("GetCommand", func(b *testing.B) { |
| 113 | + for i := 0; i < b.N; i++ { |
| 114 | + _, err := rc.Get(context.Background(), "key").Result() |
| 115 | + if err != nil { |
| 116 | + b.Fatalf("Failed to get key: %v", err) |
| 117 | + } |
| 118 | + } |
| 119 | + }) |
| 120 | +} |
| 121 | + |
| 122 | +func BenchmarkGetCommandWithOriginalClient(b *testing.B) { |
| 123 | + srv, err := miniredis.Run() |
| 124 | + if err != nil { |
| 125 | + b.Fatalf("could not start mock Redis server: %v", err) |
| 126 | + } |
| 127 | + defer srv.Close() |
| 128 | + |
| 129 | + cfg := &redis.UniversalOptions{ |
| 130 | + Addrs: []string{srv.Addr()}, |
| 131 | + } |
| 132 | + |
| 133 | + rc := redis.NewUniversalClient(&redis.UniversalOptions{ |
| 134 | + Addrs: cfg.Addrs, |
| 135 | + MasterName: cfg.MasterName, |
| 136 | + Password: cfg.Password, |
| 137 | + SentinelPassword: cfg.SentinelPassword, |
| 138 | + DB: cfg.DB, |
| 139 | + ReadTimeout: cfg.ReadTimeout, |
| 140 | + WriteTimeout: cfg.WriteTimeout, |
| 141 | + }) |
| 142 | + rc.Set(context.Background(), "key", "value", 0) |
| 143 | + |
| 144 | + b.ResetTimer() |
| 145 | + |
| 146 | + b.Run("GetCommand", func(b *testing.B) { |
| 147 | + for i := 0; i < b.N; i++ { |
| 148 | + _, err := rc.Get(context.Background(), "key").Result() |
| 149 | + if err != nil { |
| 150 | + b.Fatalf("Failed to get key: %v", err) |
| 151 | + } |
| 152 | + } |
| 153 | + }) |
| 154 | +} |
| 155 | + |
| 156 | +func BenchmarkSetCommandWithOriginalClient(b *testing.B) { |
| 157 | + srv, err := miniredis.Run() |
| 158 | + if err != nil { |
| 159 | + b.Fatalf("could not start mock Redis server: %v", err) |
| 160 | + } |
| 161 | + defer srv.Close() |
| 162 | + |
| 163 | + cfg := &redis.UniversalOptions{ |
| 164 | + Addrs: []string{srv.Addr()}, |
| 165 | + } |
| 166 | + |
| 167 | + rc := redis.NewUniversalClient(&redis.UniversalOptions{ |
| 168 | + Addrs: cfg.Addrs, |
| 169 | + MasterName: cfg.MasterName, |
| 170 | + Password: cfg.Password, |
| 171 | + SentinelPassword: cfg.SentinelPassword, |
| 172 | + DB: cfg.DB, |
| 173 | + ReadTimeout: cfg.ReadTimeout, |
| 174 | + WriteTimeout: cfg.WriteTimeout, |
| 175 | + }) |
| 176 | + |
| 177 | + b.ResetTimer() |
| 178 | + |
| 179 | + b.Run("SetCommand", func(b *testing.B) { |
| 180 | + for i := 0; i < b.N; i++ { |
| 181 | + err := rc.Set(context.Background(), "key", "value", 0).Err() |
| 182 | + if err != nil { |
| 183 | + b.Fatalf("Failed to set key: %v", err) |
| 184 | + } |
| 185 | + } |
| 186 | + }) |
| 187 | +} |
0 commit comments