github.com/annwntech/go-micro/v2@v2.9.5/function_test.go (about)

     1  package micro
     2  
     3  import (
     4  	"context"
     5  	"sync"
     6  	"testing"
     7  
     8  	proto "github.com/annwntech/go-micro/v2/debug/service/proto"
     9  	"github.com/annwntech/go-micro/v2/registry/memory"
    10  	"github.com/annwntech/go-micro/v2/util/test"
    11  )
    12  
    13  func TestFunction(t *testing.T) {
    14  	var wg sync.WaitGroup
    15  	wg.Add(1)
    16  
    17  	r := memory.NewRegistry(memory.Services(test.Data))
    18  
    19  	// create service
    20  	fn := NewFunction(
    21  		Registry(r),
    22  		Name("test.function"),
    23  		AfterStart(func() error {
    24  			wg.Done()
    25  			return nil
    26  		}),
    27  	)
    28  
    29  	// we can't test fn.Init as it parses the command line
    30  	// fn.Init()
    31  
    32  	ch := make(chan error, 2)
    33  
    34  	go func() {
    35  		// run service
    36  		ch <- fn.Run()
    37  	}()
    38  
    39  	// wait for start
    40  	wg.Wait()
    41  
    42  	// test call debug
    43  	req := fn.Client().NewRequest(
    44  		"test.function",
    45  		"Debug.Health",
    46  		new(proto.HealthRequest),
    47  	)
    48  
    49  	rsp := new(proto.HealthResponse)
    50  
    51  	err := fn.Client().Call(context.TODO(), req, rsp)
    52  	if err != nil {
    53  		t.Fatal(err)
    54  	}
    55  
    56  	if rsp.Status != "ok" {
    57  		t.Fatalf("function response: %s", rsp.Status)
    58  	}
    59  
    60  	if err := <-ch; err != nil {
    61  		t.Fatal(err)
    62  	}
    63  }