Skip to content

Commit

Permalink
fix format & bug
Browse files Browse the repository at this point in the history
  • Loading branch information
smx-Morgan committed Oct 22, 2024
1 parent 24b1986 commit 246eeba
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions nacos/v2/nacos_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,49 @@ func TestCompareMaps(t *testing.T) {
}
}
}

// TestHertzAppWithNacosRegistry test a client call a hertz app with NacosRegistry
func TestHertzAppWithNacosRegistry(t *testing.T) {
namingClient = getNamingClient()
register := NewNacosRegistry(namingClient)
address := "127.0.0.1:4576"
srvName := "d.h.t"
var opts []config.Option
opts = append(opts, server.WithHostPorts(address), server.WithExitWaitTime(2*time.Second))
opts = append(opts, server.WithRegistry(register, &registry.Info{
ServiceName: srvName,
Addr: utils.NewNetAddr("tcp", address),
Weight: 10,
Tags: nil,
}))
// run a hertz app,registry src info into NacosRegistry
srv := server.New(opts...)
srv.GET("/ping", func(c context.Context, ctx *app.RequestContext) {
ctx.String(200, "pong")
})
go srv.Spin()
// Because delayed registration, we need sleep more time.
time.Sleep(2 * time.Second)

// client call an url, with NacosResolver
newClient, _ := client.NewClient()
resolver := NewNacosResolver(namingClient)
newClient.Use(sd.Discovery(resolver))

status, body, err := newClient.Get(context.TODO(), nil, "http://d.h.t/ping",
config.WithSD(true))
assert.Nil(t, err)
assert.Equal(t, 200, status)
assert.Equal(t, "pong", string(body))

ctx, cancelFunc := context.WithTimeout(context.Background(), 5*time.Second)
defer cancelFunc()
srv.Shutdown(ctx) //nolint:errcheck // ignore error

time.Sleep(5 * time.Second)
status, body, err = newClient.Get(context.Background(), nil, "http://d.h.t/ping",
config.WithSD(true))
assert.NotNil(t, err)
assert.Equal(t, 0, status)
assert.Equal(t, "", string(body))
}

0 comments on commit 246eeba

Please sign in to comment.