2017-01-07 14:52:23 +00:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
2018-03-03 11:53:52 +00:00
|
|
|
"context"
|
2017-01-07 14:52:23 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/micro/go-micro/registry/mock"
|
|
|
|
proto "github.com/micro/go-micro/server/debug/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestService(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
|
|
|
|
// cancellation context
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
// create service
|
|
|
|
service := NewService(
|
|
|
|
Name("test.service"),
|
|
|
|
Context(ctx),
|
|
|
|
Registry(mock.NewRegistry()),
|
|
|
|
AfterStart(func() error {
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
// we can't test service.Init as it parses the command line
|
|
|
|
// service.Init()
|
|
|
|
|
2018-03-13 18:50:58 +00:00
|
|
|
// run service
|
2018-05-25 14:39:50 +01:00
|
|
|
go func() {
|
|
|
|
// wait for start
|
|
|
|
wg.Wait()
|
2018-03-13 18:12:42 +07:00
|
|
|
|
2018-05-25 14:39:50 +01:00
|
|
|
// test call debug
|
|
|
|
req := service.Client().NewRequest(
|
|
|
|
"test.service",
|
|
|
|
"Debug.Health",
|
|
|
|
new(proto.HealthRequest),
|
|
|
|
)
|
2018-03-13 18:12:42 +07:00
|
|
|
|
2018-05-25 14:39:50 +01:00
|
|
|
rsp := new(proto.HealthResponse)
|
2017-01-07 14:52:23 +00:00
|
|
|
|
2018-05-25 14:39:50 +01:00
|
|
|
err := service.Client().Call(context.TODO(), req, rsp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-07 14:52:23 +00:00
|
|
|
|
2018-05-25 14:39:50 +01:00
|
|
|
if rsp.Status != "ok" {
|
|
|
|
t.Fatalf("service response: %s", rsp.Status)
|
|
|
|
}
|
2017-01-07 14:52:23 +00:00
|
|
|
|
2018-05-25 14:39:50 +01:00
|
|
|
// shutdown the service
|
|
|
|
cancel()
|
|
|
|
}()
|
2017-01-07 14:52:23 +00:00
|
|
|
|
2018-05-25 14:39:50 +01:00
|
|
|
if err := service.Run(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-01-07 14:52:23 +00:00
|
|
|
}
|