rename mock things to memory
This commit is contained in:
parent
c17d0fcc0f
commit
39c24baca9
@ -6,12 +6,20 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/micro/go-micro/registry/mock"
|
"github.com/micro/go-micro/registry/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func newTestRegistry() *memory.Registry {
|
||||||
|
r := memory.NewRegistry()
|
||||||
|
m := r.(*memory.Registry)
|
||||||
|
m.Setup()
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
func sub(be *testing.B, c int) {
|
func sub(be *testing.B, c int) {
|
||||||
be.StopTimer()
|
be.StopTimer()
|
||||||
m := mock.NewRegistry()
|
m := newTestRegistry()
|
||||||
|
|
||||||
b := NewBroker(Registry(m))
|
b := NewBroker(Registry(m))
|
||||||
topic := uuid.New().String()
|
topic := uuid.New().String()
|
||||||
|
|
||||||
@ -70,7 +78,7 @@ func sub(be *testing.B, c int) {
|
|||||||
|
|
||||||
func pub(be *testing.B, c int) {
|
func pub(be *testing.B, c int) {
|
||||||
be.StopTimer()
|
be.StopTimer()
|
||||||
m := mock.NewRegistry()
|
m := newTestRegistry()
|
||||||
b := NewBroker(Registry(m))
|
b := NewBroker(Registry(m))
|
||||||
topic := uuid.New().String()
|
topic := uuid.New().String()
|
||||||
|
|
||||||
@ -139,7 +147,7 @@ func pub(be *testing.B, c int) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestBroker(t *testing.T) {
|
func TestBroker(t *testing.T) {
|
||||||
m := mock.NewRegistry()
|
m := newTestRegistry()
|
||||||
b := NewBroker(Registry(m))
|
b := NewBroker(Registry(m))
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
if err := b.Init(); err != nil {
|
||||||
@ -186,7 +194,7 @@ func TestBroker(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConcurrentSubBroker(t *testing.T) {
|
func TestConcurrentSubBroker(t *testing.T) {
|
||||||
m := mock.NewRegistry()
|
m := newTestRegistry()
|
||||||
b := NewBroker(Registry(m))
|
b := NewBroker(Registry(m))
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
if err := b.Init(); err != nil {
|
||||||
@ -243,7 +251,7 @@ func TestConcurrentSubBroker(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TestConcurrentPubBroker(t *testing.T) {
|
func TestConcurrentPubBroker(t *testing.T) {
|
||||||
m := mock.NewRegistry()
|
m := newTestRegistry()
|
||||||
b := NewBroker(Registry(m))
|
b := NewBroker(Registry(m))
|
||||||
|
|
||||||
if err := b.Init(); err != nil {
|
if err := b.Init(); err != nil {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
// Package mock provides a mock broker for testing
|
// Package memory provides a memory broker
|
||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -9,20 +9,20 @@ import (
|
|||||||
"github.com/micro/go-micro/broker"
|
"github.com/micro/go-micro/broker"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockBroker struct {
|
type memoryBroker struct {
|
||||||
opts broker.Options
|
opts broker.Options
|
||||||
|
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
connected bool
|
connected bool
|
||||||
Subscribers map[string][]*mockSubscriber
|
Subscribers map[string][]*memorySubscriber
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockPublication struct {
|
type memoryPublication struct {
|
||||||
topic string
|
topic string
|
||||||
message *broker.Message
|
message *broker.Message
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockSubscriber struct {
|
type memorySubscriber struct {
|
||||||
id string
|
id string
|
||||||
topic string
|
topic string
|
||||||
exit chan bool
|
exit chan bool
|
||||||
@ -30,15 +30,15 @@ type mockSubscriber struct {
|
|||||||
opts broker.SubscribeOptions
|
opts broker.SubscribeOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Options() broker.Options {
|
func (m *memoryBroker) Options() broker.Options {
|
||||||
return m.opts
|
return m.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Address() string {
|
func (m *memoryBroker) Address() string {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Connect() error {
|
func (m *memoryBroker) Connect() error {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -51,7 +51,7 @@ func (m *mockBroker) Connect() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Disconnect() error {
|
func (m *memoryBroker) Disconnect() error {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -64,14 +64,14 @@ func (m *mockBroker) Disconnect() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Init(opts ...broker.Option) error {
|
func (m *memoryBroker) Init(opts ...broker.Option) error {
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&m.opts)
|
o(&m.opts)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Publish(topic string, message *broker.Message, opts ...broker.PublishOption) error {
|
func (m *memoryBroker) Publish(topic string, message *broker.Message, opts ...broker.PublishOption) error {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -84,7 +84,7 @@ func (m *mockBroker) Publish(topic string, message *broker.Message, opts ...brok
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
p := &mockPublication{
|
p := &memoryPublication{
|
||||||
topic: topic,
|
topic: topic,
|
||||||
message: message,
|
message: message,
|
||||||
}
|
}
|
||||||
@ -98,7 +98,7 @@ func (m *mockBroker) Publish(topic string, message *broker.Message, opts ...brok
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
func (m *memoryBroker) Subscribe(topic string, handler broker.Handler, opts ...broker.SubscribeOption) (broker.Subscriber, error) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -111,7 +111,7 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
sub := &mockSubscriber{
|
sub := &memorySubscriber{
|
||||||
exit: make(chan bool, 1),
|
exit: make(chan bool, 1),
|
||||||
id: uuid.New().String(),
|
id: uuid.New().String(),
|
||||||
topic: topic,
|
topic: topic,
|
||||||
@ -124,7 +124,7 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
|||||||
go func() {
|
go func() {
|
||||||
<-sub.exit
|
<-sub.exit
|
||||||
m.Lock()
|
m.Lock()
|
||||||
var newSubscribers []*mockSubscriber
|
var newSubscribers []*memorySubscriber
|
||||||
for _, sb := range m.Subscribers[topic] {
|
for _, sb := range m.Subscribers[topic] {
|
||||||
if sb.id == sub.id {
|
if sb.id == sub.id {
|
||||||
continue
|
continue
|
||||||
@ -138,31 +138,31 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
|
|||||||
return sub, nil
|
return sub, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockBroker) String() string {
|
func (m *memoryBroker) String() string {
|
||||||
return "mock"
|
return "memory"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockPublication) Topic() string {
|
func (m *memoryPublication) Topic() string {
|
||||||
return m.topic
|
return m.topic
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockPublication) Message() *broker.Message {
|
func (m *memoryPublication) Message() *broker.Message {
|
||||||
return m.message
|
return m.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockPublication) Ack() error {
|
func (m *memoryPublication) Ack() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockSubscriber) Options() broker.SubscribeOptions {
|
func (m *memorySubscriber) Options() broker.SubscribeOptions {
|
||||||
return m.opts
|
return m.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockSubscriber) Topic() string {
|
func (m *memorySubscriber) Topic() string {
|
||||||
return m.topic
|
return m.topic
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockSubscriber) Unsubscribe() error {
|
func (m *memorySubscriber) Unsubscribe() error {
|
||||||
m.exit <- true
|
m.exit <- true
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -173,8 +173,8 @@ func NewBroker(opts ...broker.Option) broker.Broker {
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &mockBroker{
|
return &memoryBroker{
|
||||||
opts: options,
|
opts: options,
|
||||||
Subscribers: make(map[string][]*mockSubscriber),
|
Subscribers: make(map[string][]*memorySubscriber),
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/micro/go-micro/broker"
|
"github.com/micro/go-micro/broker"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestBroker(t *testing.T) {
|
func TestMemoryBroker(t *testing.T) {
|
||||||
b := NewBroker()
|
b := NewBroker()
|
||||||
|
|
||||||
if err := b.Connect(); err != nil {
|
if err := b.Connect(); err != nil {
|
@ -7,10 +7,16 @@ import (
|
|||||||
|
|
||||||
"github.com/micro/go-micro/errors"
|
"github.com/micro/go-micro/errors"
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
"github.com/micro/go-micro/registry/mock"
|
"github.com/micro/go-micro/registry/memory"
|
||||||
"github.com/micro/go-micro/selector"
|
"github.com/micro/go-micro/selector"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func newTestRegistry() registry.Registry {
|
||||||
|
r := memory.NewRegistry()
|
||||||
|
r.(*memory.Registry).Setup()
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
func TestCallAddress(t *testing.T) {
|
func TestCallAddress(t *testing.T) {
|
||||||
var called bool
|
var called bool
|
||||||
service := "test.service"
|
service := "test.service"
|
||||||
@ -38,7 +44,7 @@ func TestCallAddress(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r := mock.NewRegistry()
|
r := newTestRegistry()
|
||||||
c := NewClient(
|
c := NewClient(
|
||||||
Registry(r),
|
Registry(r),
|
||||||
WrapCall(wrap),
|
WrapCall(wrap),
|
||||||
@ -77,7 +83,7 @@ func TestCallRetry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r := mock.NewRegistry()
|
r := newTestRegistry()
|
||||||
c := NewClient(
|
c := NewClient(
|
||||||
Registry(r),
|
Registry(r),
|
||||||
WrapCall(wrap),
|
WrapCall(wrap),
|
||||||
@ -127,7 +133,7 @@ func TestCallWrapper(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
r := mock.NewRegistry()
|
r := newTestRegistry()
|
||||||
c := NewClient(
|
c := NewClient(
|
||||||
Registry(r),
|
Registry(r),
|
||||||
WrapCall(wrap),
|
WrapCall(wrap),
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/micro/go-micro/transport"
|
"github.com/micro/go-micro/transport"
|
||||||
"github.com/micro/go-micro/transport/mock"
|
"github.com/micro/go-micro/transport/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
func testPool(t *testing.T, size int, ttl time.Duration) {
|
func testPool(t *testing.T, size int, ttl time.Duration) {
|
||||||
@ -13,7 +13,7 @@ func testPool(t *testing.T, size int, ttl time.Duration) {
|
|||||||
p := newPool(size, ttl)
|
p := newPool(size, ttl)
|
||||||
|
|
||||||
// mock transport
|
// mock transport
|
||||||
tr := mock.NewTransport()
|
tr := memory.NewTransport()
|
||||||
|
|
||||||
// listen
|
// listen
|
||||||
l, err := tr.Listen(":0")
|
l, err := tr.Listen(":0")
|
||||||
|
10
cmd/cmd.go
10
cmd/cmd.go
@ -17,12 +17,14 @@ import (
|
|||||||
// brokers
|
// brokers
|
||||||
"github.com/micro/go-micro/broker"
|
"github.com/micro/go-micro/broker"
|
||||||
"github.com/micro/go-micro/broker/http"
|
"github.com/micro/go-micro/broker/http"
|
||||||
|
"github.com/micro/go-micro/broker/memory"
|
||||||
|
|
||||||
// registries
|
// registries
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
"github.com/micro/go-micro/registry/consul"
|
"github.com/micro/go-micro/registry/consul"
|
||||||
"github.com/micro/go-micro/registry/gossip"
|
"github.com/micro/go-micro/registry/gossip"
|
||||||
"github.com/micro/go-micro/registry/mdns"
|
"github.com/micro/go-micro/registry/mdns"
|
||||||
|
rmem "github.com/micro/go-micro/registry/memory"
|
||||||
|
|
||||||
// selectors
|
// selectors
|
||||||
"github.com/micro/go-micro/selector"
|
"github.com/micro/go-micro/selector"
|
||||||
@ -32,6 +34,7 @@ import (
|
|||||||
// transports
|
// transports
|
||||||
"github.com/micro/go-micro/transport"
|
"github.com/micro/go-micro/transport"
|
||||||
thttp "github.com/micro/go-micro/transport/http"
|
thttp "github.com/micro/go-micro/transport/http"
|
||||||
|
tmem "github.com/micro/go-micro/transport/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Cmd interface {
|
type Cmd interface {
|
||||||
@ -164,7 +167,8 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
|
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
|
||||||
"http": http.NewBroker,
|
"http": http.NewBroker,
|
||||||
|
"memory": memory.NewBroker,
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultClients = map[string]func(...client.Option) client.Client{
|
DefaultClients = map[string]func(...client.Option) client.Client{
|
||||||
@ -175,6 +179,7 @@ var (
|
|||||||
"consul": consul.NewRegistry,
|
"consul": consul.NewRegistry,
|
||||||
"gossip": gossip.NewRegistry,
|
"gossip": gossip.NewRegistry,
|
||||||
"mdns": mdns.NewRegistry,
|
"mdns": mdns.NewRegistry,
|
||||||
|
"memory": rmem.NewRegistry,
|
||||||
}
|
}
|
||||||
|
|
||||||
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
|
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
|
||||||
@ -189,7 +194,8 @@ var (
|
|||||||
}
|
}
|
||||||
|
|
||||||
DefaultTransports = map[string]func(...transport.Option) transport.Transport{
|
DefaultTransports = map[string]func(...transport.Option) transport.Transport{
|
||||||
"http": thttp.NewTransport,
|
"memory": tmem.NewTransport,
|
||||||
|
"http": thttp.NewTransport,
|
||||||
}
|
}
|
||||||
|
|
||||||
// used for default selection as the fall back
|
// used for default selection as the fall back
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry/mock"
|
"github.com/micro/go-micro/registry/memory"
|
||||||
proto "github.com/micro/go-micro/server/debug/proto"
|
proto "github.com/micro/go-micro/server/debug/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -13,9 +13,12 @@ func TestFunction(t *testing.T) {
|
|||||||
var wg sync.WaitGroup
|
var wg sync.WaitGroup
|
||||||
wg.Add(1)
|
wg.Add(1)
|
||||||
|
|
||||||
|
r := memory.NewRegistry()
|
||||||
|
r.(*memory.Registry).Setup()
|
||||||
|
|
||||||
// create service
|
// create service
|
||||||
fn := NewFunction(
|
fn := NewFunction(
|
||||||
Registry(mock.NewRegistry()),
|
Registry(r),
|
||||||
Name("test.function"),
|
Name("test.function"),
|
||||||
AfterStart(func() error {
|
AfterStart(func() error {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
@ -1,4 +1,4 @@
|
|||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
@ -1,5 +1,5 @@
|
|||||||
// Package mock provides a mock registry for testing
|
// Package memory provides an in-memory registry
|
||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"sync"
|
"sync"
|
||||||
@ -7,13 +7,14 @@ import (
|
|||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockRegistry struct {
|
type Registry struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
Services map[string][]*registry.Service
|
Services map[string][]*registry.Service
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
mockData = map[string][]*registry.Service{
|
// mock data
|
||||||
|
Data = map[string][]*registry.Service{
|
||||||
"foo": []*registry.Service{
|
"foo": []*registry.Service{
|
||||||
{
|
{
|
||||||
Name: "foo",
|
Name: "foo",
|
||||||
@ -57,15 +58,16 @@ var (
|
|||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func (m *mockRegistry) init() {
|
// Setup sets mock data
|
||||||
|
func (m *Registry) Setup() {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
// add some mock data
|
// add some memory data
|
||||||
m.Services = mockData
|
m.Services = Data
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) {
|
func (m *Registry) GetService(service string) ([]*registry.Service, error) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -77,7 +79,7 @@ func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) ListServices() ([]*registry.Service, error) {
|
func (m *Registry) ListServices() ([]*registry.Service, error) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -88,7 +90,7 @@ func (m *mockRegistry) ListServices() ([]*registry.Service, error) {
|
|||||||
return services, nil
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
func (m *Registry) Register(s *registry.Service, opts ...registry.RegisterOption) error {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -97,7 +99,7 @@ func (m *mockRegistry) Register(s *registry.Service, opts ...registry.RegisterOp
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) Deregister(s *registry.Service) error {
|
func (m *Registry) Deregister(s *registry.Service) error {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -106,28 +108,28 @@ func (m *mockRegistry) Deregister(s *registry.Service) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
func (m *Registry) Watch(opts ...registry.WatchOption) (registry.Watcher, error) {
|
||||||
var wopts registry.WatchOptions
|
var wopts registry.WatchOptions
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&wopts)
|
o(&wopts)
|
||||||
}
|
}
|
||||||
return &mockWatcher{exit: make(chan bool), opts: wopts}, nil
|
return &memoryWatcher{exit: make(chan bool), opts: wopts}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) String() string {
|
func (m *Registry) String() string {
|
||||||
return "mock"
|
return "memory"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) Init(opts ...registry.Option) error {
|
func (m *Registry) Init(opts ...registry.Option) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockRegistry) Options() registry.Options {
|
func (m *Registry) Options() registry.Options {
|
||||||
return registry.Options{}
|
return registry.Options{}
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRegistry(opts ...registry.Options) registry.Registry {
|
func NewRegistry(opts ...registry.Option) registry.Registry {
|
||||||
m := &mockRegistry{Services: make(map[string][]*registry.Service)}
|
return &Registry{
|
||||||
m.init()
|
Services: make(map[string][]*registry.Service),
|
||||||
return m
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@ -82,6 +82,7 @@ var (
|
|||||||
|
|
||||||
func TestMockRegistry(t *testing.T) {
|
func TestMockRegistry(t *testing.T) {
|
||||||
m := NewRegistry()
|
m := NewRegistry()
|
||||||
|
m.(*Registry).Setup()
|
||||||
|
|
||||||
fn := func(k string, v []*registry.Service) {
|
fn := func(k string, v []*registry.Service) {
|
||||||
services, err := m.GetService(k)
|
services, err := m.GetService(k)
|
||||||
@ -107,8 +108,8 @@ func TestMockRegistry(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// test existing mock data
|
// test existing memory data
|
||||||
for k, v := range mockData {
|
for k, v := range Data {
|
||||||
fn(k, v)
|
fn(k, v)
|
||||||
}
|
}
|
||||||
|
|
@ -1,4 +1,4 @@
|
|||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -6,12 +6,12 @@ import (
|
|||||||
"github.com/micro/go-micro/registry"
|
"github.com/micro/go-micro/registry"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockWatcher struct {
|
type memoryWatcher struct {
|
||||||
exit chan bool
|
exit chan bool
|
||||||
opts registry.WatchOptions
|
opts registry.WatchOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockWatcher) Next() (*registry.Result, error) {
|
func (m *memoryWatcher) Next() (*registry.Result, error) {
|
||||||
// not implement so we just block until exit
|
// not implement so we just block until exit
|
||||||
select {
|
select {
|
||||||
case <-m.exit:
|
case <-m.exit:
|
||||||
@ -19,7 +19,7 @@ func (m *mockWatcher) Next() (*registry.Result, error) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockWatcher) Stop() {
|
func (m *memoryWatcher) Stop() {
|
||||||
select {
|
select {
|
||||||
case <-m.exit:
|
case <-m.exit:
|
||||||
return
|
return
|
@ -3,13 +3,15 @@ package selector
|
|||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry/mock"
|
"github.com/micro/go-micro/registry/memory"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestRegistrySelector(t *testing.T) {
|
func TestRegistrySelector(t *testing.T) {
|
||||||
counts := map[string]int{}
|
counts := map[string]int{}
|
||||||
|
|
||||||
cache := NewSelector(Registry(mock.NewRegistry()))
|
r := memory.NewRegistry()
|
||||||
|
r.(*memory.Registry).Setup()
|
||||||
|
cache := NewSelector(Registry(r))
|
||||||
|
|
||||||
next, err := cache.Select("foo")
|
next, err := cache.Select("foo")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -5,7 +5,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/micro/go-micro/registry/mock"
|
"github.com/micro/go-micro/registry/memory"
|
||||||
proto "github.com/micro/go-micro/server/debug/proto"
|
proto "github.com/micro/go-micro/server/debug/proto"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -16,11 +16,14 @@ func TestService(t *testing.T) {
|
|||||||
// cancellation context
|
// cancellation context
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
r := memory.NewRegistry()
|
||||||
|
r.(*memory.Registry).Setup()
|
||||||
|
|
||||||
// create service
|
// create service
|
||||||
service := NewService(
|
service := NewService(
|
||||||
Name("test.service"),
|
Name("test.service"),
|
||||||
Context(ctx),
|
Context(ctx),
|
||||||
Registry(mock.NewRegistry()),
|
Registry(r),
|
||||||
AfterStart(func() error {
|
AfterStart(func() error {
|
||||||
wg.Done()
|
wg.Done()
|
||||||
return nil
|
return nil
|
||||||
|
@ -1,4 +1,5 @@
|
|||||||
package mock
|
// Package memory is an in-memory transport
|
||||||
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
@ -11,7 +12,7 @@ import (
|
|||||||
"github.com/micro/go-micro/transport"
|
"github.com/micro/go-micro/transport"
|
||||||
)
|
)
|
||||||
|
|
||||||
type mockSocket struct {
|
type memorySocket struct {
|
||||||
recv chan *transport.Message
|
recv chan *transport.Message
|
||||||
send chan *transport.Message
|
send chan *transport.Message
|
||||||
// sock exit
|
// sock exit
|
||||||
@ -23,26 +24,26 @@ type mockSocket struct {
|
|||||||
remote string
|
remote string
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockClient struct {
|
type memoryClient struct {
|
||||||
*mockSocket
|
*memorySocket
|
||||||
opts transport.DialOptions
|
opts transport.DialOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockListener struct {
|
type memoryListener struct {
|
||||||
addr string
|
addr string
|
||||||
exit chan bool
|
exit chan bool
|
||||||
conn chan *mockSocket
|
conn chan *memorySocket
|
||||||
opts transport.ListenOptions
|
opts transport.ListenOptions
|
||||||
}
|
}
|
||||||
|
|
||||||
type mockTransport struct {
|
type memoryTransport struct {
|
||||||
opts transport.Options
|
opts transport.Options
|
||||||
|
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
listeners map[string]*mockListener
|
listeners map[string]*memoryListener
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *mockSocket) Recv(m *transport.Message) error {
|
func (ms *memorySocket) Recv(m *transport.Message) error {
|
||||||
select {
|
select {
|
||||||
case <-ms.exit:
|
case <-ms.exit:
|
||||||
return errors.New("connection closed")
|
return errors.New("connection closed")
|
||||||
@ -54,15 +55,15 @@ func (ms *mockSocket) Recv(m *transport.Message) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *mockSocket) Local() string {
|
func (ms *memorySocket) Local() string {
|
||||||
return ms.local
|
return ms.local
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *mockSocket) Remote() string {
|
func (ms *memorySocket) Remote() string {
|
||||||
return ms.remote
|
return ms.remote
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *mockSocket) Send(m *transport.Message) error {
|
func (ms *memorySocket) Send(m *transport.Message) error {
|
||||||
select {
|
select {
|
||||||
case <-ms.exit:
|
case <-ms.exit:
|
||||||
return errors.New("connection closed")
|
return errors.New("connection closed")
|
||||||
@ -73,7 +74,7 @@ func (ms *mockSocket) Send(m *transport.Message) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ms *mockSocket) Close() error {
|
func (ms *memorySocket) Close() error {
|
||||||
select {
|
select {
|
||||||
case <-ms.exit:
|
case <-ms.exit:
|
||||||
return nil
|
return nil
|
||||||
@ -83,11 +84,11 @@ func (ms *mockSocket) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockListener) Addr() string {
|
func (m *memoryListener) Addr() string {
|
||||||
return m.addr
|
return m.addr
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockListener) Close() error {
|
func (m *memoryListener) Close() error {
|
||||||
select {
|
select {
|
||||||
case <-m.exit:
|
case <-m.exit:
|
||||||
return nil
|
return nil
|
||||||
@ -97,13 +98,13 @@ func (m *mockListener) Close() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockListener) Accept(fn func(transport.Socket)) error {
|
func (m *memoryListener) Accept(fn func(transport.Socket)) error {
|
||||||
for {
|
for {
|
||||||
select {
|
select {
|
||||||
case <-m.exit:
|
case <-m.exit:
|
||||||
return nil
|
return nil
|
||||||
case c := <-m.conn:
|
case c := <-m.conn:
|
||||||
go fn(&mockSocket{
|
go fn(&memorySocket{
|
||||||
lexit: c.lexit,
|
lexit: c.lexit,
|
||||||
exit: c.exit,
|
exit: c.exit,
|
||||||
send: c.recv,
|
send: c.recv,
|
||||||
@ -115,7 +116,7 @@ func (m *mockListener) Accept(fn func(transport.Socket)) error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockTransport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
|
func (m *memoryTransport) Dial(addr string, opts ...transport.DialOption) (transport.Client, error) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -129,8 +130,8 @@ func (m *mockTransport) Dial(addr string, opts ...transport.DialOption) (transpo
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
client := &mockClient{
|
client := &memoryClient{
|
||||||
&mockSocket{
|
&memorySocket{
|
||||||
send: make(chan *transport.Message),
|
send: make(chan *transport.Message),
|
||||||
recv: make(chan *transport.Message),
|
recv: make(chan *transport.Message),
|
||||||
exit: make(chan bool),
|
exit: make(chan bool),
|
||||||
@ -145,13 +146,13 @@ func (m *mockTransport) Dial(addr string, opts ...transport.DialOption) (transpo
|
|||||||
select {
|
select {
|
||||||
case <-listener.exit:
|
case <-listener.exit:
|
||||||
return nil, errors.New("connection error")
|
return nil, errors.New("connection error")
|
||||||
case listener.conn <- client.mockSocket:
|
case listener.conn <- client.memorySocket:
|
||||||
}
|
}
|
||||||
|
|
||||||
return client, nil
|
return client, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockTransport) Listen(addr string, opts ...transport.ListenOption) (transport.Listener, error) {
|
func (m *memoryTransport) Listen(addr string, opts ...transport.ListenOption) (transport.Listener, error) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
|
|
||||||
@ -174,10 +175,10 @@ func (m *mockTransport) Listen(addr string, opts ...transport.ListenOption) (tra
|
|||||||
return nil, errors.New("already listening on " + addr)
|
return nil, errors.New("already listening on " + addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
listener := &mockListener{
|
listener := &memoryListener{
|
||||||
opts: options,
|
opts: options,
|
||||||
addr: addr,
|
addr: addr,
|
||||||
conn: make(chan *mockSocket),
|
conn: make(chan *memorySocket),
|
||||||
exit: make(chan bool),
|
exit: make(chan bool),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -186,19 +187,19 @@ func (m *mockTransport) Listen(addr string, opts ...transport.ListenOption) (tra
|
|||||||
return listener, nil
|
return listener, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockTransport) Init(opts ...transport.Option) error {
|
func (m *memoryTransport) Init(opts ...transport.Option) error {
|
||||||
for _, o := range opts {
|
for _, o := range opts {
|
||||||
o(&m.opts)
|
o(&m.opts)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockTransport) Options() transport.Options {
|
func (m *memoryTransport) Options() transport.Options {
|
||||||
return m.opts
|
return m.opts
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *mockTransport) String() string {
|
func (m *memoryTransport) String() string {
|
||||||
return "mock"
|
return "memory"
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewTransport(opts ...transport.Option) transport.Transport {
|
func NewTransport(opts ...transport.Option) transport.Transport {
|
||||||
@ -207,8 +208,8 @@ func NewTransport(opts ...transport.Option) transport.Transport {
|
|||||||
o(&options)
|
o(&options)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &mockTransport{
|
return &memoryTransport{
|
||||||
opts: options,
|
opts: options,
|
||||||
listeners: make(map[string]*mockListener),
|
listeners: make(map[string]*memoryListener),
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -1,4 +1,4 @@
|
|||||||
package mock
|
package memory
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"testing"
|
"testing"
|
||||||
@ -6,7 +6,7 @@ import (
|
|||||||
"github.com/micro/go-micro/transport"
|
"github.com/micro/go-micro/transport"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestTransport(t *testing.T) {
|
func TestMemoryTransport(t *testing.T) {
|
||||||
tr := NewTransport()
|
tr := NewTransport()
|
||||||
|
|
||||||
// bind / listen
|
// bind / listen
|
Loading…
Reference in New Issue
Block a user