Skip to content

Commit 7e20fe9

Browse files
authored
add meta in state delete api (#82)
* add meta in state delete api * add testcase for state delete, also cover all the get/save/delete method with meta and options
1 parent 2d699b6 commit 7e20fe9

File tree

3 files changed

+81
-3
lines changed

3 files changed

+81
-3
lines changed

client/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ type Client interface {
7070
DeleteState(ctx context.Context, store, key string) error
7171

7272
// DeleteStateWithETag deletes content from store using provided state options and etag.
73-
DeleteStateWithETag(ctx context.Context, store, key, etag string, opts *StateOptions) error
73+
DeleteStateWithETag(ctx context.Context, store, key, etag string, meta map[string]string, opts *StateOptions) error
7474

7575
// ExecuteStateTransaction provides way to execute multiple operations on a specified store.
7676
ExecuteStateTransaction(ctx context.Context, store string, meta map[string]string, ops []*StateOperation) error

client/state.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,11 @@ func (c *GRPCClient) GetStateWithConsistency(ctx context.Context, store, key str
284284

285285
// DeleteState deletes content from store using default state options.
286286
func (c *GRPCClient) DeleteState(ctx context.Context, store, key string) error {
287-
return c.DeleteStateWithETag(ctx, store, key, "", nil)
287+
return c.DeleteStateWithETag(ctx, store, key, "", nil, nil)
288288
}
289289

290290
// DeleteStateWithETag deletes content from store using provided state options and etag.
291-
func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, store, key, etag string, opts *StateOptions) error {
291+
func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, store, key, etag string, meta map[string]string, opts *StateOptions) error {
292292
if store == "" {
293293
return errors.New("nil store")
294294
}
@@ -301,6 +301,7 @@ func (c *GRPCClient) DeleteStateWithETag(ctx context.Context, store, key, etag s
301301
Key: key,
302302
Etag: etag,
303303
Options: toProtoStateOptions(opts),
304+
Metadata: meta,
304305
}
305306

306307
_, err := c.protoClient.DeleteState(c.withAuthToken(ctx), req)

client/state_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,83 @@ func TestSaveState(t *testing.T) {
7373
})
7474
}
7575

76+
// go test -timeout 30s ./client -count 1 -run ^TestDeleteState$
77+
func TestDeleteState(t *testing.T) {
78+
ctx := context.Background()
79+
data := "test"
80+
store := "test"
81+
key := "key1"
82+
83+
t.Run("delete not exist data", func(t *testing.T) {
84+
err := testClient.DeleteState(ctx, store, key)
85+
assert.Nil(t, err)
86+
})
87+
t.Run("delete not exist data with etag and meta", func(t *testing.T) {
88+
err := testClient.DeleteStateWithETag(ctx, store, key, "100", map[string]string{"meta1": "value1"},
89+
&StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual})
90+
assert.Nil(t, err)
91+
})
92+
93+
t.Run("save data", func(t *testing.T) {
94+
err := testClient.SaveState(ctx, store, key, []byte(data))
95+
assert.Nil(t, err)
96+
})
97+
t.Run("confirm data saved", func(t *testing.T) {
98+
item, err := testClient.GetState(ctx, store, key)
99+
assert.Nil(t, err)
100+
assert.NotNil(t, item)
101+
assert.NotEmpty(t, item.Etag)
102+
assert.Equal(t, item.Key, key)
103+
assert.Equal(t, string(item.Value), data)
104+
})
105+
106+
t.Run("delete exist data", func(t *testing.T) {
107+
err := testClient.DeleteState(ctx, store, key)
108+
assert.Nil(t, err)
109+
})
110+
t.Run("confirm data deleted", func(t *testing.T) {
111+
item, err := testClient.GetState(ctx, store, key)
112+
assert.Nil(t, err)
113+
assert.NotNil(t, item)
114+
assert.NotEmpty(t, item.Etag)
115+
assert.Equal(t, item.Key, key)
116+
assert.Nil(t, item.Value)
117+
})
118+
119+
t.Run("save data again with etag, meta", func(t *testing.T) {
120+
err := testClient.SaveStateItems(ctx, store, &SetStateItem{
121+
Key: key,
122+
Value: []byte(data),
123+
Etag: "100",
124+
Metadata: map[string]string{"meta1": "value1"},
125+
Options: &StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual},
126+
})
127+
assert.Nil(t, err)
128+
})
129+
t.Run("confirm data saved", func(t *testing.T) {
130+
item, err := testClient.GetStateWithConsistency(ctx, store, key, map[string]string{"meta1": "value1"}, StateConsistencyEventual)
131+
assert.Nil(t, err)
132+
assert.NotNil(t, item)
133+
assert.NotEmpty(t, item.Etag)
134+
assert.Equal(t, item.Key, key)
135+
assert.Equal(t, string(item.Value), data)
136+
})
137+
138+
t.Run("delete exist data with etag and meta", func(t *testing.T) {
139+
err := testClient.DeleteStateWithETag(ctx, store, key, "100", map[string]string{"meta1": "value1"},
140+
&StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual})
141+
assert.Nil(t, err)
142+
})
143+
t.Run("confirm data deleted", func(t *testing.T) {
144+
item, err := testClient.GetStateWithConsistency(ctx, store, key, map[string]string{"meta1": "value1"}, StateConsistencyEventual)
145+
assert.Nil(t, err)
146+
assert.NotNil(t, item)
147+
assert.NotEmpty(t, item.Etag)
148+
assert.Equal(t, item.Key, key)
149+
assert.Nil(t, item.Value)
150+
})
151+
}
152+
76153
// go test -timeout 30s ./client -count 1 -run ^TestStateTransactions$
77154
func TestStateTransactions(t *testing.T) {
78155
ctx := context.Background()

0 commit comments

Comments
 (0)