mirror of
https://github.com/go-micro/go-micro.git
synced 2024-12-24 10:07:04 +02:00
ab692ff590
memory registry can be used as fast inprocess registry, so mock data needs to be in tests only Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
64 lines
993 B
Go
64 lines
993 B
Go
package micro
|
|
|
|
import (
|
|
"context"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/micro/go-micro/registry/memory"
|
|
proto "github.com/micro/go-micro/server/debug/proto"
|
|
)
|
|
|
|
func TestFunction(t *testing.T) {
|
|
var wg sync.WaitGroup
|
|
wg.Add(1)
|
|
|
|
r := memory.NewRegistry()
|
|
r.(*memory.Registry).Services = testData
|
|
|
|
// create service
|
|
fn := NewFunction(
|
|
Registry(r),
|
|
Name("test.function"),
|
|
AfterStart(func() error {
|
|
wg.Done()
|
|
return nil
|
|
}),
|
|
)
|
|
|
|
// we can't test fn.Init as it parses the command line
|
|
// fn.Init()
|
|
|
|
ch := make(chan error, 2)
|
|
|
|
go func() {
|
|
// run service
|
|
ch <- fn.Run()
|
|
}()
|
|
|
|
// 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 {
|
|
t.Fatal(err)
|
|
}
|
|
}
|