Skip to content

Commit

Permalink
remove lock
Browse files Browse the repository at this point in the history
  • Loading branch information
zetaab committed Sep 5, 2023
1 parent a430cf9 commit 5a4a331
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 23 deletions.
2 changes: 1 addition & 1 deletion arrays_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ func TestArrayContainsIgnoreCase(t *testing.T) {
},
{
keys: []string{"asiakas", "bar"},
value: "IN-asiaskasfoo",
value: "IN-asiakasfoo",
result: true,
name: "asiakas in value",
},
Expand Down
6 changes: 4 additions & 2 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,13 @@ func TestMakeRequestMock(t *testing.T) {
MaxRetries: 1,
}

helloWorld := `{"hello":"world"}`

client := &MockClient{
DoFunc: func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: 200,
Body: io.NopCloser(strings.NewReader(`{"hello":"world"}`)),
Body: io.NopCloser(strings.NewReader(helloWorld)),
}, nil
},
}
Expand All @@ -90,6 +92,6 @@ func TestMakeRequestMock(t *testing.T) {
backoff,
)
require.NoError(t, err)
assert.Equal(t, `{"hello":"world"}`, body.Body)
assert.Equal(t, helloWorld, string(body.Body))
assert.Equal(t, 200, body.StatusCode)
}
11 changes: 1 addition & 10 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"reflect"
"runtime"
"strings"
"sync"

"github.com/fsnotify/fsnotify"
"github.com/getsentry/sentry-go"
Expand Down Expand Up @@ -103,22 +102,16 @@ func RemoveDot(input string) string {
return input
}

type ConfigLock struct {
lock sync.Mutex
}

// LoadAndListenConfig loads config file to struct and listen changes in it.
func LoadAndListenConfig(path string, l *ConfigLock, obj interface{}, onUpdate func(oldObj interface{})) error {
func LoadAndListenConfig(path string, obj interface{}, onUpdate func(oldObj interface{})) error {
v := viper.New()
v.SetConfigFile(path)
if err := v.ReadInConfig(); err != nil {
return fmt.Errorf("unable to read config: %w", err)
}
l.lock.Lock()
if err := v.Unmarshal(&obj); err != nil {
return fmt.Errorf("unable to marshal config: %w", err)
}
l.lock.Unlock()
log.Info().
Str("path", v.ConfigFileUsed()).
Msg("config loaded")
Expand All @@ -127,13 +120,11 @@ func LoadAndListenConfig(path string, l *ConfigLock, obj interface{}, onUpdate f
Str("path", e.Name).
Msg("config reloaded")
oldObj := reflect.Indirect(reflect.ValueOf(obj)).Interface()
l.lock.Lock()
if err := v.Unmarshal(&obj); err != nil {
log.Fatal().
Str("path", e.Name).
Msgf("unable to marshal config: %v", err)
}
l.lock.Unlock()
if onUpdate != nil {
onUpdate(oldObj)
}
Expand Down
13 changes: 3 additions & 10 deletions utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ type Config struct {
}

func TestLoadAndListenConfig_NonExistingFile(t *testing.T) {
err := LoadAndListenConfig("invalid.yaml", &ConfigLock{}, &Config{}, nil)
err := LoadAndListenConfig("invalid.yaml", &Config{}, nil)
assert.ErrorContains(t, err, "no such file or directory")
}

func TestLoadAndListenConfig_InvalidSyntax(t *testing.T) {
err := LoadAndListenConfig("testdata/invalid.yaml", &ConfigLock{}, &Config{}, nil)
err := LoadAndListenConfig("testdata/invalid.yaml", &Config{}, nil)
assert.ErrorContains(t, err, "invalid syntax")
}

Expand Down Expand Up @@ -115,16 +115,13 @@ func TestLoadAndListenConfigOnUpdate(t *testing.T) {
require.NoError(t, err)

realConf := &Config{}
l := &ConfigLock{}
values := &UpdateValues{}
notifyFn, waitForUpdate := updateCallbacks()
err = LoadAndListenConfig(filePath, l, realConf, func(oldConf interface{}) {
err = LoadAndListenConfig(filePath, realConf, func(oldConf interface{}) {
values.Set(1, oldConf, notifyFn)
})
require.NoError(t, err)
l.lock.Lock()
assert.Equal(t, 0, realConf.Index)
l.lock.Unlock()
assert.Equal(t, 0, values.GetOldValue())
assert.Equal(t, 0, values.GetUpdateCalls())

Expand All @@ -136,9 +133,7 @@ func TestLoadAndListenConfigOnUpdate(t *testing.T) {
require.NoError(t, err)

waitForUpdate(t)
l.lock.Lock()
assert.Equal(t, 1, realConf.Index)
l.lock.Unlock()
assert.Equal(t, 0, values.GetOldValue())
assert.Equal(t, 1, values.GetUpdateCalls())

Expand All @@ -150,9 +145,7 @@ func TestLoadAndListenConfigOnUpdate(t *testing.T) {
require.NoError(t, err)

waitForUpdate(t)
l.lock.Lock()
assert.Equal(t, 2, realConf.Index)
l.lock.Unlock()
assert.Equal(t, 1, values.GetOldValue())
assert.Equal(t, 2, values.GetUpdateCalls())
}
Expand Down

0 comments on commit 5a4a331

Please sign in to comment.