2017-06-01 14:11:52 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2017-06-01 14:11:52 +03:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
"github.com/micro/go-micro/registry/memory"
|
2017-06-01 14:11:52 +03:00
|
|
|
proto "github.com/micro/go-micro/server/debug/proto"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFunction(t *testing.T) {
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(1)
|
|
|
|
|
2019-01-14 18:27:25 +03:00
|
|
|
r := memory.NewRegistry()
|
|
|
|
r.(*memory.Registry).Setup()
|
|
|
|
|
2017-06-01 14:11:52 +03:00
|
|
|
// create service
|
|
|
|
fn := NewFunction(
|
2019-01-14 18:27:25 +03:00
|
|
|
Registry(r),
|
2018-05-28 17:40:28 +03:00
|
|
|
Name("test.function"),
|
2017-06-01 14:11:52 +03:00
|
|
|
AfterStart(func() error {
|
|
|
|
wg.Done()
|
|
|
|
return nil
|
|
|
|
}),
|
|
|
|
)
|
|
|
|
|
|
|
|
// we can't test fn.Init as it parses the command line
|
|
|
|
// fn.Init()
|
|
|
|
|
2018-05-28 17:40:28 +03:00
|
|
|
ch := make(chan error, 2)
|
|
|
|
|
2017-06-01 14:11:52 +03:00
|
|
|
go func() {
|
2018-05-28 17:40:28 +03:00
|
|
|
// run service
|
|
|
|
ch <- fn.Run()
|
2017-06-01 14:11:52 +03:00
|
|
|
}()
|
|
|
|
|
2018-05-28 17:40:28 +03:00
|
|
|
// wait for start
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// test call debug
|
|
|
|
req := fn.Client().NewRequest(
|
|
|
|
"test.function",
|
|
|
|
"Debug.Health",
|
|
|
|
new(proto.HealthRequest),
|
|
|
|
)
|
|
|
|
|
|
|
|
rsp := new(proto.HealthResponse)
|
|
|
|
|
|
|
|
err := fn.Client().Call(context.TODO(), req, rsp)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rsp.Status != "ok" {
|
|
|
|
t.Fatalf("function response: %s", rsp.Status)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := <-ch; err != nil {
|
2018-05-25 16:39:50 +03:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-06-01 14:11:52 +03:00
|
|
|
}
|