Skip to content

Commit

Permalink
Merge pull request #2639 from jonyangx/issue2638
Browse files Browse the repository at this point in the history
[ISSUE #2638] refactor go sdk id module test
  • Loading branch information
xwm1992 authored Dec 26, 2022
2 parents 0d26e39 + 4279411 commit d505742
Show file tree
Hide file tree
Showing 5 changed files with 230 additions and 36 deletions.
59 changes: 29 additions & 30 deletions eventmesh-sdk-go/common/id/id_snake_test.go
Original file line number Diff line number Diff line change
@@ -1,37 +1,38 @@
// 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 id

import (
"errors"
"github.com/agiledragon/gomonkey/v2"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/sony/sonyflake"
"net"
"reflect"
"strconv"
"strings"
"testing"

"github.com/agiledragon/gomonkey/v2"
. "github.com/smartystreets/goconvey/convey"
"github.com/sony/sonyflake"
)

func TestInit(t *testing.T) {
Convey("TestInit", t, func() {
var _ = Describe("id_snake test", func() {

Convey("mock GetNetInterfaces", func() {
Context("NewFlake() exception test ", func() {
It("should get error", func() {
mockPatches := gomonkey.ApplyFunc(GetNetInterfaces, func() ([]net.Interface, error) {
return nil, errors.New("test error")
})
Expand All @@ -40,21 +41,18 @@ func TestInit(t *testing.T) {
defer func() {
if err := recover(); err != nil {
ret = err.(error).Error()
Ω(ret).To(Equal(want))
}
mockPatches.Reset()
}()

_ = NewFlake()
So(ret, ShouldEqual, want)
})
})

}

func TestNext(t *testing.T) {
Convey("TestNext", t, func() {
Context("NextID() exception test ", func() {

Convey("mock flake.sf.NextID()", func() {
It("should get error", func() {
macAddr := getMacAddr()
st := sonyflake.Settings{
MachineID: func() (uint16, error) {
Expand All @@ -77,12 +75,13 @@ func TestNext(t *testing.T) {
defer func() {
if err := recover(); err != nil {
ret = err.(error).Error()
Ω(ret).To(Equal(want))
}
mockPatches.Reset()
}()

_ = flake.Next()
So(ret, ShouldEqual, want)
})
})
}

})
30 changes: 30 additions & 0 deletions eventmesh-sdk-go/common/id/id_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* 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 id

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestIdAPIs(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "id module Tests")
}
50 changes: 50 additions & 0 deletions eventmesh-sdk-go/common/id/id_uuid_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/**
* 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 id

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

var _ = Describe("uuid test", func() {

var u Interface

BeforeEach(func() {
u = NewUUID()

})

Context("construct uuid ", func() {

It("should not nil", func() {
Ω(u).To(Not(BeNil()))
})
})

Context("next uuid ", func() {

It("should not nil", func() {
n := u.Next()
Ω(n).To(Not(BeNil()))
Ω(len(n)).To(Equal(32))
})
})

})
2 changes: 2 additions & 0 deletions eventmesh-sdk-go/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ require (
github.com/cloudevents/sdk-go/v2 v2.6.0
github.com/google/uuid v1.3.0
github.com/json-iterator/go v1.1.10
github.com/onsi/ginkgo v1.16.5
github.com/onsi/gomega v1.24.2
github.com/panjf2000/ants v1.3.0
github.com/smartystreets/goconvey v1.7.2
github.com/sony/sonyflake v1.0.0
Expand Down
Loading

0 comments on commit d505742

Please sign in to comment.