You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
2.1 KiB
Go
122 lines
2.1 KiB
Go
package components
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/urfave/cli/v2"
|
|
"go-micro.dev/v4"
|
|
)
|
|
|
|
type TestHandler struct {
|
|
initialized bool
|
|
name string
|
|
priority int
|
|
TimeInit time.Time
|
|
TimeStop time.Time
|
|
}
|
|
|
|
func newTestHandler(name string, priority int) *TestHandler {
|
|
return &TestHandler{name: name, priority: priority}
|
|
}
|
|
|
|
func (h *TestHandler) Name() string {
|
|
return h.name
|
|
}
|
|
|
|
func (h *TestHandler) Priority() int {
|
|
return h.priority
|
|
}
|
|
|
|
func (h *TestHandler) Initialized() bool {
|
|
return h.initialized
|
|
}
|
|
|
|
func (h *TestHandler) Init(components *Registry, cli *cli.Context) error {
|
|
if h.initialized {
|
|
return errors.New("already initialized")
|
|
}
|
|
|
|
h.TimeInit = time.Now()
|
|
|
|
h.initialized = true
|
|
return nil
|
|
}
|
|
|
|
func (h *TestHandler) Stop() error {
|
|
if !h.initialized {
|
|
return errors.New("not initialized")
|
|
}
|
|
|
|
h.TimeStop = time.Now()
|
|
|
|
h.initialized = false
|
|
return nil
|
|
}
|
|
|
|
func (h *TestHandler) Flags(r *Registry) []cli.Flag {
|
|
return []cli.Flag{}
|
|
}
|
|
|
|
func (h *TestHandler) Health(context context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
func TestInitSorting(t *testing.T) {
|
|
h1 := newTestHandler("Test1", 1)
|
|
h2 := newTestHandler("Test2", 5)
|
|
|
|
service := micro.NewService()
|
|
cReg := New(service, "test", h2, h1)
|
|
|
|
cli := &cli.Context{}
|
|
if err := cReg.Init(cli); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if h2.TimeInit.Before(h1.TimeInit) {
|
|
t.Error("Test has been initialized before Test1")
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestStopBeforeInit(t *testing.T) {
|
|
h1 := newTestHandler("Test1", 1)
|
|
h2 := newTestHandler("Test2", 5)
|
|
|
|
service := micro.NewService()
|
|
cReg := New(service, "test", h1, h2)
|
|
|
|
if err := cReg.Stop(); err == nil {
|
|
t.Error("Stop must fail if there was no init before")
|
|
return
|
|
}
|
|
}
|
|
|
|
func TestStopSorting(t *testing.T) {
|
|
h1 := newTestHandler("Test1", 1)
|
|
h2 := newTestHandler("Test2", 5)
|
|
|
|
service := micro.NewService()
|
|
cReg := New(service, "test", h2, h1)
|
|
|
|
cli := &cli.Context{}
|
|
if err := cReg.Init(cli); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if err := cReg.Stop(); err != nil {
|
|
t.Error(err)
|
|
return
|
|
}
|
|
|
|
if h1.TimeStop.Before(h2.TimeStop) {
|
|
t.Error("Test has been initialized before Test1")
|
|
return
|
|
}
|
|
}
|