diff --git a/nacos/v2/nacos_test.go b/nacos/v2/nacos_test.go index 25efbd3..64ce67b 100644 --- a/nacos/v2/nacos_test.go +++ b/nacos/v2/nacos_test.go @@ -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, ®istry.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)) +}