Skip to content
Snippets Groups Projects
Commit 9632fd7f authored by xg.gao's avatar xg.gao
Browse files

update consul test

parent 7970243e
No related branches found
No related tags found
No related merge requests found
...@@ -143,7 +143,7 @@ func (suite *consulMetadataReportTestSuite) testGetServiceDefinition() { ...@@ -143,7 +143,7 @@ func (suite *consulMetadataReportTestSuite) testGetServiceDefinition() {
func test1(t *testing.T) { func test1(t *testing.T) {
consulAgent := consul.NewConsulAgent(t, 8500) consulAgent := consul.NewConsulAgent(t, 8500)
defer consulAgent.Close() defer consulAgent.Shutdown()
url := newProviderRegistryUrl("localhost", 8500) url := newProviderRegistryUrl("localhost", 8500)
mf := extension.GetMetadataReportFactory("consul") mf := extension.GetMetadataReportFactory("consul")
......
...@@ -148,7 +148,7 @@ func (suite *consulRegistryTestSuite) close() { ...@@ -148,7 +148,7 @@ func (suite *consulRegistryTestSuite) close() {
// register -> subscribe -> unregister // register -> subscribe -> unregister
func test1(t *testing.T) { func test1(t *testing.T) {
consulAgent := consul.NewConsulAgent(t, registryPort) consulAgent := consul.NewConsulAgent(t, registryPort)
defer consulAgent.Close() defer consulAgent.Shutdown()
server := newServer(providerHost, providerPort) server := newServer(providerHost, providerPort)
defer server.close() defer server.close()
...@@ -165,10 +165,10 @@ func test1(t *testing.T) { ...@@ -165,10 +165,10 @@ func test1(t *testing.T) {
suite.testListener(remoting.EventTypeDel) suite.testListener(remoting.EventTypeDel)
} }
// subscribe -> register // subscribe -> register -> unregister
func test2(t *testing.T) { func test2(t *testing.T) {
consulAgent := consul.NewConsulAgent(t, registryPort) consulAgent := consul.NewConsulAgent(t, registryPort)
defer consulAgent.Close() defer consulAgent.Shutdown()
server := newServer(providerHost, providerPort) server := newServer(providerHost, providerPort)
defer server.close() defer server.close()
...@@ -181,6 +181,8 @@ func test2(t *testing.T) { ...@@ -181,6 +181,8 @@ func test2(t *testing.T) {
suite.testNewProviderRegistry() suite.testNewProviderRegistry()
suite.testRegister() suite.testRegister()
suite.testListener(remoting.EventTypeAdd) suite.testListener(remoting.EventTypeAdd)
suite.testUnregister()
suite.testListener(remoting.EventTypeDel)
} }
func TestConsulRegistry(t *testing.T) { func TestConsulRegistry(t *testing.T) {
......
...@@ -18,8 +18,6 @@ ...@@ -18,8 +18,6 @@
package consul package consul
import ( import (
"io/ioutil"
"os"
"strconv" "strconv"
"testing" "testing"
) )
...@@ -30,35 +28,11 @@ import ( ...@@ -30,35 +28,11 @@ import (
// Consul agent, used for test, simulates // Consul agent, used for test, simulates
// an embedded consul server. // an embedded consul server.
type ConsulAgent struct { func NewConsulAgent(t *testing.T, port int) *agent.TestAgent {
dataDir string
testAgent *agent.TestAgent
}
func NewConsulAgent(t *testing.T, port int) *ConsulAgent {
dataDir, _ := ioutil.TempDir("./", "agent")
hcl := ` hcl := `
ports { ports {
http = ` + strconv.Itoa(port) + ` http = ` + strconv.Itoa(port) + `
} }
data_dir = "` + dataDir + `"
` `
testAgent := &agent.TestAgent{Name: t.Name(), DataDir: dataDir, HCL: hcl} return agent.NewTestAgent(t, hcl)
testAgent.Start(t)
consulAgent := &ConsulAgent{
dataDir: dataDir,
testAgent: testAgent,
}
return consulAgent
}
func (consulAgent *ConsulAgent) Close() error {
var err error
err = consulAgent.testAgent.Shutdown()
if err != nil {
return err
}
return os.RemoveAll(consulAgent.dataDir)
} }
...@@ -27,6 +27,6 @@ import ( ...@@ -27,6 +27,6 @@ import (
func TestNewConsulAgent(t *testing.T) { func TestNewConsulAgent(t *testing.T) {
consulAgent := NewConsulAgent(t, 8500) consulAgent := NewConsulAgent(t, 8500)
err := consulAgent.Close() err := consulAgent.Shutdown()
assert.NoError(t, err) assert.NoError(t, err)
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment