@@ -19,11 +19,15 @@ import (
1919 "fmt"
2020 "net"
2121 "os"
22+ "strconv"
23+ "sync"
2224 "testing"
25+ "time"
2326
2427 "github.com/golang/protobuf/ptypes/empty"
28+ "github.com/google/uuid"
29+ "github.com/pkg/errors"
2530 "github.com/stretchr/testify/assert"
26-
2731 "google.golang.org/grpc"
2832 "google.golang.org/grpc/test/bufconn"
2933 "google.golang.org/protobuf/types/known/anypb"
@@ -130,7 +134,8 @@ func TestShutdown(t *testing.T) {
130134func getTestClient (ctx context.Context ) (client Client , closer func ()) {
131135 s := grpc .NewServer ()
132136 pb .RegisterDaprServer (s , & testDaprServer {
133- state : make (map [string ][]byte ),
137+ state : make (map [string ][]byte ),
138+ configurationSubscriptionID : map [string ]chan struct {}{},
134139 })
135140
136141 l := bufconn .Listen (testBufSize )
@@ -161,7 +166,8 @@ func getTestClient(ctx context.Context) (client Client, closer func()) {
161166func getTestClientWithSocket (ctx context.Context ) (client Client , closer func ()) {
162167 s := grpc .NewServer ()
163168 pb .RegisterDaprServer (s , & testDaprServer {
164- state : make (map [string ][]byte ),
169+ state : make (map [string ][]byte ),
170+ configurationSubscriptionID : map [string ]chan struct {}{},
165171 })
166172
167173 var lc net.ListenConfig
@@ -191,7 +197,9 @@ func getTestClientWithSocket(ctx context.Context) (client Client, closer func())
191197
192198type testDaprServer struct {
193199 pb.UnimplementedDaprServer
194- state map [string ][]byte
200+ state map [string ][]byte
201+ configurationSubscriptionIDMapLoc sync.Mutex
202+ configurationSubscriptionID map [string ]chan struct {}
195203}
196204
197205func (s * testDaprServer ) InvokeService (ctx context.Context , req * pb.InvokeServiceRequest ) (* commonv1pb.InvokeResponse , error ) {
@@ -348,3 +356,62 @@ func (s *testDaprServer) UnregisterActorTimer(context.Context, *pb.UnregisterAct
348356func (s * testDaprServer ) Shutdown (ctx context.Context , req * empty.Empty ) (* empty.Empty , error ) {
349357 return & empty.Empty {}, nil
350358}
359+
360+ func (s * testDaprServer ) GetConfigurationAlpha1 (ctx context.Context , in * pb.GetConfigurationRequest ) (* pb.GetConfigurationResponse , error ) {
361+ if in .GetStoreName () == "" {
362+ return & pb.GetConfigurationResponse {}, errors .New ("store name notfound" )
363+ }
364+ items := make ([]* commonv1pb.ConfigurationItem , 0 )
365+ for _ , v := range in .GetKeys () {
366+ items = append (items , & commonv1pb.ConfigurationItem {
367+ Key : v ,
368+ Value : v + valueSuffix ,
369+ })
370+ }
371+ return & pb.GetConfigurationResponse {
372+ Items : items ,
373+ }, nil
374+ }
375+
376+ func (s * testDaprServer ) SubscribeConfigurationAlpha1 (in * pb.SubscribeConfigurationRequest , server pb.Dapr_SubscribeConfigurationAlpha1Server ) error {
377+ stopCh := make (chan struct {})
378+ id , _ := uuid .NewUUID ()
379+ s .configurationSubscriptionIDMapLoc .Lock ()
380+ s .configurationSubscriptionID [id .String ()] = stopCh
381+ s .configurationSubscriptionIDMapLoc .Unlock ()
382+ for i := 0 ; i < 5 ; i ++ {
383+ select {
384+ case <- stopCh :
385+ return nil
386+ default :
387+ }
388+ items := make ([]* commonv1pb.ConfigurationItem , 0 )
389+ for _ , v := range in .GetKeys () {
390+ items = append (items , & commonv1pb.ConfigurationItem {
391+ Key : v ,
392+ Value : v + "_" + strconv .Itoa (i ),
393+ },
394+ )
395+ }
396+ if err := server .Send (& pb.SubscribeConfigurationResponse {
397+ Id : id .String (),
398+ Items : items ,
399+ }); err != nil {
400+ return err
401+ }
402+ time .Sleep (time .Second )
403+ }
404+ return nil
405+ }
406+
407+ func (s * testDaprServer ) UnsubscribeConfigurationAlpha1 (ctx context.Context , in * pb.UnsubscribeConfigurationRequest ) (* pb.UnsubscribeConfigurationResponse , error ) {
408+ s .configurationSubscriptionIDMapLoc .Lock ()
409+ defer s .configurationSubscriptionIDMapLoc .Unlock ()
410+ ch , ok := s .configurationSubscriptionID [in .Id ]
411+ if ! ok {
412+ return & pb.UnsubscribeConfigurationResponse {Ok : true }, nil
413+ }
414+ close (ch )
415+ delete (s .configurationSubscriptionID , in .Id )
416+ return & pb.UnsubscribeConfigurationResponse {Ok : true }, nil
417+ }
0 commit comments