|
@@ -0,0 +1,117 @@
|
|
|
|
|
+package test
|
|
|
|
|
+
|
|
|
|
|
+import (
|
|
|
|
|
+ "context"
|
|
|
|
|
+ "encoding/json"
|
|
|
|
|
+ "fmt"
|
|
|
|
|
+ "git.sxidc.com/service-supports/dapr_api/pubsub"
|
|
|
|
|
+ "git.sxidc.com/service-supports/dapr_api/utils"
|
|
|
|
|
+ "io"
|
|
|
|
|
+ "net/http"
|
|
|
|
|
+ "testing"
|
|
|
|
|
+ "time"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+const (
|
|
|
|
|
+ serverAddress = "localhost:10081"
|
|
|
|
|
+ sendMessageMaxCount = 3
|
|
|
|
|
+ pubsubName = "dapr_pubsub"
|
|
|
|
|
+ topic = "test_pubsub"
|
|
|
|
|
+)
|
|
|
|
|
+
|
|
|
|
|
+func TestPubSub(t *testing.T) {
|
|
|
|
|
+ api := pubsub.NewAPI(daprHttpPort, 10*time.Second)
|
|
|
|
|
+ defer pubsub.DestroyAPI(api)
|
|
|
|
|
+
|
|
|
|
|
+ sendMessageCount := 0
|
|
|
|
|
+
|
|
|
|
|
+ srv := initHttpServer(&sendMessageCount)
|
|
|
|
|
+
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ for sendMessageCount < sendMessageMaxCount {
|
|
|
|
|
+ data, err := json.Marshal(map[string]interface{}{"data": utils.SimpleUUID()})
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ fmt.Println(err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ err = api.Publish(pubsubName, topic, string(data), nil)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ fmt.Println(err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+
|
|
|
|
|
+ for sendMessageCount < sendMessageMaxCount {
|
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _ = srv.Shutdown(context.Background())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func TestPubSubJson(t *testing.T) {
|
|
|
|
|
+ api := pubsub.NewAPI(daprHttpPort, 10*time.Second)
|
|
|
|
|
+ defer pubsub.DestroyAPI(api)
|
|
|
|
|
+
|
|
|
|
|
+ sendMessageCount := 0
|
|
|
|
|
+
|
|
|
|
|
+ srv := initHttpServer(&sendMessageCount)
|
|
|
|
|
+
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ for sendMessageCount < sendMessageMaxCount {
|
|
|
|
|
+ data, err := json.Marshal(map[string]interface{}{"data": utils.SimpleUUID()})
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ fmt.Println(err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ err = api.PublishJSON(pubsubName, topic, string(data), nil)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ fmt.Println(err)
|
|
|
|
|
+ continue
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+
|
|
|
|
|
+ for sendMessageCount < sendMessageMaxCount {
|
|
|
|
|
+ time.Sleep(1 * time.Second)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ _ = srv.Shutdown(context.Background())
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+func initHttpServer(sendMessageCount *int) *http.Server {
|
|
|
|
|
+ mux := http.NewServeMux()
|
|
|
|
|
+ mux.HandleFunc("/test_pubsub", func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
+ body, err := io.ReadAll(r.Body)
|
|
|
|
|
+ if err != nil {
|
|
|
|
|
+ fmt.Println(err)
|
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ fmt.Println("Receive", string(body))
|
|
|
|
|
+
|
|
|
|
|
+ *sendMessageCount++
|
|
|
|
|
+
|
|
|
|
|
+ w.WriteHeader(http.StatusOK)
|
|
|
|
|
+ })
|
|
|
|
|
+
|
|
|
|
|
+ srv := &http.Server{
|
|
|
|
|
+ Addr: serverAddress,
|
|
|
|
|
+ Handler: mux,
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ go func() {
|
|
|
|
|
+ err := srv.ListenAndServe()
|
|
|
|
|
+ if err != nil && err != http.ErrServerClosed {
|
|
|
|
|
+ panic(err)
|
|
|
|
|
+ }
|
|
|
|
|
+ }()
|
|
|
|
|
+
|
|
|
|
|
+ return srv
|
|
|
|
|
+}
|