You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by GitBox <gi...@apache.org> on 2020/11/11 03:08:45 UTC

[GitHub] [dubbo-go-hessian2] zonghaishang commented on a change in pull request #242: support clean encoder/decoder, discard decode buffer

zonghaishang commented on a change in pull request #242:
URL: https://github.com/apache/dubbo-go-hessian2/pull/242#discussion_r521068593



##########
File path: hessian_demo/loop_encode_decode_test.go
##########
@@ -0,0 +1,125 @@
+/*
+ * 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 hessian_demo
+
+import (
+	"reflect"
+	"testing"
+)
+
+import (
+	hessian "github.com/apache/dubbo-go-hessian2"
+)
+
+import (
+	"github.com/stretchr/testify/assert"
+)
+
+type User struct {
+	Name string
+	Age  int
+}
+
+func (User) JavaClassName() string {
+	return "hessian.demo.User"
+}
+
+type Location struct {
+	Address  string
+	Postcode string
+}
+
+func (Location) JavaClassName() string {
+	return "hessian.demo.Location"
+}
+
+func TestLoopDecode(t *testing.T) {
+	u := &User{
+		Name: "wongoo",
+		Age:  18,
+	}
+	loc := &Location{
+		Address:  "xiamen",
+		Postcode: "361000",
+	}
+
+	// demo a bytes buffer from client to server
+	var bytes []byte
+	e := hessian.NewEncoder()
+
+	// -------- encode 1
+	if err := e.Encode(u); err != nil {
+		t.Fatal(err)
+	}
+	bytes = append(bytes, e.Buffer()...)
+
+	// -------- encode 2
+	e.Clean()
+	if err := e.Encode(12345); err != nil {
+		t.Fatal(err)
+	}
+	bytes = append(bytes, e.Buffer()...)
+
+	// -------- encode 3
+	e.Clean()
+	if err := e.Encode(loc); err != nil {
+		t.Fatal(err)
+	}
+	bytes = append(bytes, e.Buffer()...)
+
+	// -------- encode 4
+	e.Clean()
+	if err := e.Encode("hello"); err != nil {
+		t.Fatal(err)
+	}
+	bytes = append(bytes, e.Buffer()...)
+
+	// demo a decoder to decode buffer from client
+	d := hessian.NewDecoder(bytes)
+
+	// -------- decode 1
+	res, err := d.Decode()
+	if err != nil {
+		t.Fatal(err)
+	}
+	assert.True(t, reflect.DeepEqual(u, res))
+
+	// -------- decode 2
+	d.Clean()

Review comment:
       Why is' clean 'called once per decode.
   If you have a complex object type, such as an object with multiple instance arrays,  after the first instance is decoded, `clean` is called, will the decoding of subsequent instances fail?




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@dubbo.apache.org
For additional commands, e-mail: notifications-help@dubbo.apache.org