Skip to content

Commit

Permalink
feat:add service unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
geebytes committed May 24, 2024
1 parent 4bb5f81 commit f8e39d2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
39 changes: 38 additions & 1 deletion gateway/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func testRegisterClient(t *testing.T) {
go example.Run(helloAddr)
time.Sleep(2 * time.Second)
go gw.Start()
time.Sleep(1 * time.Second)
time.Sleep(2 * time.Second)
f := func() {
gw.Start()
}
Expand Down Expand Up @@ -1088,6 +1088,41 @@ func testUpdateLoadbalance(t *testing.T) {
}
})
}
func testStartErr(t *testing.T) {
c.Convey("test start error", t, func() {
_, filename, _, _ := runtime.Caller(0)
pbFile := filepath.Join(filepath.Dir(filepath.Dir(filename)), "testdata", "helloworld.pb")
pb, err := os.ReadFile(pbFile)
c.So(err, c.ShouldBeNil)
pd, err := NewDescriptionFromBinary(pb, filepath.Join("tmp", "test-pd"))
c.So(err, c.ShouldBeNil)
opts, cnf := newTestServer(0, 0)
localGW := NewGateway(cnf, opts)
err = localGW.RegisterHandlerClient(context.Background(), pd)
c.So(err, c.ShouldBeNil)

c.So(localGW.Start, c.ShouldPanic)


min := 1949
max := 12138
rander := rand.New(rand.NewSource(time.Now().Unix())) // 初始化随机数种子

randomNumber := rander.Intn(max-min+1) + min
opts1, cnf1 := newTestServer(randomNumber+3, randomNumber)
localGW1 := NewGateway(cnf1, opts1)
err = localGW1.RegisterHandlerClient(context.Background(), pd)
c.So(err, c.ShouldBeNil)
go localGW1.Start()
time.Sleep(3 * time.Second)
opts2, cnf2 := newTestServer(gwPort, randomNumber+4)
localGW2 := NewGateway(cnf2, opts2)
err = localGW2.RegisterHandlerClient(context.Background(), pd)
c.So(err, c.ShouldBeNil)
c.So(localGW2.Start,c.ShouldPanic)

})
}
func TestHttp(t *testing.T) {
t.Run("testRegisterClient", testRegisterClient)
t.Run("testRequestGet", testRequestGet)
Expand All @@ -1108,5 +1143,7 @@ func TestHttp(t *testing.T) {
t.Run("testLoadHttpEndpointItemErr", testLoadHttpEndpointItemErr)
t.Run("testDeleteEndpoint", testDeleteEndpoint)
t.Run("testRegisterLocalService", testRegisterLocalService)
t.Run("testStartErr", testStartErr)

// time.Sleep(30 * time.Second)
}
6 changes: 6 additions & 0 deletions gateway/mask_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,11 @@ func TestDecodeErr(t *testing.T) {
c.So(err, c.ShouldNotBeNil)
c.So(err.Error(), c.ShouldContainSubstring, caseV.err.Error())
}
r := bytes.NewReader([]byte(`{"message":"John Doe","msg":{"msg":"hello world"},"allow":"DENY","repeated_msg":[{"msg":"John Doe"}]}`))
decoder := NewMaskDecoder(NewJsonDecoder(r))
mapData:=make(map[string]interface{})
err := decoder.Decode(mapData)
c.So(err, c.ShouldBeNil)
c.So(len(mapData), c.ShouldEqual, 0)
})
}

0 comments on commit f8e39d2

Please sign in to comment.