2024-02-27 23:35:49 +03:00
|
|
|
package micro
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"go.unistack.org/micro/v3/broker"
|
|
|
|
"go.unistack.org/micro/v3/fsm"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAs(t *testing.T) {
|
|
|
|
var b *bro
|
|
|
|
broTarget := &bro{name: "kafka"}
|
|
|
|
fsmTarget := &fsmT{name: "fsm"}
|
|
|
|
|
|
|
|
testCases := []struct {
|
|
|
|
b any
|
|
|
|
target any
|
|
|
|
want any
|
2024-12-09 13:06:43 +03:00
|
|
|
|
|
|
|
match bool
|
2024-02-27 23:35:49 +03:00
|
|
|
}{
|
|
|
|
{
|
2024-12-09 13:06:43 +03:00
|
|
|
b: broTarget,
|
|
|
|
target: &b,
|
|
|
|
match: true,
|
|
|
|
want: broTarget,
|
2024-02-27 23:35:49 +03:00
|
|
|
},
|
|
|
|
{
|
2024-12-09 13:06:43 +03:00
|
|
|
b: nil,
|
|
|
|
target: &b,
|
|
|
|
match: false,
|
|
|
|
want: nil,
|
2024-02-27 23:35:49 +03:00
|
|
|
},
|
|
|
|
{
|
2024-12-09 13:06:43 +03:00
|
|
|
b: fsmTarget,
|
|
|
|
target: &b,
|
|
|
|
match: false,
|
|
|
|
want: nil,
|
2024-02-27 23:35:49 +03:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for i, tc := range testCases {
|
|
|
|
name := fmt.Sprintf("%d:As(Errorf(..., %v), %v)", i, tc.b, tc.target)
|
|
|
|
// Clear the target pointer, in case it was set in a previous test.
|
|
|
|
rtarget := reflect.ValueOf(tc.target)
|
|
|
|
rtarget.Elem().Set(reflect.Zero(reflect.TypeOf(tc.target).Elem()))
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
match := As(tc.b, tc.target)
|
|
|
|
if match != tc.match {
|
|
|
|
t.Fatalf("match: got %v; want %v", match, tc.match)
|
|
|
|
}
|
|
|
|
if !match {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got := rtarget.Elem().Interface(); got != tc.want {
|
|
|
|
t.Fatalf("got %#v, want %#v", got, tc.want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type bro struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *bro) Name() string { return p.name }
|
|
|
|
|
2024-12-02 13:20:13 +03:00
|
|
|
func (p *bro) Live() bool { return true }
|
|
|
|
|
|
|
|
func (p *bro) Ready() bool { return true }
|
|
|
|
|
|
|
|
func (p *bro) Health() bool { return true }
|
|
|
|
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) Init(_ ...broker.Option) error { return nil }
|
2024-02-27 23:35:49 +03:00
|
|
|
|
|
|
|
// Options returns broker options
|
|
|
|
func (p *bro) Options() broker.Options { return broker.Options{} }
|
|
|
|
|
|
|
|
// Address return configured address
|
|
|
|
func (p *bro) Address() string { return "" }
|
|
|
|
|
|
|
|
// Connect connects to broker
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) Connect(_ context.Context) error { return nil }
|
2024-02-27 23:35:49 +03:00
|
|
|
|
|
|
|
// Disconnect disconnect from broker
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) Disconnect(_ context.Context) error { return nil }
|
2024-02-27 23:35:49 +03:00
|
|
|
|
|
|
|
// Publish message, msg can be single broker.Message or []broker.Message
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) Publish(_ context.Context, _ string, _ *broker.Message, _ ...broker.PublishOption) error {
|
2024-02-27 23:35:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BatchPublish messages to broker with multiple topics
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) BatchPublish(_ context.Context, _ []*broker.Message, _ ...broker.PublishOption) error {
|
2024-02-27 23:35:49 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BatchSubscribe subscribes to topic messages via handler
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) BatchSubscribe(_ context.Context, _ string, _ broker.BatchHandler, _ ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2024-02-27 23:35:49 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subscribe subscribes to topic message via handler
|
2024-12-09 13:06:43 +03:00
|
|
|
func (p *bro) Subscribe(_ context.Context, _ string, _ broker.Handler, _ ...broker.SubscribeOption) (broker.Subscriber, error) {
|
2024-02-27 23:35:49 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String type of broker
|
|
|
|
func (p *bro) String() string { return p.name }
|
|
|
|
|
|
|
|
type fsmT struct {
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2024-12-09 13:06:43 +03:00
|
|
|
func (f *fsmT) Start(_ context.Context, _ interface{}, _ ...Option) (interface{}, error) {
|
2024-02-27 23:35:49 +03:00
|
|
|
return nil, nil
|
|
|
|
}
|
2024-12-09 13:06:43 +03:00
|
|
|
func (f *fsmT) Current() string { return f.name }
|
|
|
|
func (f *fsmT) Reset() {}
|
|
|
|
func (f *fsmT) State(_ string, _ fsm.StateFunc) {}
|