2020-04-10 05:37:53 +05:30
|
|
|
package eventsubscribers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"testing"
|
2020-04-10 17:49:00 +05:30
|
|
|
"time"
|
2020-04-10 05:37:53 +05:30
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2020-04-21 00:48:27 +05:30
|
|
|
"github.com/stretchr/testify/mock"
|
2020-04-10 05:37:53 +05:30
|
|
|
)
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
type pingableMock struct {
|
|
|
|
mock.Mock
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *pingableMock) Ping() error {
|
|
|
|
args := p.Called()
|
|
|
|
return args.Error(0)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDatabaseChecker(t *testing.T) {
|
|
|
|
t.Run("no error", func(t *testing.T) {
|
|
|
|
p := &pingableMock{}
|
|
|
|
p.On("Ping").Return(nil)
|
|
|
|
checker := DatabaseChecker(p)
|
|
|
|
assert.Nil(t, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with error", func(t *testing.T) {
|
|
|
|
err := errors.New("mock error")
|
|
|
|
p := &pingableMock{}
|
|
|
|
p.On("Ping").Return(err)
|
|
|
|
checker := DatabaseChecker(p)
|
|
|
|
assert.Equal(t, err, checker(context.Background()))
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("context timeout", func(t *testing.T) {
|
|
|
|
p := &pingableMock{}
|
|
|
|
waitChan := make(chan time.Time, 1)
|
|
|
|
p.On("Ping").WaitUntil(waitChan).Return(nil)
|
|
|
|
|
2023-12-12 06:05:08 +05:30
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 0)
|
|
|
|
defer cancel()
|
|
|
|
|
2020-04-21 00:48:27 +05:30
|
|
|
checker := DatabaseChecker(p)
|
|
|
|
assert.Errorf(t, checker(ctx), "check timeout")
|
|
|
|
close(waitChan)
|
|
|
|
})
|
|
|
|
}
|