Skip to content

Commit

Permalink
add meta in state delete api (#82)
Browse files Browse the repository at this point in the history
* add meta in state delete api

* add testcase for state delete, also cover all the get/save/delete method with meta and options
  • Loading branch information
skyao authored Sep 29, 2020
1 parent 2d699b6 commit 7e20fe9
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 3 deletions.
2 changes: 1 addition & 1 deletion client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type Client interface {
DeleteState(ctx context.Context, store, key string) error

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

// ExecuteStateTransaction provides way to execute multiple operations on a specified store.
ExecuteStateTransaction(ctx context.Context, store string, meta map[string]string, ops []*StateOperation) error
Expand Down
5 changes: 3 additions & 2 deletions client/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,11 @@ func (c *GRPCClient) GetStateWithConsistency(ctx context.Context, store, key str

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

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

_, err := c.protoClient.DeleteState(c.withAuthToken(ctx), req)
Expand Down
77 changes: 77 additions & 0 deletions client/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,83 @@ func TestSaveState(t *testing.T) {
})
}

// go test -timeout 30s ./client -count 1 -run ^TestDeleteState$
func TestDeleteState(t *testing.T) {
ctx := context.Background()
data := "test"
store := "test"
key := "key1"

t.Run("delete not exist data", func(t *testing.T) {
err := testClient.DeleteState(ctx, store, key)
assert.Nil(t, err)
})
t.Run("delete not exist data with etag and meta", func(t *testing.T) {
err := testClient.DeleteStateWithETag(ctx, store, key, "100", map[string]string{"meta1": "value1"},
&StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual})
assert.Nil(t, err)
})

t.Run("save data", func(t *testing.T) {
err := testClient.SaveState(ctx, store, key, []byte(data))
assert.Nil(t, err)
})
t.Run("confirm data saved", func(t *testing.T) {
item, err := testClient.GetState(ctx, store, key)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
assert.Equal(t, item.Key, key)
assert.Equal(t, string(item.Value), data)
})

t.Run("delete exist data", func(t *testing.T) {
err := testClient.DeleteState(ctx, store, key)
assert.Nil(t, err)
})
t.Run("confirm data deleted", func(t *testing.T) {
item, err := testClient.GetState(ctx, store, key)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
assert.Equal(t, item.Key, key)
assert.Nil(t, item.Value)
})

t.Run("save data again with etag, meta", func(t *testing.T) {
err := testClient.SaveStateItems(ctx, store, &SetStateItem{
Key: key,
Value: []byte(data),
Etag: "100",
Metadata: map[string]string{"meta1": "value1"},
Options: &StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual},
})
assert.Nil(t, err)
})
t.Run("confirm data saved", func(t *testing.T) {
item, err := testClient.GetStateWithConsistency(ctx, store, key, map[string]string{"meta1": "value1"}, StateConsistencyEventual)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
assert.Equal(t, item.Key, key)
assert.Equal(t, string(item.Value), data)
})

t.Run("delete exist data with etag and meta", func(t *testing.T) {
err := testClient.DeleteStateWithETag(ctx, store, key, "100", map[string]string{"meta1": "value1"},
&StateOptions{Concurrency: StateConcurrencyFirstWrite, Consistency: StateConsistencyEventual})
assert.Nil(t, err)
})
t.Run("confirm data deleted", func(t *testing.T) {
item, err := testClient.GetStateWithConsistency(ctx, store, key, map[string]string{"meta1": "value1"}, StateConsistencyEventual)
assert.Nil(t, err)
assert.NotNil(t, item)
assert.NotEmpty(t, item.Etag)
assert.Equal(t, item.Key, key)
assert.Nil(t, item.Value)
})
}

// go test -timeout 30s ./client -count 1 -run ^TestStateTransactions$
func TestStateTransactions(t *testing.T) {
ctx := context.Background()
Expand Down

0 comments on commit 7e20fe9

Please sign in to comment.