|
| 1 | +/* |
| 2 | +Copyright 2023 The Dapr Authors |
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +Unless required by applicable law or agreed to in writing, software |
| 8 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 9 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implieh. |
| 10 | +See the License for the specific language governing permissions and |
| 11 | +limitations under the License. |
| 12 | +*/ |
| 13 | + |
| 14 | +package grpc |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "sync" |
| 19 | + "testing" |
| 20 | + "time" |
| 21 | + |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | + "google.golang.org/grpc" |
| 25 | + "google.golang.org/grpc/credentials/insecure" |
| 26 | + |
| 27 | + "github.com/dapr/dapr/pkg/proto/common/v1" |
| 28 | + runtimev1pb "github.com/dapr/dapr/pkg/proto/runtime/v1" |
| 29 | + "github.com/dapr/dapr/tests/integration/framework" |
| 30 | + "github.com/dapr/dapr/tests/integration/framework/process/daprd" |
| 31 | + "github.com/dapr/dapr/tests/integration/framework/process/grpc/app" |
| 32 | + "github.com/dapr/dapr/tests/integration/suite" |
| 33 | +) |
| 34 | + |
| 35 | +func init() { |
| 36 | + suite.Register(new(projection)) |
| 37 | +} |
| 38 | + |
| 39 | +type projection struct { |
| 40 | + daprd *daprd.Daprd |
| 41 | + lock sync.Mutex |
| 42 | + msg []byte |
| 43 | +} |
| 44 | + |
| 45 | +func (o *projection) Setup(t *testing.T) []framework.Option { |
| 46 | + onTopicEvent := func(ctx context.Context, in *runtimev1pb.TopicEventRequest) (*runtimev1pb.TopicEventResponse, error) { |
| 47 | + o.lock.Lock() |
| 48 | + defer o.lock.Unlock() |
| 49 | + o.msg = in.GetData() |
| 50 | + return &runtimev1pb.TopicEventResponse{ |
| 51 | + Status: runtimev1pb.TopicEventResponse_SUCCESS, |
| 52 | + }, nil |
| 53 | + } |
| 54 | + |
| 55 | + srv1 := app.New(t, app.WithOnTopicEventFn(onTopicEvent)) |
| 56 | + o.daprd = daprd.New(t, daprd.WithAppID("outboxtest"), daprd.WithAppPort(srv1.Port(t)), daprd.WithAppProtocol("grpc"), daprd.WithResourceFiles(` |
| 57 | +apiVersion: dapr.io/v1alpha1 |
| 58 | +kind: Component |
| 59 | +metadata: |
| 60 | + name: mystore |
| 61 | +spec: |
| 62 | + type: state.in-memory |
| 63 | + version: v1 |
| 64 | + metadata: |
| 65 | + - name: outboxPublishPubsub |
| 66 | + value: "mypubsub" |
| 67 | + - name: outboxPublishTopic |
| 68 | + value: "test" |
| 69 | +`, |
| 70 | + ` |
| 71 | +apiVersion: dapr.io/v1alpha1 |
| 72 | +kind: Component |
| 73 | +metadata: |
| 74 | + name: 'mypubsub' |
| 75 | +spec: |
| 76 | + type: pubsub.in-memory |
| 77 | + version: v1 |
| 78 | +`, |
| 79 | + ` |
| 80 | +apiVersion: dapr.io/v2alpha1 |
| 81 | +kind: Subscription |
| 82 | +metadata: |
| 83 | + name: 'order' |
| 84 | +spec: |
| 85 | + topic: 'test' |
| 86 | + routes: |
| 87 | + default: '/test' |
| 88 | + pubsubname: 'mypubsub' |
| 89 | +scopes: |
| 90 | +- outboxtest |
| 91 | +`)) |
| 92 | + |
| 93 | + return []framework.Option{ |
| 94 | + framework.WithProcesses(srv1, o.daprd), |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func (o *projection) Run(t *testing.T, ctx context.Context) { |
| 99 | + o.daprd.WaitUntilRunning(t, ctx) |
| 100 | + |
| 101 | + conn, err := grpc.DialContext(ctx, o.daprd.GRPCAddress(), grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithBlock()) |
| 102 | + require.NoError(t, err) |
| 103 | + t.Cleanup(func() { require.NoError(t, conn.Close()) }) |
| 104 | + |
| 105 | + _, err = runtimev1pb.NewDaprClient(conn).ExecuteStateTransaction(ctx, &runtimev1pb.ExecuteStateTransactionRequest{ |
| 106 | + StoreName: "mystore", |
| 107 | + Operations: []*runtimev1pb.TransactionalStateOperation{ |
| 108 | + { |
| 109 | + OperationType: "upsert", |
| 110 | + Request: &common.StateItem{ |
| 111 | + Key: "1", |
| 112 | + Value: []byte("2"), |
| 113 | + }, |
| 114 | + }, |
| 115 | + { |
| 116 | + OperationType: "upsert", |
| 117 | + Request: &common.StateItem{ |
| 118 | + Key: "1", |
| 119 | + Value: []byte("3"), |
| 120 | + Metadata: map[string]string{ |
| 121 | + "outbox.projection": "true", |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + }, |
| 126 | + }) |
| 127 | + require.NoError(t, err) |
| 128 | + |
| 129 | + assert.Eventually(t, func() bool { |
| 130 | + o.lock.Lock() |
| 131 | + defer o.lock.Unlock() |
| 132 | + return string(o.msg) == "3" |
| 133 | + }, time.Second*5, time.Millisecond*10, "failed to receive message in time") |
| 134 | + |
| 135 | + assert.Eventually(t, func() bool { |
| 136 | + o.lock.Lock() |
| 137 | + defer o.lock.Unlock() |
| 138 | + |
| 139 | + resp, err := runtimev1pb.NewDaprClient(conn).GetState(ctx, &runtimev1pb.GetStateRequest{ |
| 140 | + Key: "1", |
| 141 | + StoreName: "mystore", |
| 142 | + }) |
| 143 | + require.NoError(t, err) |
| 144 | + return string(resp.GetData()) == "2" |
| 145 | + }, time.Second*5, time.Millisecond*10, "failed to receive message in time") |
| 146 | +} |
0 commit comments