Skip to content

Commit

Permalink
[#7] .Update added tests for cosmos + IsHealthy
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasObenaus committed Feb 12, 2020
1 parent 3586799 commit 7872c06
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 3 deletions.
8 changes: 5 additions & 3 deletions cosmos.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,11 @@ func NumMaxActiveConnections(numMaxActiveConnections int) Option {
// New creates a new instance of the Cosmos (-DB connector)
func New(host string, options ...Option) (*Cosmos, error) {
cosmos := &Cosmos{
logger: zerolog.Nop(),
errorChannel: make(chan error),
host: host,
logger: zerolog.Nop(),
errorChannel: make(chan error),
host: host,
numMaxActiveConnections: 10,
connectionIdleTimeout: time.Second * 30,
}

for _, opt := range options {
Expand Down
71 changes: 71 additions & 0 deletions cosmos_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package gremtune

import (
"testing"
"time"

"github.com/golang/mock/gomock"
mock_interfaces "github.com/schwartzmx/gremtune/test/mocks/interfaces"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestNew(t *testing.T) {
// GIVEN
idleTimeout := time.Second * 12
maxActiveConnections := 10
username := "abcd"
password := "xyz"

// WHEN
cosmos, err := New("ws://host",
ConnectionIdleTimeout(idleTimeout),
NumMaxActiveConnections(maxActiveConnections),
WithAuth(username, password),
)

// THEN
require.NoError(t, err)
require.NotNil(t, cosmos)
assert.Equal(t, idleTimeout, cosmos.connectionIdleTimeout)
assert.Equal(t, maxActiveConnections, cosmos.numMaxActiveConnections)
assert.Equal(t, username, cosmos.username)
assert.Equal(t, password, cosmos.password)
}

func TestStop(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedQueryExecutor := mock_interfaces.NewMockQueryExecutor(mockCtrl)

cosmos, err := New("ws://host")
require.NoError(t, err)
require.NotNil(t, cosmos)
cosmos.pool = mockedQueryExecutor

// WHEN
mockedQueryExecutor.EXPECT().Close().Return(nil)
err = cosmos.Stop()
assert.NoError(t, err)
}

func TestIsHealthy(t *testing.T) {
// GIVEN
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedQueryExecutor := mock_interfaces.NewMockQueryExecutor(mockCtrl)

cosmos, err := New("ws://host")
require.NoError(t, err)
require.NotNil(t, cosmos)
cosmos.pool = mockedQueryExecutor

// WHEN -- connected --> healthy
mockedQueryExecutor.EXPECT().IsConnected().Return(true)
assert.True(t, cosmos.IsHealthy())

// WHEN -- not connected --> not healthy
mockedQueryExecutor.EXPECT().IsConnected().Return(false)
assert.False(t, cosmos.IsHealthy())
}
1 change: 1 addition & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func (p *pool) IsConnected() bool {
// in case we have at least one active connection
// --> we can return immediately with status connected
if p.active > 0 {
p.mu.RUnlock()
return true
}

Expand Down
52 changes: 52 additions & 0 deletions pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,58 @@ import (
"github.com/stretchr/testify/require"
)

func TestIsConnected(t *testing.T) {
// GIVEN
logger := zerolog.Nop()
mockCtrl := gomock.NewController(t)
defer mockCtrl.Finish()
mockedQueryExecutor := mock_interfaces.NewMockQueryExecutor(mockCtrl)
clientFactory := func() (interfaces.QueryExecutor, error) {
return mockedQueryExecutor, nil
}
pool, err := NewPool(clientFactory, 2, time.Second*30, logger)
require.NoError(t, err)
require.NotNil(t, pool)

// GIVEN -- no connections
// WHEN calling IsConnected() - THEN not connected
assert.False(t, pool.IsConnected())

// GIVEN -- one active connection
// WHEN calling IsConnected() - THEN connected
// acquire one connection
pConn, err := pool.Get()
require.NoError(t, err)
require.NotNil(t, pConn)
assert.True(t, pool.IsConnected())

// GIVEN -- one idle connection
// WHEN calling IsConnected() - THEN connected
// put back the active connection to the idlepool
pConn.Close()
mockedQueryExecutor.EXPECT().IsConnected().Return(true)
assert.True(t, pool.IsConnected())

// GIVEN -- one idle (connected) and one idle (not connected) connection
// WHEN calling IsConnected() - THEN connected
// acquire one more connection
mockedQueryExecutor.EXPECT().LastError().Return(nil)
mockedQueryExecutor.EXPECT().IsConnected().Return(true)
pConn1, err := pool.Get()
require.NoError(t, err)
require.NotNil(t, pConn1)
pConn2, err := pool.Get()
require.NoError(t, err)
require.NotNil(t, pConn2)

// put back the active connections to the idlepool
pConn1.Close()
pConn2.Close()
mockedQueryExecutor.EXPECT().IsConnected().Return(false)
mockedQueryExecutor.EXPECT().IsConnected().Return(true)
assert.True(t, pool.IsConnected())
}

func TestClose(t *testing.T) {
// GIVEN
logger := zerolog.Nop()
Expand Down

0 comments on commit 7872c06

Please sign in to comment.