You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@eventmesh.apache.org by wa...@apache.org on 2023/01/11 02:49:32 UTC

[incubator-eventmesh] branch master updated: fix issue2897

This is an automated email from the ASF dual-hosted git repository.

walleliu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-eventmesh.git


The following commit(s) were added to refs/heads/master by this push:
     new dfbae9e3a fix issue2897
     new dbd299c12 Merge pull request #2898 from jonyangx/issue2897
dfbae9e3a is described below

commit dfbae9e3a7ae7f929b14e10f00869d592061d0e8
Author: jonyangx <jo...@gmail.com>
AuthorDate: Tue Jan 10 23:41:58 2023 +0800

    fix issue2897
---
 .../http/producer/cloudevent_producer_test.go      | 142 ++++++++++-----------
 .../producer/eventmesh_message_producer_test.go    | 123 +++++++++---------
 .../http/producer/producer_suite_test.go           |  61 +++++++++
 3 files changed, 185 insertions(+), 141 deletions(-)

diff --git a/eventmesh-sdk-go/http/producer/cloudevent_producer_test.go b/eventmesh-sdk-go/http/producer/cloudevent_producer_test.go
index 6d907492c..79babc595 100644
--- a/eventmesh-sdk-go/http/producer/cloudevent_producer_test.go
+++ b/eventmesh-sdk-go/http/producer/cloudevent_producer_test.go
@@ -1,17 +1,19 @@
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements.  See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You 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.
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 producer
 
@@ -22,75 +24,63 @@ import (
 	"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/http/conf"
 	cloudevents "github.com/cloudevents/sdk-go/v2"
 	"github.com/google/uuid"
-	"github.com/stretchr/testify/assert"
-	"net/http"
-	"net/http/httptest"
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
 	"strconv"
 	"strings"
-	"testing"
 	"time"
 )
 
-func TestEventMeshHttpProducer_PublishCloudEvent(t *testing.T) {
-	f := func(w http.ResponseWriter, r *http.Request) {
-		w.WriteHeader(http.StatusOK)
-		w.Write([]byte(`{"retCode":0}`))
-	}
-	server := httptest.NewServer(http.HandlerFunc(f))
-	defer server.Close()
+var _ = Describe("EventMeshHttpProducer test", func() {
 
-	eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
-	sp := strings.Split(server.URL, ":")
-	eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
-	// Make event to send
-	event := cloudevents.NewEvent()
-	event.SetID(uuid.New().String())
-	event.SetSubject("test-topic")
-	event.SetSource("test-uri")
-	event.SetType(common.Constants.CLOUD_EVENTS_PROTOCOL_NAME)
-	event.SetExtension(common.Constants.EVENTMESH_MESSAGE_CONST_TTL, strconv.Itoa(4*1000))
-	event.SetDataContentType(cloudevents.ApplicationCloudEventsJSON)
-	data := map[string]string{"hello": "EventMesh"}
-	err := event.SetData(cloudevents.ApplicationCloudEventsJSON, utils.MarshalJsonBytes(data))
-	if err != nil {
-		t.Fail()
-	}
-	// Publish event
-	httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
-	err = httpProducer.PublishCloudEvent(&event)
-	assert.Nil(t, err)
-}
+	Context("PublishCloudEvent  test", func() {
+		It("should success", func() {
+			eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
+			sp := strings.Split(server.URL, ":")
+			eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
+			// Make event to send
+			event := cloudevents.NewEvent()
+			event.SetID(uuid.New().String())
+			event.SetSubject("test-topic")
+			event.SetSource("test-uri")
+			event.SetType(common.Constants.CLOUD_EVENTS_PROTOCOL_NAME)
+			event.SetExtension(common.Constants.EVENTMESH_MESSAGE_CONST_TTL, strconv.Itoa(4*1000))
+			event.SetDataContentType(cloudevents.ApplicationCloudEventsJSON)
+			data := map[string]string{"hello": "EventMesh"}
+			err := event.SetData(cloudevents.ApplicationCloudEventsJSON, utils.MarshalJsonBytes(data))
+			Ω(err).NotTo(HaveOccurred())
 
-func TestEventMeshHttpProducer_RequestCloudEvent(t *testing.T) {
-	f := func(w http.ResponseWriter, r *http.Request) {
-		w.WriteHeader(http.StatusOK)
-		w.Write([]byte(`{"retCode":0, "retMsg":"{\"topic\":\"test-topic\",\"body\":\"{\\\"data\\\":1}\",\"properties\":null}"}`))
-	}
+			// Publish event
+			httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
+			err = httpProducer.PublishCloudEvent(&event)
+			Ω(err).NotTo(HaveOccurred())
+		})
+	})
 
-	server := httptest.NewServer(http.HandlerFunc(f))
-	defer server.Close()
+	Context("RequestCloudEvent  test", func() {
+		It("should success", func() {
+			eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
+			sp := strings.Split(server.URL, ":")
+			eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
+			// Make event to send
+			event := cloudevents.NewEvent()
+			event.SetID(uuid.New().String())
+			event.SetSubject("test-topic")
+			event.SetSource("test-uri")
+			event.SetType(common.Constants.CLOUD_EVENTS_PROTOCOL_NAME)
+			event.SetExtension(common.Constants.EVENTMESH_MESSAGE_CONST_TTL, strconv.Itoa(4*1000))
+			event.SetDataContentType(cloudevents.ApplicationCloudEventsJSON)
+			data := map[string]string{"hello": "EventMesh"}
+			err := event.SetData(cloudevents.ApplicationCloudEventsJSON, utils.MarshalJsonBytes(data))
+			Ω(err).NotTo(HaveOccurred())
 
-	eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
-	sp := strings.Split(server.URL, ":")
-	eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
-	// Make event to send
-	event := cloudevents.NewEvent()
-	event.SetID(uuid.New().String())
-	event.SetSubject("test-topic")
-	event.SetSource("test-uri")
-	event.SetType(common.Constants.CLOUD_EVENTS_PROTOCOL_NAME)
-	event.SetExtension(common.Constants.EVENTMESH_MESSAGE_CONST_TTL, strconv.Itoa(4*1000))
-	event.SetDataContentType(cloudevents.ApplicationCloudEventsJSON)
-	data := map[string]string{"hello": "EventMesh"}
-	err := event.SetData(cloudevents.ApplicationCloudEventsJSON, utils.MarshalJsonBytes(data))
-	if err != nil {
-		t.Fail()
-	}
+			httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
+			ret, err := httpProducer.RequestCloudEvent(&event, time.Second)
+			Ω(err).NotTo(HaveOccurred())
 
-	httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
-	ret, err := httpProducer.RequestCloudEvent(&event, time.Second)
-	assert.Nil(t, err)
-	retData := make(map[string]interface{})
-	utils.UnMarshalJsonString(string(ret.DataEncoded), &retData)
-	assert.Equal(t, float64(1), retData["data"])
-}
+			retData := make(map[string]interface{})
+			utils.UnMarshalJsonString(string(ret.DataEncoded), &retData)
+			Ω(float64(1)).To(Equal(retData["data"]))
+		})
+	})
+})
diff --git a/eventmesh-sdk-go/http/producer/eventmesh_message_producer_test.go b/eventmesh-sdk-go/http/producer/eventmesh_message_producer_test.go
index 1d400c8be..dc6a78fd4 100644
--- a/eventmesh-sdk-go/http/producer/eventmesh_message_producer_test.go
+++ b/eventmesh-sdk-go/http/producer/eventmesh_message_producer_test.go
@@ -1,17 +1,19 @@
-// Licensed to the Apache Software Foundation (ASF) under one or more
-// contributor license agreements.  See the NOTICE file distributed with
-// this work for additional information regarding copyright ownership.
-// The ASF licenses this file to You 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.
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 producer
 
@@ -20,63 +22,54 @@ import (
 	"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/common/protocol"
 	"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/common/utils"
 	"github.com/apache/incubator-eventmesh/eventmesh-sdk-go/http/conf"
-	"github.com/stretchr/testify/assert"
-	"net/http"
-	"net/http/httptest"
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
 	"strings"
-	"testing"
 	"time"
 )
 
-func TestEventMeshHttpProducer_PublishEventMeshMessage(t *testing.T) {
-	f := func(w http.ResponseWriter, r *http.Request) {
-		w.WriteHeader(http.StatusOK)
-		w.Write([]byte(`{"retCode":0}`))
-	}
-	server := httptest.NewServer(http.HandlerFunc(f))
-	defer server.Close()
+var _ = Describe("EventMeshHttpProducer test", func() {
 
-	eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
-	sp := strings.Split(server.URL, ":")
-	eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
+	Context("PublishEventMeshMessage  test", func() {
+		It("should success", func() {
+			eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
+			sp := strings.Split(server.URL, ":")
+			eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
 
-	message := &protocol.EventMeshMessage{
-		BizSeqNo: "test-biz-no",
-		UniqueId: "test-unique-id",
-		Topic:    "test-topic",
-		Content:  "test-content",
-		Prop:     map[string]string{"hello": "EventMesh"},
-	}
-	// Publish event
-	httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
-	err := httpProducer.PublishEventMeshMessage(message)
-	assert.Nil(t, err)
-}
+			message := &protocol.EventMeshMessage{
+				BizSeqNo: "test-biz-no",
+				UniqueId: "test-unique-id",
+				Topic:    "test-topic",
+				Content:  "test-content",
+				Prop:     map[string]string{"hello": "EventMesh"},
+			}
+			// Publish event
+			httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
+			err := httpProducer.PublishEventMeshMessage(message)
+			Ω(err).NotTo(HaveOccurred())
+		})
+	})
 
-func TestEventMeshHttpProducer_RequestEventMeshMessage(t *testing.T) {
-	f := func(w http.ResponseWriter, r *http.Request) {
-		w.WriteHeader(http.StatusOK)
-		w.Write([]byte(`{"retCode":0, "retMsg":"{\"topic\":\"test-topic\",\"body\":\"{\\\"data\\\":1}\",\"properties\":null}"}`))
-	}
+	Context("RequestEventMeshMessage  test", func() {
+		It("should success", func() {
+			eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
+			sp := strings.Split(server.URL, ":")
+			eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
 
-	server := httptest.NewServer(http.HandlerFunc(f))
-	defer server.Close()
+			message := &protocol.EventMeshMessage{
+				BizSeqNo: "test-biz-no",
+				UniqueId: "test-unique-id",
+				Topic:    "test-topic",
+				Content:  "test-content",
+				Prop:     map[string]string{"hello": "EventMesh"},
+			}
+			httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
+			ret, err := httpProducer.RequestEventMeshMessage(message, time.Second)
+			Ω(err).NotTo(HaveOccurred())
 
-	eventMeshClientConfig := conf.DefaultEventMeshHttpClientConfig
-	sp := strings.Split(server.URL, ":")
-	eventMeshClientConfig.SetLiteEventMeshAddr(fmt.Sprintf("127.0.0.1:%s", sp[len(sp)-1]))
-
-	message := &protocol.EventMeshMessage{
-		BizSeqNo: "test-biz-no",
-		UniqueId: "test-unique-id",
-		Topic:    "test-topic",
-		Content:  "test-content",
-		Prop:     map[string]string{"hello": "EventMesh"},
-	}
-	httpProducer := NewEventMeshHttpProducer(eventMeshClientConfig)
-	ret, err := httpProducer.RequestEventMeshMessage(message, time.Second)
-	assert.Nil(t, err)
-	retData := make(map[string]interface{})
-	utils.UnMarshalJsonString(ret.Content, &retData)
-	assert.Equal(t, float64(1), retData["data"])
-}
+			retData := make(map[string]interface{})
+			utils.UnMarshalJsonString(ret.Content, &retData)
+			Ω(float64(1)).To(Equal(retData["data"]))
+		})
+	})
+})
diff --git a/eventmesh-sdk-go/http/producer/producer_suite_test.go b/eventmesh-sdk-go/http/producer/producer_suite_test.go
new file mode 100644
index 000000000..a319e59e5
--- /dev/null
+++ b/eventmesh-sdk-go/http/producer/producer_suite_test.go
@@ -0,0 +1,61 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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 producer
+
+import (
+	gutils "github.com/apache/incubator-eventmesh/eventmesh-sdk-go/common/utils"
+	ghttp "github.com/apache/incubator-eventmesh/eventmesh-sdk-go/http"
+	. "github.com/onsi/ginkgo"
+	. "github.com/onsi/gomega"
+	"net/http"
+	"net/http/httptest"
+	"testing"
+)
+
+func TestProducerAPIs(t *testing.T) {
+	RegisterFailHandler(Fail)
+	RunSpecs(t, "producer module Tests")
+}
+
+var server *httptest.Server
+
+var _ = BeforeSuite(func() {
+	retData := make(map[string]interface{})
+	retData["data"] = 1
+
+	rpy := ghttp.ReplyMessage{
+		Topic: "test-topic",
+		Body:  gutils.MarshalJsonString(retData),
+	}
+
+	ret := ghttp.EventMeshRetObj{
+		RetCode: 0,
+		RetMsg:  gutils.MarshalJsonString(rpy),
+	}
+	f := func(w http.ResponseWriter, r *http.Request) {
+		w.WriteHeader(http.StatusOK)
+		w.Write([]byte(gutils.MarshalJsonString(ret)))
+	}
+	server = httptest.NewServer(http.HandlerFunc(f))
+})
+
+var _ = AfterSuite(func() {
+	if server != nil {
+		server.Close()
+	}
+})


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@eventmesh.apache.org
For additional commands, e-mail: commits-help@eventmesh.apache.org