rename mock things to memory

This commit is contained in:
Asim Aslam 2019-01-14 15:27:25 +00:00
parent c17d0fcc0f
commit 39c24baca9
16 changed files with 142 additions and 110 deletions

View File

@ -6,12 +6,20 @@ import (
"time"
"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) {
be.StopTimer()
m := mock.NewRegistry()
m := newTestRegistry()
b := NewBroker(Registry(m))
topic := uuid.New().String()
@ -70,7 +78,7 @@ func sub(be *testing.B, c int) {
func pub(be *testing.B, c int) {
be.StopTimer()
m := mock.NewRegistry()
m := newTestRegistry()
b := NewBroker(Registry(m))
topic := uuid.New().String()
@ -139,7 +147,7 @@ func pub(be *testing.B, c int) {
}
func TestBroker(t *testing.T) {
m := mock.NewRegistry()
m := newTestRegistry()
b := NewBroker(Registry(m))
if err := b.Init(); err != nil {
@ -186,7 +194,7 @@ func TestBroker(t *testing.T) {
}
func TestConcurrentSubBroker(t *testing.T) {
m := mock.NewRegistry()
m := newTestRegistry()
b := NewBroker(Registry(m))
if err := b.Init(); err != nil {
@ -243,7 +251,7 @@ func TestConcurrentSubBroker(t *testing.T) {
}
func TestConcurrentPubBroker(t *testing.T) {
m := mock.NewRegistry()
m := newTestRegistry()
b := NewBroker(Registry(m))
if err := b.Init(); err != nil {

View File

@ -1,5 +1,5 @@
// Package mock provides a mock broker for testing
package mock
// Package memory provides a memory broker
package memory
import (
"errors"
@ -9,20 +9,20 @@ import (
"github.com/micro/go-micro/broker"
)
type mockBroker struct {
type memoryBroker struct {
opts broker.Options
sync.RWMutex
connected bool
Subscribers map[string][]*mockSubscriber
Subscribers map[string][]*memorySubscriber
}
type mockPublication struct {
type memoryPublication struct {
topic string
message *broker.Message
}
type mockSubscriber struct {
type memorySubscriber struct {
id string
topic string
exit chan bool
@ -30,15 +30,15 @@ type mockSubscriber struct {
opts broker.SubscribeOptions
}
func (m *mockBroker) Options() broker.Options {
func (m *memoryBroker) Options() broker.Options {
return m.opts
}
func (m *mockBroker) Address() string {
func (m *memoryBroker) Address() string {
return ""
}
func (m *mockBroker) Connect() error {
func (m *memoryBroker) Connect() error {
m.Lock()
defer m.Unlock()
@ -51,7 +51,7 @@ func (m *mockBroker) Connect() error {
return nil
}
func (m *mockBroker) Disconnect() error {
func (m *memoryBroker) Disconnect() error {
m.Lock()
defer m.Unlock()
@ -64,14 +64,14 @@ func (m *mockBroker) Disconnect() error {
return nil
}
func (m *mockBroker) Init(opts ...broker.Option) error {
func (m *memoryBroker) Init(opts ...broker.Option) error {
for _, o := range opts {
o(&m.opts)
}
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()
defer m.Unlock()
@ -84,7 +84,7 @@ func (m *mockBroker) Publish(topic string, message *broker.Message, opts ...brok
return nil
}
p := &mockPublication{
p := &memoryPublication{
topic: topic,
message: message,
}
@ -98,7 +98,7 @@ func (m *mockBroker) Publish(topic string, message *broker.Message, opts ...brok
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()
defer m.Unlock()
@ -111,7 +111,7 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
o(&options)
}
sub := &mockSubscriber{
sub := &memorySubscriber{
exit: make(chan bool, 1),
id: uuid.New().String(),
topic: topic,
@ -124,7 +124,7 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
go func() {
<-sub.exit
m.Lock()
var newSubscribers []*mockSubscriber
var newSubscribers []*memorySubscriber
for _, sb := range m.Subscribers[topic] {
if sb.id == sub.id {
continue
@ -138,31 +138,31 @@ func (m *mockBroker) Subscribe(topic string, handler broker.Handler, opts ...bro
return sub, nil
}
func (m *mockBroker) String() string {
return "mock"
func (m *memoryBroker) String() string {
return "memory"
}
func (m *mockPublication) Topic() string {
func (m *memoryPublication) Topic() string {
return m.topic
}
func (m *mockPublication) Message() *broker.Message {
func (m *memoryPublication) Message() *broker.Message {
return m.message
}
func (m *mockPublication) Ack() error {
func (m *memoryPublication) Ack() error {
return nil
}
func (m *mockSubscriber) Options() broker.SubscribeOptions {
func (m *memorySubscriber) Options() broker.SubscribeOptions {
return m.opts
}
func (m *mockSubscriber) Topic() string {
func (m *memorySubscriber) Topic() string {
return m.topic
}
func (m *mockSubscriber) Unsubscribe() error {
func (m *memorySubscriber) Unsubscribe() error {
m.exit <- true
return nil
}
@ -173,8 +173,8 @@ func NewBroker(opts ...broker.Option) broker.Broker {
o(&options)
}
return &mockBroker{
return &memoryBroker{
opts: options,
Subscribers: make(map[string][]*mockSubscriber),
Subscribers: make(map[string][]*memorySubscriber),
}
}

View File

@ -1,4 +1,4 @@
package mock
package memory
import (
"fmt"
@ -7,7 +7,7 @@ import (
"github.com/micro/go-micro/broker"
)
func TestBroker(t *testing.T) {
func TestMemoryBroker(t *testing.T) {
b := NewBroker()
if err := b.Connect(); err != nil {

View File

@ -7,10 +7,16 @@ import (
"github.com/micro/go-micro/errors"
"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"
)
func newTestRegistry() registry.Registry {
r := memory.NewRegistry()
r.(*memory.Registry).Setup()
return r
}
func TestCallAddress(t *testing.T) {
var called bool
service := "test.service"
@ -38,7 +44,7 @@ func TestCallAddress(t *testing.T) {
}
}
r := mock.NewRegistry()
r := newTestRegistry()
c := NewClient(
Registry(r),
WrapCall(wrap),
@ -77,7 +83,7 @@ func TestCallRetry(t *testing.T) {
}
}
r := mock.NewRegistry()
r := newTestRegistry()
c := NewClient(
Registry(r),
WrapCall(wrap),
@ -127,7 +133,7 @@ func TestCallWrapper(t *testing.T) {
}
}
r := mock.NewRegistry()
r := newTestRegistry()
c := NewClient(
Registry(r),
WrapCall(wrap),

View File

@ -5,7 +5,7 @@ import (
"time"
"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) {
@ -13,7 +13,7 @@ func testPool(t *testing.T, size int, ttl time.Duration) {
p := newPool(size, ttl)
// mock transport
tr := mock.NewTransport()
tr := memory.NewTransport()
// listen
l, err := tr.Listen(":0")

View File

@ -17,12 +17,14 @@ import (
// brokers
"github.com/micro/go-micro/broker"
"github.com/micro/go-micro/broker/http"
"github.com/micro/go-micro/broker/memory"
// registries
"github.com/micro/go-micro/registry"
"github.com/micro/go-micro/registry/consul"
"github.com/micro/go-micro/registry/gossip"
"github.com/micro/go-micro/registry/mdns"
rmem "github.com/micro/go-micro/registry/memory"
// selectors
"github.com/micro/go-micro/selector"
@ -32,6 +34,7 @@ import (
// transports
"github.com/micro/go-micro/transport"
thttp "github.com/micro/go-micro/transport/http"
tmem "github.com/micro/go-micro/transport/memory"
)
type Cmd interface {
@ -165,6 +168,7 @@ var (
DefaultBrokers = map[string]func(...broker.Option) broker.Broker{
"http": http.NewBroker,
"memory": memory.NewBroker,
}
DefaultClients = map[string]func(...client.Option) client.Client{
@ -175,6 +179,7 @@ var (
"consul": consul.NewRegistry,
"gossip": gossip.NewRegistry,
"mdns": mdns.NewRegistry,
"memory": rmem.NewRegistry,
}
DefaultSelectors = map[string]func(...selector.Option) selector.Selector{
@ -189,6 +194,7 @@ var (
}
DefaultTransports = map[string]func(...transport.Option) transport.Transport{
"memory": tmem.NewTransport,
"http": thttp.NewTransport,
}

View File

@ -5,7 +5,7 @@ import (
"sync"
"testing"
"github.com/micro/go-micro/registry/mock"
"github.com/micro/go-micro/registry/memory"
proto "github.com/micro/go-micro/server/debug/proto"
)
@ -13,9 +13,12 @@ func TestFunction(t *testing.T) {
var wg sync.WaitGroup
wg.Add(1)
r := memory.NewRegistry()
r.(*memory.Registry).Setup()
// create service
fn := NewFunction(
Registry(mock.NewRegistry()),
Registry(r),
Name("test.function"),
AfterStart(func() error {
wg.Done()

View File

@ -1,4 +1,4 @@
package mock
package memory
import (
"github.com/micro/go-micro/registry"

View File

@ -1,4 +1,4 @@
package mock
package memory
import (
"testing"

View File

@ -1,5 +1,5 @@
// Package mock provides a mock registry for testing
package mock
// Package memory provides an in-memory registry
package memory
import (
"sync"
@ -7,13 +7,14 @@ import (
"github.com/micro/go-micro/registry"
)
type mockRegistry struct {
type Registry struct {
sync.RWMutex
Services map[string][]*registry.Service
}
var (
mockData = map[string][]*registry.Service{
// mock data
Data = map[string][]*registry.Service{
"foo": []*registry.Service{
{
Name: "foo",
@ -57,15 +58,16 @@ var (
}
)
func (m *mockRegistry) init() {
// Setup sets mock data
func (m *Registry) Setup() {
m.Lock()
defer m.Unlock()
// add some mock data
m.Services = mockData
// add some memory data
m.Services = Data
}
func (m *mockRegistry) GetService(service string) ([]*registry.Service, error) {
func (m *Registry) GetService(service string) ([]*registry.Service, error) {
m.Lock()
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()
defer m.Unlock()
@ -88,7 +90,7 @@ func (m *mockRegistry) ListServices() ([]*registry.Service, error) {
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()
defer m.Unlock()
@ -97,7 +99,7 @@ func (m *mockRegistry) Register(s *registry.Service, opts ...registry.RegisterOp
return nil
}
func (m *mockRegistry) Deregister(s *registry.Service) error {
func (m *Registry) Deregister(s *registry.Service) error {
m.Lock()
defer m.Unlock()
@ -106,28 +108,28 @@ func (m *mockRegistry) Deregister(s *registry.Service) error {
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
for _, o := range opts {
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 {
return "mock"
func (m *Registry) String() string {
return "memory"
}
func (m *mockRegistry) Init(opts ...registry.Option) error {
func (m *Registry) Init(opts ...registry.Option) error {
return nil
}
func (m *mockRegistry) Options() registry.Options {
func (m *Registry) Options() registry.Options {
return registry.Options{}
}
func NewRegistry(opts ...registry.Options) registry.Registry {
m := &mockRegistry{Services: make(map[string][]*registry.Service)}
m.init()
return m
func NewRegistry(opts ...registry.Option) registry.Registry {
return &Registry{
Services: make(map[string][]*registry.Service),
}
}

View File

@ -1,4 +1,4 @@
package mock
package memory
import (
"testing"
@ -82,6 +82,7 @@ var (
func TestMockRegistry(t *testing.T) {
m := NewRegistry()
m.(*Registry).Setup()
fn := func(k string, v []*registry.Service) {
services, err := m.GetService(k)
@ -107,8 +108,8 @@ func TestMockRegistry(t *testing.T) {
}
}
// test existing mock data
for k, v := range mockData {
// test existing memory data
for k, v := range Data {
fn(k, v)
}

View File

@ -1,4 +1,4 @@
package mock
package memory
import (
"errors"
@ -6,12 +6,12 @@ import (
"github.com/micro/go-micro/registry"
)
type mockWatcher struct {
type memoryWatcher struct {
exit chan bool
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
select {
case <-m.exit:
@ -19,7 +19,7 @@ func (m *mockWatcher) Next() (*registry.Result, error) {
}
}
func (m *mockWatcher) Stop() {
func (m *memoryWatcher) Stop() {
select {
case <-m.exit:
return

View File

@ -3,13 +3,15 @@ package selector
import (
"testing"
"github.com/micro/go-micro/registry/mock"
"github.com/micro/go-micro/registry/memory"
)
func TestRegistrySelector(t *testing.T) {
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")
if err != nil {

View File

@ -5,7 +5,7 @@ import (
"sync"
"testing"
"github.com/micro/go-micro/registry/mock"
"github.com/micro/go-micro/registry/memory"
proto "github.com/micro/go-micro/server/debug/proto"
)
@ -16,11 +16,14 @@ func TestService(t *testing.T) {
// cancellation context
ctx, cancel := context.WithCancel(context.Background())
r := memory.NewRegistry()
r.(*memory.Registry).Setup()
// create service
service := NewService(
Name("test.service"),
Context(ctx),
Registry(mock.NewRegistry()),
Registry(r),
AfterStart(func() error {
wg.Done()
return nil

View File

@ -1,4 +1,5 @@
package mock
// Package memory is an in-memory transport
package memory
import (
"errors"
@ -11,7 +12,7 @@ import (
"github.com/micro/go-micro/transport"
)
type mockSocket struct {
type memorySocket struct {
recv chan *transport.Message
send chan *transport.Message
// sock exit
@ -23,26 +24,26 @@ type mockSocket struct {
remote string
}
type mockClient struct {
*mockSocket
type memoryClient struct {
*memorySocket
opts transport.DialOptions
}
type mockListener struct {
type memoryListener struct {
addr string
exit chan bool
conn chan *mockSocket
conn chan *memorySocket
opts transport.ListenOptions
}
type mockTransport struct {
type memoryTransport struct {
opts transport.Options
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 {
case <-ms.exit:
return errors.New("connection closed")
@ -54,15 +55,15 @@ func (ms *mockSocket) Recv(m *transport.Message) error {
return nil
}
func (ms *mockSocket) Local() string {
func (ms *memorySocket) Local() string {
return ms.local
}
func (ms *mockSocket) Remote() string {
func (ms *memorySocket) Remote() string {
return ms.remote
}
func (ms *mockSocket) Send(m *transport.Message) error {
func (ms *memorySocket) Send(m *transport.Message) error {
select {
case <-ms.exit:
return errors.New("connection closed")
@ -73,7 +74,7 @@ func (ms *mockSocket) Send(m *transport.Message) error {
return nil
}
func (ms *mockSocket) Close() error {
func (ms *memorySocket) Close() error {
select {
case <-ms.exit:
return nil
@ -83,11 +84,11 @@ func (ms *mockSocket) Close() error {
return nil
}
func (m *mockListener) Addr() string {
func (m *memoryListener) Addr() string {
return m.addr
}
func (m *mockListener) Close() error {
func (m *memoryListener) Close() error {
select {
case <-m.exit:
return nil
@ -97,13 +98,13 @@ func (m *mockListener) Close() error {
return nil
}
func (m *mockListener) Accept(fn func(transport.Socket)) error {
func (m *memoryListener) Accept(fn func(transport.Socket)) error {
for {
select {
case <-m.exit:
return nil
case c := <-m.conn:
go fn(&mockSocket{
go fn(&memorySocket{
lexit: c.lexit,
exit: c.exit,
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()
defer m.Unlock()
@ -129,8 +130,8 @@ func (m *mockTransport) Dial(addr string, opts ...transport.DialOption) (transpo
o(&options)
}
client := &mockClient{
&mockSocket{
client := &memoryClient{
&memorySocket{
send: make(chan *transport.Message),
recv: make(chan *transport.Message),
exit: make(chan bool),
@ -145,13 +146,13 @@ func (m *mockTransport) Dial(addr string, opts ...transport.DialOption) (transpo
select {
case <-listener.exit:
return nil, errors.New("connection error")
case listener.conn <- client.mockSocket:
case listener.conn <- client.memorySocket:
}
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()
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)
}
listener := &mockListener{
listener := &memoryListener{
opts: options,
addr: addr,
conn: make(chan *mockSocket),
conn: make(chan *memorySocket),
exit: make(chan bool),
}
@ -186,19 +187,19 @@ func (m *mockTransport) Listen(addr string, opts ...transport.ListenOption) (tra
return listener, nil
}
func (m *mockTransport) Init(opts ...transport.Option) error {
func (m *memoryTransport) Init(opts ...transport.Option) error {
for _, o := range opts {
o(&m.opts)
}
return nil
}
func (m *mockTransport) Options() transport.Options {
func (m *memoryTransport) Options() transport.Options {
return m.opts
}
func (m *mockTransport) String() string {
return "mock"
func (m *memoryTransport) String() string {
return "memory"
}
func NewTransport(opts ...transport.Option) transport.Transport {
@ -207,8 +208,8 @@ func NewTransport(opts ...transport.Option) transport.Transport {
o(&options)
}
return &mockTransport{
return &memoryTransport{
opts: options,
listeners: make(map[string]*mockListener),
listeners: make(map[string]*memoryListener),
}
}

View File

@ -1,4 +1,4 @@
package mock
package memory
import (
"testing"
@ -6,7 +6,7 @@ import (
"github.com/micro/go-micro/transport"
)
func TestTransport(t *testing.T) {
func TestMemoryTransport(t *testing.T) {
tr := NewTransport()
// bind / listen