fix(storage): only log warning if flushing merged servers to file fails
This commit is contained in:
@@ -7,3 +7,4 @@ func newNoopLogger() *noopLogger {
|
||||
}
|
||||
|
||||
func (l *noopLogger) Info(string) {}
|
||||
func (l *noopLogger) Warn(string) {}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
package storage
|
||||
|
||||
//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Infoer
|
||||
//go:generate mockgen -destination=mocks_test.go -package $GOPACKAGE . Logger
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// Code generated by MockGen. DO NOT EDIT.
|
||||
// Source: github.com/qdm12/gluetun/internal/storage (interfaces: Infoer)
|
||||
// Source: github.com/qdm12/gluetun/internal/storage (interfaces: Logger)
|
||||
|
||||
// Package storage is a generated GoMock package.
|
||||
package storage
|
||||
@@ -10,37 +10,49 @@ import (
|
||||
gomock "github.com/golang/mock/gomock"
|
||||
)
|
||||
|
||||
// MockInfoer is a mock of Infoer interface.
|
||||
type MockInfoer struct {
|
||||
// MockLogger is a mock of Logger interface.
|
||||
type MockLogger struct {
|
||||
ctrl *gomock.Controller
|
||||
recorder *MockInfoerMockRecorder
|
||||
recorder *MockLoggerMockRecorder
|
||||
}
|
||||
|
||||
// MockInfoerMockRecorder is the mock recorder for MockInfoer.
|
||||
type MockInfoerMockRecorder struct {
|
||||
mock *MockInfoer
|
||||
// MockLoggerMockRecorder is the mock recorder for MockLogger.
|
||||
type MockLoggerMockRecorder struct {
|
||||
mock *MockLogger
|
||||
}
|
||||
|
||||
// NewMockInfoer creates a new mock instance.
|
||||
func NewMockInfoer(ctrl *gomock.Controller) *MockInfoer {
|
||||
mock := &MockInfoer{ctrl: ctrl}
|
||||
mock.recorder = &MockInfoerMockRecorder{mock}
|
||||
// NewMockLogger creates a new mock instance.
|
||||
func NewMockLogger(ctrl *gomock.Controller) *MockLogger {
|
||||
mock := &MockLogger{ctrl: ctrl}
|
||||
mock.recorder = &MockLoggerMockRecorder{mock}
|
||||
return mock
|
||||
}
|
||||
|
||||
// EXPECT returns an object that allows the caller to indicate expected use.
|
||||
func (m *MockInfoer) EXPECT() *MockInfoerMockRecorder {
|
||||
func (m *MockLogger) EXPECT() *MockLoggerMockRecorder {
|
||||
return m.recorder
|
||||
}
|
||||
|
||||
// Info mocks base method.
|
||||
func (m *MockInfoer) Info(arg0 string) {
|
||||
func (m *MockLogger) Info(arg0 string) {
|
||||
m.ctrl.T.Helper()
|
||||
m.ctrl.Call(m, "Info", arg0)
|
||||
}
|
||||
|
||||
// Info indicates an expected call of Info.
|
||||
func (mr *MockInfoerMockRecorder) Info(arg0 interface{}) *gomock.Call {
|
||||
func (mr *MockLoggerMockRecorder) Info(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockInfoer)(nil).Info), arg0)
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Info", reflect.TypeOf((*MockLogger)(nil).Info), arg0)
|
||||
}
|
||||
|
||||
// Warn mocks base method.
|
||||
func (m *MockLogger) Warn(arg0 string) {
|
||||
m.ctrl.T.Helper()
|
||||
m.ctrl.Call(m, "Warn", arg0)
|
||||
}
|
||||
|
||||
// Warn indicates an expected call of Warn.
|
||||
func (mr *MockLoggerMockRecorder) Warn(arg0 interface{}) *gomock.Call {
|
||||
mr.mock.ctrl.T.Helper()
|
||||
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Warn", reflect.TypeOf((*MockLogger)(nil).Warn), arg0)
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ func Test_extractServersFromBytes(t *testing.T) {
|
||||
t.Parallel()
|
||||
ctrl := gomock.NewController(t)
|
||||
|
||||
logger := NewMockInfoer(ctrl)
|
||||
logger := NewMockLogger(ctrl)
|
||||
var previousLogCall *gomock.Call
|
||||
for _, logged := range testCase.logged {
|
||||
call := logger.EXPECT().Info(logged)
|
||||
|
||||
@@ -13,19 +13,20 @@ type Storage struct {
|
||||
// the embedded JSON file on every call to the
|
||||
// SyncServers method.
|
||||
hardcodedServers models.AllServers
|
||||
logger Infoer
|
||||
logger Logger
|
||||
filepath string
|
||||
}
|
||||
|
||||
type Infoer interface {
|
||||
type Logger interface {
|
||||
Info(s string)
|
||||
Warn(s string)
|
||||
}
|
||||
|
||||
// New creates a new storage and reads the servers from the
|
||||
// embedded servers file and the file on disk.
|
||||
// Passing an empty filepath disables the reading and writing of
|
||||
// servers.
|
||||
func New(logger Infoer, filepath string) (storage *Storage, err error) {
|
||||
func New(logger Logger, filepath string) (storage *Storage, err error) {
|
||||
// A unit test prevents any error from being returned
|
||||
// and ensures all providers are part of the servers returned.
|
||||
hardcodedServers, _ := parseHardcodedServers()
|
||||
|
||||
@@ -52,7 +52,7 @@ func (s *Storage) syncServers() (err error) {
|
||||
|
||||
err = s.flushToFile(s.filepath)
|
||||
if err != nil {
|
||||
return fmt.Errorf("writing servers to file: %w", err)
|
||||
s.logger.Warn("failed writing servers to file: " + err.Error())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user