Skip to content

Commit

Permalink
Adds mockery
Browse files Browse the repository at this point in the history
  • Loading branch information
ksysoev committed Apr 6, 2024
1 parent 75cc9da commit 6725c42
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 45 deletions.
53 changes: 14 additions & 39 deletions channel_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,12 @@ import (
"net/http"
"net/http/httptest"
"testing"

"golang.org/x/net/websocket"
)

type MockDispatcher struct{}

func (d *MockDispatcher) Dispatch(_ Connection, _ []byte) {}

type MockConnectionRegistry struct{}

func (r *MockConnectionRegistry) AddConnection(
_ context.Context,
_ *websocket.Conn,
_ onMessage,
) Connection {
return nil
}
func (r *MockConnectionRegistry) GetConnection(_ string) Connection {
return nil
}

type MockRequestParser struct{}

func (p *MockRequestParser) Parse(_ []byte) (Request, error) {
return nil, nil
}

func TestNewDefaultChannel(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)

Check failure on line 12 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockDispatcher
connRegistry := NewMockConnectionRegistry(t)

Check failure on line 13 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockConnectionRegistry

channel := NewDefaultChannel(path, dispatcher, connRegistry)

Expand All @@ -57,8 +32,8 @@ func TestNewDefaultChannel(t *testing.T) {
}
func TestDefaultChannel_Path(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)

Check failure on line 35 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockDispatcher
connRegistry := NewMockConnectionRegistry(t)

Check failure on line 36 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockConnectionRegistry

channel := NewDefaultChannel(path, dispatcher, connRegistry)

Expand All @@ -68,8 +43,8 @@ func TestDefaultChannel_Path(t *testing.T) {
}
func TestDefaultChannel_Handler(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)

Check failure on line 46 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockDispatcher
connRegistry := NewMockConnectionRegistry(t)

Check failure on line 47 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockConnectionRegistry

channel := NewDefaultChannel(path, dispatcher, connRegistry)
channel.SetContext(context.Background())
Expand All @@ -83,8 +58,8 @@ func TestDefaultChannel_Handler(t *testing.T) {
}
func TestDefaultChannel_SetContext(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)

Check failure on line 61 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockDispatcher
connRegistry := NewMockConnectionRegistry(t)

Check failure on line 62 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockConnectionRegistry

channel := NewDefaultChannel(path, dispatcher, connRegistry)

Expand All @@ -97,8 +72,8 @@ func TestDefaultChannel_SetContext(t *testing.T) {
}
func TestDefaultChannel_Use(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)

Check failure on line 75 in channel_test.go

View workflow job for this annotation

GitHub Actions / tests

undefined: NewMockDispatcher
connRegistry := NewMockConnectionRegistry(t)

channel := NewDefaultChannel(path, dispatcher, connRegistry)

Expand All @@ -117,8 +92,8 @@ func TestDefaultChannel_Use(t *testing.T) {

func TestDefaultChannel_wrapMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)
connRegistry := NewMockConnectionRegistry(t)

channel := NewDefaultChannel(path, dispatcher, connRegistry)

Expand Down Expand Up @@ -153,8 +128,8 @@ func TestDefaultChannel_wrapMiddleware(t *testing.T) {
}
func TestDefaultChannel_SetContextMiddleware(t *testing.T) {
path := "/test/path"
dispatcher := &MockDispatcher{}
connRegistry := &MockConnectionRegistry{}
dispatcher := NewMockDispatcher(t)
connRegistry := NewMockConnectionRegistry(t)

channel := NewDefaultChannel(path, dispatcher, connRegistry)

Expand Down
12 changes: 6 additions & 6 deletions connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ type ConnectionRegistry interface {
AddConnection(
ctx context.Context,
ws *websocket.Conn,
cb onMessage,
cb OnMessage,
) Connection
GetConnection(id string) Connection
}
Expand Down Expand Up @@ -44,7 +44,7 @@ func NewDefaultConnectionRegistry() *DefaultConnectionRegistry {
func (r *DefaultConnectionRegistry) AddConnection(
ctx context.Context,
ws *websocket.Conn,
cb onMessage,
cb OnMessage,
) Connection {
r.mu.Lock()
defer r.mu.Unlock()
Expand Down Expand Up @@ -84,21 +84,21 @@ type Connection interface {
type Conn struct {
ws *websocket.Conn
ctx context.Context
onMessageCB onMessage
onMessageCB OnMessage
onClose chan<- string
ctxCancel context.CancelFunc
id string
isClosed atomic.Bool
}

// onMessage is type for onMessage callback
type onMessage func(conn Connection, data []byte)
// OnMessage is type for OnMessage callback
type OnMessage func(conn Connection, data []byte)

// NewConnection creates new instance of websocket connection
func NewConnection(
ctx context.Context,
ws *websocket.Conn,
cb onMessage,
cb OnMessage,
onClose chan<- string,
) *Conn {
ctx, cancel := context.WithCancel(ctx)
Expand Down

0 comments on commit 6725c42

Please sign in to comment.