|
1 | 1 | package memcached |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "strconv" |
4 | 5 | "strings" |
5 | 6 | "testing" |
6 | 7 | "time" |
7 | 8 |
|
8 | 9 | "github.com/dapr/components-contrib/state" |
| 10 | + "github.com/dapr/kit/logger" |
9 | 11 | "github.com/stretchr/testify/assert" |
10 | 12 | ) |
11 | 13 |
|
@@ -65,3 +67,52 @@ func TestMemcachedMetadata(t *testing.T) { |
65 | 67 | assert.Equal(t, 5000*time.Millisecond, metadata.timeout) |
66 | 68 | }) |
67 | 69 | } |
| 70 | + |
| 71 | +func TestParseTTL(t *testing.T) { |
| 72 | + store := NewMemCacheStateStore(logger.NewLogger("test")) |
| 73 | + t.Run("TTL Not an integer", func(t *testing.T) { |
| 74 | + ttlInSeconds := "not an integer" |
| 75 | + ttl, err := store.parseTTL(&state.SetRequest{ |
| 76 | + Metadata: map[string]string{ |
| 77 | + "ttlInSeconds": ttlInSeconds, |
| 78 | + }, |
| 79 | + }) |
| 80 | + |
| 81 | + assert.NotNil(t, err, "tll is not an integer") |
| 82 | + assert.Nil(t, ttl) |
| 83 | + }) |
| 84 | + t.Run("TTL specified with wrong key", func(t *testing.T) { |
| 85 | + ttlInSeconds := 12345 |
| 86 | + ttl, err := store.parseTTL(&state.SetRequest{ |
| 87 | + Metadata: map[string]string{ |
| 88 | + "expirationTime": strconv.Itoa(ttlInSeconds), |
| 89 | + }, |
| 90 | + }) |
| 91 | + |
| 92 | + assert.NoError(t, err) |
| 93 | + assert.Nil(t, ttl) |
| 94 | + }) |
| 95 | + t.Run("TTL is a number", func(t *testing.T) { |
| 96 | + ttlInSeconds := 12345 |
| 97 | + ttl, err := store.parseTTL(&state.SetRequest{ |
| 98 | + Metadata: map[string]string{ |
| 99 | + "ttlInSeconds": strconv.Itoa(ttlInSeconds), |
| 100 | + }, |
| 101 | + }) |
| 102 | + |
| 103 | + assert.NoError(t, err) |
| 104 | + assert.Equal(t, int(*ttl), ttlInSeconds) |
| 105 | + }) |
| 106 | + |
| 107 | + t.Run("TTL never expires", func(t *testing.T) { |
| 108 | + ttlInSeconds := 0 |
| 109 | + ttl, err := store.parseTTL(&state.SetRequest{ |
| 110 | + Metadata: map[string]string{ |
| 111 | + "ttlInSeconds": strconv.Itoa(ttlInSeconds), |
| 112 | + }, |
| 113 | + }) |
| 114 | + |
| 115 | + assert.NoError(t, err) |
| 116 | + assert.Equal(t, int(*ttl), ttlInSeconds) |
| 117 | + }) |
| 118 | +} |
0 commit comments