Skip to content

Commit

Permalink
feature: support dapr state api (mosn#377)
Browse files Browse the repository at this point in the history
* dapr/state:add get/save state dapr api

Signed-off-by: zach <zachchou016@gmail.com>

* dapr/state:add query/delete state

Signed-off-by: zach <zachchou016@gmail.com>

* dapr/state_api:replace cel-go with v0.5.1

Signed-off-by: zach <zachchou016@gmail.com>

* dapr/state_api:fix state api ut

Signed-off-by: zach <zachchou016@gmail.com>

* dapr/state_api:init slice with cap

Signed-off-by: zach <zachchou016@gmail.com>

* dapr/state_api: remove encrypt dependency

Signed-off-by: zach <zachchou016@gmail.com>

* remove unused code

* add comments

Co-authored-by: seeflood <349895584@qq.com>
# Conflicts:
#	pkg/grpc/dapr/dapr_api_state.go
#	pkg/grpc/dapr/dapr_api_unimplement.go
#	pkg/grpc/default_api/api.go
#	pkg/grpc/default_api/api_state.go
  • Loading branch information
seeflood committed Feb 10, 2022
1 parent a23b07f commit 7eb35e5
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 108 deletions.
67 changes: 67 additions & 0 deletions pkg/grpc/dapr/dapr_api_state_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Copyright 2021 Layotto Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package dapr

import (
"github.com/dapr/components-contrib/state"
"github.com/stretchr/testify/assert"
"testing"
)

func TestGetResponse2GetStateResponse(t *testing.T) {
resp := GetResponse2GetStateResponse(&state.GetResponse{
Data: []byte("v"),
ETag: nil,
Metadata: make(map[string]string),
})
assert.Equal(t, resp.Data, []byte("v"))
assert.Equal(t, resp.Etag, "")
assert.True(t, len(resp.Metadata) == 0)
}

func TestGetResponse2BulkStateItem(t *testing.T) {
itm := GetResponse2BulkStateItem(&state.GetResponse{
Data: []byte("v"),
ETag: nil,
Metadata: make(map[string]string),
}, "key")
assert.Equal(t, itm.Key, "key")
assert.Equal(t, itm.Data, []byte("v"))
assert.Equal(t, itm.Etag, "")
assert.Equal(t, itm.Error, "")
assert.True(t, len(itm.Metadata) == 0)
}

func TestBulkGetResponse2BulkStateItem(t *testing.T) {
t.Run("convert nil", func(t *testing.T) {
itm := BulkGetResponse2BulkStateItem(nil)
assert.NotNil(t, itm)
})
t.Run("normal", func(t *testing.T) {
itm := BulkGetResponse2BulkStateItem(&state.BulkGetResponse{
Key: "key",
Data: []byte("v"),
ETag: nil,
Metadata: nil,
Error: "",
})
assert.Equal(t, itm.Key, "key")
assert.Equal(t, itm.Data, []byte("v"))
assert.Equal(t, itm.Etag, "")
assert.Equal(t, itm.Error, "")
assert.True(t, len(itm.Metadata) == 0)
})
}
60 changes: 60 additions & 0 deletions pkg/grpc/dapr/dapr_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/phayes/freeport"
"github.com/stretchr/testify/assert"
grpc_api "mosn.io/layotto/pkg/grpc"
dapr_common_v1pb "mosn.io/layotto/pkg/grpc/dapr/proto/common/v1"
mock_state "mosn.io/layotto/pkg/mock/components/state"
"net"
"testing"
Expand Down Expand Up @@ -127,3 +128,62 @@ func createTestClient(port int) *grpc.ClientConn {
}
return conn
}

func TestStateItem2SetRequest(t *testing.T) {
req := StateItem2SetRequest(&dapr_common_v1pb.StateItem{
Key: "",
Value: []byte("v"),
Etag: nil,
Metadata: nil,
Options: &dapr_common_v1pb.StateOptions{
Concurrency: dapr_common_v1pb.StateOptions_CONCURRENCY_UNSPECIFIED,
Consistency: dapr_common_v1pb.StateOptions_CONSISTENCY_UNSPECIFIED,
},
}, "appid||key")
assert.Equal(t, req.Key, "appid||key")
assert.Equal(t, req.Value, []byte("v"))
assert.Nil(t, req.ETag)
assert.Equal(t, req.Options.Consistency, "")
assert.Equal(t, req.Options.Concurrency, "")
}

func TestDeleteStateRequest2DeleteRequest(t *testing.T) {
t.Run("nil", func(t *testing.T) {
req := DeleteStateRequest2DeleteRequest(nil, "")
assert.NotNil(t, req)
})
t.Run("normal", func(t *testing.T) {
req := DeleteStateRequest2DeleteRequest(&dapr_v1pb.DeleteStateRequest{
StoreName: "redis",
Key: "",
Etag: nil,
Options: &dapr_common_v1pb.StateOptions{
Concurrency: dapr_common_v1pb.StateOptions_CONCURRENCY_LAST_WRITE,
Consistency: dapr_common_v1pb.StateOptions_CONSISTENCY_EVENTUAL,
},
Metadata: nil,
}, "appid||key")
assert.Equal(t, req.Key, "appid||key")
assert.Nil(t, req.ETag)
assert.Equal(t, req.Options.Consistency, "eventual")
assert.Equal(t, req.Options.Concurrency, "last-write")
})
}

func TestStateItem2DeleteRequest(t *testing.T) {
req := StateItem2DeleteRequest(&dapr_common_v1pb.StateItem{
Key: "",
Value: []byte("v"),
Etag: nil,
Metadata: nil,
Options: &dapr_common_v1pb.StateOptions{
Concurrency: dapr_common_v1pb.StateOptions_CONCURRENCY_LAST_WRITE,
Consistency: dapr_common_v1pb.StateOptions_CONSISTENCY_EVENTUAL,
},
}, "appid||key")
assert.Equal(t, req.Key, "appid||key")
assert.Nil(t, req.ETag)
assert.Nil(t, req.ETag)
assert.Equal(t, req.Options.Consistency, "eventual")
assert.Equal(t, req.Options.Concurrency, "last-write")
}
4 changes: 0 additions & 4 deletions pkg/grpc/default_api/api_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ import (
"github.com/dapr/components-contrib/state"
"google.golang.org/grpc/codes"
"google.golang.org/protobuf/types/known/emptypb"
"mosn.io/layotto/pkg/common"
dapr_common_v1pb "mosn.io/layotto/pkg/grpc/dapr/proto/common/v1"
dapr_v1pb "mosn.io/layotto/pkg/grpc/dapr/proto/runtime/v1"
state2 "mosn.io/layotto/pkg/runtime/state"
runtimev1pb "mosn.io/layotto/spec/proto/runtime/v1"
_ "net/http/pprof"

"google.golang.org/grpc/status"
)

func (a *api) SaveState(ctx context.Context, in *runtimev1pb.SaveStateRequest) (*emptypb.Empty, error) {
Expand Down
104 changes: 0 additions & 104 deletions pkg/grpc/default_api/api_state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,110 +204,6 @@ func TestSaveState(t *testing.T) {
})
}

func TestGetResponse2GetStateResponse(t *testing.T) {
resp := GetResponse2GetStateResponse(&state.GetResponse{
Data: []byte("v"),
ETag: nil,
Metadata: make(map[string]string),
})
assert.Equal(t, resp.Data, []byte("v"))
assert.Equal(t, resp.Etag, "")
assert.True(t, len(resp.Metadata) == 0)
}

func TestGetResponse2BulkStateItem(t *testing.T) {
itm := GetResponse2BulkStateItem(&state.GetResponse{
Data: []byte("v"),
ETag: nil,
Metadata: make(map[string]string),
}, "key")
assert.Equal(t, itm.Key, "key")
assert.Equal(t, itm.Data, []byte("v"))
assert.Equal(t, itm.Etag, "")
assert.Equal(t, itm.Error, "")
assert.True(t, len(itm.Metadata) == 0)
}

func TestBulkGetResponse2BulkStateItem(t *testing.T) {
t.Run("convert nil", func(t *testing.T) {
itm := BulkGetResponse2BulkStateItem(nil)
assert.NotNil(t, itm)
})
t.Run("normal", func(t *testing.T) {
itm := BulkGetResponse2BulkStateItem(&state.BulkGetResponse{
Key: "key",
Data: []byte("v"),
ETag: nil,
Metadata: nil,
Error: "",
})
assert.Equal(t, itm.Key, "key")
assert.Equal(t, itm.Data, []byte("v"))
assert.Equal(t, itm.Etag, "")
assert.Equal(t, itm.Error, "")
assert.True(t, len(itm.Metadata) == 0)
})
}

func TestStateItem2SetRequest(t *testing.T) {
req := StateItem2SetRequest(&runtimev1pb.StateItem{
Key: "",
Value: []byte("v"),
Etag: nil,
Metadata: nil,
Options: &runtimev1pb.StateOptions{
Concurrency: runtimev1pb.StateOptions_CONCURRENCY_UNSPECIFIED,
Consistency: runtimev1pb.StateOptions_CONSISTENCY_UNSPECIFIED,
},
}, "appid||key")
assert.Equal(t, req.Key, "appid||key")
assert.Equal(t, req.Value, []byte("v"))
assert.Nil(t, req.ETag)
assert.Equal(t, req.Options.Consistency, "")
assert.Equal(t, req.Options.Concurrency, "")
}

func TestDeleteStateRequest2DeleteRequest(t *testing.T) {
t.Run("nil", func(t *testing.T) {
req := DeleteStateRequest2DeleteRequest(nil, "")
assert.NotNil(t, req)
})
t.Run("normal", func(t *testing.T) {
req := DeleteStateRequest2DeleteRequest(&runtimev1pb.DeleteStateRequest{
StoreName: "redis",
Key: "",
Etag: nil,
Options: &runtimev1pb.StateOptions{
Concurrency: runtimev1pb.StateOptions_CONCURRENCY_LAST_WRITE,
Consistency: runtimev1pb.StateOptions_CONSISTENCY_EVENTUAL,
},
Metadata: nil,
}, "appid||key")
assert.Equal(t, req.Key, "appid||key")
assert.Nil(t, req.ETag)
assert.Equal(t, req.Options.Consistency, "eventual")
assert.Equal(t, req.Options.Concurrency, "last-write")
})
}

func TestStateItem2DeleteRequest(t *testing.T) {
req := StateItem2DeleteRequest(&runtimev1pb.StateItem{
Key: "",
Value: []byte("v"),
Etag: nil,
Metadata: nil,
Options: &runtimev1pb.StateOptions{
Concurrency: runtimev1pb.StateOptions_CONCURRENCY_LAST_WRITE,
Consistency: runtimev1pb.StateOptions_CONSISTENCY_EVENTUAL,
},
}, "appid||key")
assert.Equal(t, req.Key, "appid||key")
assert.Nil(t, req.ETag)
assert.Nil(t, req.ETag)
assert.Equal(t, req.Options.Consistency, "eventual")
assert.Equal(t, req.Options.Concurrency, "last-write")
}

func TestGetBulkState(t *testing.T) {
t.Run("state store not found", func(t *testing.T) {
ctrl := gomock.NewController(t)
Expand Down

0 comments on commit 7eb35e5

Please sign in to comment.