mirror of
https://github.com/go-kratos/kratos.git
synced 2026-05-22 10:15:24 +02:00
@@ -31,6 +31,7 @@ type App struct {
|
|||||||
opts options
|
opts options
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel func()
|
cancel func()
|
||||||
|
lk sync.Mutex
|
||||||
instance *registry.ServiceInstance
|
instance *registry.ServiceInstance
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,7 +105,9 @@ func (a *App) Run() error {
|
|||||||
if err := a.opts.registrar.Register(rctx, instance); err != nil {
|
if err := a.opts.registrar.Register(rctx, instance); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
a.lk.Lock()
|
||||||
a.instance = instance
|
a.instance = instance
|
||||||
|
a.lk.Unlock()
|
||||||
}
|
}
|
||||||
c := make(chan os.Signal, 1)
|
c := make(chan os.Signal, 1)
|
||||||
signal.Notify(c, a.opts.sigs...)
|
signal.Notify(c, a.opts.sigs...)
|
||||||
@@ -130,10 +133,13 @@ func (a *App) Run() error {
|
|||||||
|
|
||||||
// Stop gracefully stops the application.
|
// Stop gracefully stops the application.
|
||||||
func (a *App) Stop() error {
|
func (a *App) Stop() error {
|
||||||
if a.opts.registrar != nil && a.instance != nil {
|
a.lk.Lock()
|
||||||
|
instance := a.instance
|
||||||
|
a.lk.Unlock()
|
||||||
|
if a.opts.registrar != nil && instance != nil {
|
||||||
ctx, cancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
|
ctx, cancel := context.WithTimeout(a.opts.ctx, a.opts.registrarTimeout)
|
||||||
defer cancel()
|
defer cancel()
|
||||||
if err := a.opts.registrar.Deregister(ctx, a.instance); err != nil {
|
if err := a.opts.registrar.Deregister(ctx, instance); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
+29
@@ -2,7 +2,9 @@ package kratos
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -12,6 +14,32 @@ import (
|
|||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type mockRegistry struct {
|
||||||
|
lk sync.Mutex
|
||||||
|
service map[string]*registry.ServiceInstance
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *mockRegistry) Register(ctx context.Context, service *registry.ServiceInstance) error {
|
||||||
|
if service == nil || service.ID == "" {
|
||||||
|
return fmt.Errorf("no service id")
|
||||||
|
}
|
||||||
|
r.lk.Lock()
|
||||||
|
defer r.lk.Unlock()
|
||||||
|
r.service[service.ID] = service
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Deregister the registration.
|
||||||
|
func (r *mockRegistry) Deregister(ctx context.Context, service *registry.ServiceInstance) error {
|
||||||
|
r.lk.Lock()
|
||||||
|
defer r.lk.Unlock()
|
||||||
|
if r.service[service.ID] == nil {
|
||||||
|
return fmt.Errorf("deregister service not found")
|
||||||
|
}
|
||||||
|
delete(r.service, service.ID)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func TestApp(t *testing.T) {
|
func TestApp(t *testing.T) {
|
||||||
hs := http.NewServer()
|
hs := http.NewServer()
|
||||||
gs := grpc.NewServer()
|
gs := grpc.NewServer()
|
||||||
@@ -19,6 +47,7 @@ func TestApp(t *testing.T) {
|
|||||||
Name("kratos"),
|
Name("kratos"),
|
||||||
Version("v1.0.0"),
|
Version("v1.0.0"),
|
||||||
Server(hs, gs),
|
Server(hs, gs),
|
||||||
|
Registrar(&mockRegistry{service: make(map[string]*registry.ServiceInstance)}),
|
||||||
)
|
)
|
||||||
time.AfterFunc(time.Second, func() {
|
time.AfterFunc(time.Second, func() {
|
||||||
_ = app.Stop()
|
_ = app.Stop()
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package transport
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
// mockTransport is a gRPC transport.
|
||||||
|
type mockTransport struct {
|
||||||
|
endpoint string
|
||||||
|
operation string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Kind returns the transport kind.
|
||||||
|
func (tr *mockTransport) Kind() Kind {
|
||||||
|
return KindGRPC
|
||||||
|
}
|
||||||
|
|
||||||
|
// Endpoint returns the transport endpoint.
|
||||||
|
func (tr *mockTransport) Endpoint() string {
|
||||||
|
return tr.endpoint
|
||||||
|
}
|
||||||
|
|
||||||
|
// Operation returns the transport operation.
|
||||||
|
func (tr *mockTransport) Operation() string {
|
||||||
|
return tr.operation
|
||||||
|
}
|
||||||
|
|
||||||
|
// RequestHeader returns the request header.
|
||||||
|
func (tr *mockTransport) RequestHeader() Header {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReplyHeader returns the reply header.
|
||||||
|
func (tr *mockTransport) ReplyHeader() Header {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestServerTransport(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
ctx = NewServerContext(ctx, &mockTransport{endpoint: "test_endpoint"})
|
||||||
|
tr, ok := FromServerContext(ctx)
|
||||||
|
|
||||||
|
assert.Equal(t, true, ok)
|
||||||
|
assert.NotNil(t, tr)
|
||||||
|
mtr, ok := tr.(*mockTransport)
|
||||||
|
assert.Equal(t, true, ok)
|
||||||
|
assert.NotNil(t, mtr)
|
||||||
|
assert.Equal(t, mtr.endpoint, "test_endpoint")
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestClientTransport(t *testing.T) {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
ctx = NewClientContext(ctx, &mockTransport{endpoint: "test_endpoint"})
|
||||||
|
tr, ok := FromClientContext(ctx)
|
||||||
|
|
||||||
|
assert.Equal(t, true, ok)
|
||||||
|
assert.NotNil(t, tr)
|
||||||
|
mtr, ok := tr.(*mockTransport)
|
||||||
|
assert.Equal(t, true, ok)
|
||||||
|
assert.NotNil(t, mtr)
|
||||||
|
assert.Equal(t, mtr.endpoint, "test_endpoint")
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user