@cryptotaxi247 / netdata-1 / commits / 05a136e43

fix(go.d): correct unlockall impl (#19154)

Ilya Mashchenko committed Dec 7, 2024 at 19:39 UTC 05a136e43ecdef61211a5718b78884062acaaf30
2 files changed +34 -2
src/go/plugin/go.d/agent/filelock/filelock.go
+3 -2
@@ -55,8 +55,9 @@ func (l *Locker) Unlock(name string) {
55 }
56
57 func (l *Locker) UnlockAll() {
58 - for name := range l.locks {
59 - l.Unlock(name)
58 + for key, locker := range l.locks {
59 + delete(l.locks, key)
60 + _ = locker.Close()
61 }
62 }
63
src/go/plugin/go.d/agent/filelock/filelock_test.go
+31
@@ -97,3 +97,34 @@ func TestLocker_Unlock(t *testing.T) {
97 })
98 }
99 }
100 +
101 +func TestLocker_UnlockAll(t *testing.T) {
102 + tests := map[string]func(t *testing.T, dir string){
103 + "unlock all": func(t *testing.T, dir string) {
104 + reg := New(dir)
105 +
106 + ok, err := reg.Lock("name1")
107 + require.True(t, ok)
108 + require.NoError(t, err)
109 +
110 + ok, err = reg.Lock("name2")
111 + require.True(t, ok)
112 + require.NoError(t, err)
113 +
114 + reg.UnlockAll()
115 +
116 + assert.False(t, reg.isLocked("name1"))
117 + assert.False(t, reg.isLocked("name2"))
118 + },
119 + }
120 +
121 + for name, test := range tests {
122 + t.Run(name, func(t *testing.T) {
123 + dir, err := os.MkdirTemp(os.TempDir(), "netdata-go-test-file-lock-registry")
124 + require.NoError(t, err)
125 + defer func() { require.NoError(t, os.RemoveAll(dir)) }()
126 +
127 + test(t, dir)
128 + })
129 + }
130 +}