From 2a6c0a1f5c99d2a31c8ea5d83d526007c5be6d5f Mon Sep 17 00:00:00 2001
From: cvictory <shenglicao2@gmail.com>
Date: Wed, 16 Sep 2020 20:53:50 +0800
Subject: [PATCH] fix nacos test

---
 metadata/report/nacos/report_test.go     | 11 ++++++++++-
 registry/nacos/registry_test.go          | 15 ++++++++++++---
 registry/nacos/service_discovery_test.go |  3 +--
 3 files changed, 23 insertions(+), 6 deletions(-)

diff --git a/metadata/report/nacos/report_test.go b/metadata/report/nacos/report_test.go
index 81432ff2a..1651f72e5 100644
--- a/metadata/report/nacos/report_test.go
+++ b/metadata/report/nacos/report_test.go
@@ -22,6 +22,7 @@ import (
 	"net/http"
 	"strconv"
 	"testing"
+	"time"
 )
 
 import (
@@ -37,7 +38,7 @@ import (
 )
 
 func TestNacosMetadataReport_CRUD(t *testing.T) {
-	if _, err := http.Get("http://console.nacos.io/nacos/"); err != nil {
+	if !checkNacosServerAlive() {
 		return
 	}
 	rpt := newTestReport()
@@ -118,3 +119,11 @@ func newTestReport() report.MetadataReport {
 	res := extension.GetMetadataReportFactory("nacos").CreateMetadataReport(&regurl)
 	return res
 }
+
+func checkNacosServerAlive() bool {
+	c := http.Client{Timeout: time.Second}
+	if _, err := c.Get("http://console.nacos.io/nacos/"); err != nil {
+		return false
+	}
+	return true
+}
diff --git a/registry/nacos/registry_test.go b/registry/nacos/registry_test.go
index a07b41941..473733b44 100644
--- a/registry/nacos/registry_test.go
+++ b/registry/nacos/registry_test.go
@@ -23,6 +23,7 @@ import (
 	"net/url"
 	"strconv"
 	"testing"
+	"time"
 )
 
 import (
@@ -36,7 +37,7 @@ import (
 )
 
 func TestNacosRegistry_Register(t *testing.T) {
-	if _, err := http.Get("http://console.nacos.io/nacos/"); err != nil {
+	if !checkNacosServerAlive() {
 		return
 	}
 	regurl, _ := common.NewURL("registry://console.nacos.io:80", common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER)))
@@ -68,7 +69,7 @@ func TestNacosRegistry_Register(t *testing.T) {
 }
 
 func TestNacosRegistry_Subscribe(t *testing.T) {
-	if _, err := http.Get("http://console.nacos.io/nacos/"); err != nil {
+	if !checkNacosServerAlive() {
 		return
 	}
 	regurl, _ := common.NewURL("registry://console.nacos.io:80", common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER)))
@@ -109,7 +110,7 @@ func TestNacosRegistry_Subscribe(t *testing.T) {
 }
 
 func TestNacosRegistry_Subscribe_del(t *testing.T) {
-	if _, err := http.Get("http://console.nacos.io/nacos/"); err != nil {
+	if !checkNacosServerAlive() {
 		return
 	}
 	regurl, _ := common.NewURL("registry://console.nacos.io:80", common.WithParamsValue(constant.ROLE_KEY, strconv.Itoa(common.PROVIDER)))
@@ -198,3 +199,11 @@ func TestNacosListener_Close(t *testing.T) {
 	_, err = listener.Next()
 	assert.NotNil(t, err)
 }
+
+func checkNacosServerAlive() bool {
+	c := http.Client{Timeout: time.Second}
+	if _, err := c.Get("http://console.nacos.io/nacos/"); err != nil {
+		return false
+	}
+	return true
+}
diff --git a/registry/nacos/service_discovery_test.go b/registry/nacos/service_discovery_test.go
index 0628e991a..3b09136d2 100644
--- a/registry/nacos/service_discovery_test.go
+++ b/registry/nacos/service_discovery_test.go
@@ -19,7 +19,6 @@ package nacos
 
 import (
 	"math/rand"
-	"net/http"
 	"strconv"
 	"testing"
 	"time"
@@ -82,7 +81,7 @@ func TestNacosServiceDiscovery_Destroy(t *testing.T) {
 }
 
 func TestNacosServiceDiscovery_CRUD(t *testing.T) {
-	if _, err := http.Get("http://console.nacos.io/nacos/"); err != nil {
+	if !checkNacosServerAlive() {
 		return
 	}
 	prepareData()
-- 
GitLab