diff --git a/temporal/internal/driver/redisv9/keyvalue.go b/temporal/internal/driver/redisv9/keyvalue.go index 65ea9c71..e93adf60 100644 --- a/temporal/internal/driver/redisv9/keyvalue.go +++ b/temporal/internal/driver/redisv9/keyvalue.go @@ -99,7 +99,7 @@ func (r *RedisV9) Expire(ctx context.Context, key string, expiration time.Durati // TTL returns the remaining time to live of a key that has a timeout func (r *RedisV9) TTL(ctx context.Context, key string) (int64, error) { if key == "" { - return 0, temperr.KeyEmpty + return -2, temperr.KeyEmpty } duration, err := r.client.TTL(ctx, key).Result() @@ -107,6 +107,12 @@ func (r *RedisV9) TTL(ctx context.Context, key string) (int64, error) { return 0, err } + // since redis-go v8.3.1, if there's no expiration or the key doesn't exists, + // the ttl returned is measured in nanoseconds + if duration.Nanoseconds() == -1 || duration.Nanoseconds() == -2 { + return duration.Nanoseconds(), nil + } + return int64(duration.Seconds()), nil } diff --git a/temporal/keyvalue/keyvalue_test.go b/temporal/keyvalue/keyvalue_test.go index db365292..2d6a58e1 100644 --- a/temporal/keyvalue/keyvalue_test.go +++ b/temporal/keyvalue/keyvalue_test.go @@ -505,15 +505,39 @@ func TestKeyValue_TTL(t *testing.T) { expectedErr: nil, }, { - name: "non_existing_key", + name: "existing_non_exipiring_key", + setup: func(db KeyValue) { + err := db.Set(context.Background(), "key2", "value1", -1*time.Second) + if err != nil { + t.Fatalf("Set() error = %v", err) + } + }, key: "key2", - expectedTTL: 0, + expectedTTL: -1, + expectedErr: nil, + }, + { + name: "key_without_ttl", + setup: func(db KeyValue) { + err := db.Set(context.Background(), "key_without_ttl", "value1", 0*time.Second) + if err != nil { + t.Fatalf("Set() error = %v", err) + } + }, + key: "key_without_ttl", + expectedTTL: -1, + expectedErr: nil, + }, + { + name: "non_existing_key", + key: "non_existing_key", + expectedTTL: -2, expectedErr: nil, }, { name: "empty_key", key: "", - expectedTTL: 0, + expectedTTL: -2, expectedErr: temperr.KeyEmpty, }, }