Merge: - clients: fix rename

Close #1340

Squashed commit of the following:

commit 95f0291c681f98c29f4014b651c159d387301af4
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Jan 9 18:56:23 2020 +0300

    add test

commit 293be277e245ff2f430e8c1e9ee3e82dc7da0995
Author: Simon Zolin <s.zolin@adguard.com>
Date:   Thu Jan 9 16:04:36 2020 +0300

    - clients: fix rename
This commit is contained in:
Simon Zolin 2020-01-10 15:28:58 +03:00
parent 84b11ff7a5
commit 0cd25cf598
2 changed files with 7 additions and 2 deletions

View File

@ -468,12 +468,16 @@ func (clients *clientsContainer) Update(name string, c Client) error {
// update Name index
if old.Name != c.Name {
delete(clients.list, old.Name)
clients.list[c.Name] = old
}
// update upstreams cache
delete(clients.upstreamsCache, name)
delete(clients.upstreamsCache, old.Name)
if old.Name != c.Name {
delete(clients.upstreamsCache, old.Name)
} else {
delete(clients.upstreamsCache, c.Name)
}
*old = c
return nil

View File

@ -114,6 +114,7 @@ func TestClients(t *testing.T) {
c = Client{}
c, b = clients.Find("1.1.1.2")
assert.True(t, b && c.Name == "client1-renamed" && c.IDs[0] == "1.1.1.2" && c.UseOwnSettings)
assert.True(t, clients.list["client1"] == nil)
// failed remove - no such name
if clients.Del("client3") {