mirror of
https://github.com/optim-enterprises-bv/vault.git
synced 2025-11-02 03:27:54 +00:00
Fix properly initialize replicateStateStore from SetReadYourWrites() (#13486)
Fixes an issue where the `replicateStateStore` was being set to `nil` upon consecutive calls to `client.SetReadYourWrites(true)`.
This commit is contained in:
@@ -20,9 +20,9 @@ import (
|
|||||||
"unicode"
|
"unicode"
|
||||||
|
|
||||||
"github.com/hashicorp/errwrap"
|
"github.com/hashicorp/errwrap"
|
||||||
cleanhttp "github.com/hashicorp/go-cleanhttp"
|
"github.com/hashicorp/go-cleanhttp"
|
||||||
retryablehttp "github.com/hashicorp/go-retryablehttp"
|
"github.com/hashicorp/go-retryablehttp"
|
||||||
rootcerts "github.com/hashicorp/go-rootcerts"
|
"github.com/hashicorp/go-rootcerts"
|
||||||
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
"github.com/hashicorp/go-secure-stdlib/parseutil"
|
||||||
"golang.org/x/net/http2"
|
"golang.org/x/net/http2"
|
||||||
"golang.org/x/time/rate"
|
"golang.org/x/time/rate"
|
||||||
@@ -880,8 +880,10 @@ func (c *Client) SetReadYourWrites(preventStaleReads bool) {
|
|||||||
c.config.modifyLock.Lock()
|
c.config.modifyLock.Lock()
|
||||||
defer c.config.modifyLock.Unlock()
|
defer c.config.modifyLock.Unlock()
|
||||||
|
|
||||||
if preventStaleReads && c.replicationStateStore == nil {
|
if preventStaleReads {
|
||||||
c.replicationStateStore = &replicationStateStore{}
|
if c.replicationStateStore == nil {
|
||||||
|
c.replicationStateStore = &replicationStateStore{}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
c.replicationStateStore = nil
|
c.replicationStateStore = nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -986,3 +986,69 @@ func TestClient_ReadYourWrites(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClient_SetReadYourWrites(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
config *Config
|
||||||
|
calls []bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "false",
|
||||||
|
config: &Config{},
|
||||||
|
calls: []bool{false},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "true",
|
||||||
|
config: &Config{},
|
||||||
|
calls: []bool{true},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-false",
|
||||||
|
config: &Config{},
|
||||||
|
calls: []bool{false, false},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-true",
|
||||||
|
config: &Config{},
|
||||||
|
calls: []bool{true, true},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "multi-mix",
|
||||||
|
config: &Config{},
|
||||||
|
calls: []bool{false, true, false, true},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
assertSetReadYourRights := func(t *testing.T, c *Client, v bool, s *replicationStateStore) {
|
||||||
|
t.Helper()
|
||||||
|
c.SetReadYourWrites(v)
|
||||||
|
if c.config.ReadYourWrites != v {
|
||||||
|
t.Fatalf("expected config.ReadYourWrites %#v, actual %#v", v, c.config.ReadYourWrites)
|
||||||
|
}
|
||||||
|
if !reflect.DeepEqual(s, c.replicationStateStore) {
|
||||||
|
t.Fatalf("expected replicationStateStore %#v, actual %#v", s, c.replicationStateStore)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for _, tt := range tests {
|
||||||
|
t.Run(tt.name, func(t *testing.T) {
|
||||||
|
c := &Client{
|
||||||
|
config: tt.config,
|
||||||
|
}
|
||||||
|
for i, v := range tt.calls {
|
||||||
|
var expectStateStore *replicationStateStore
|
||||||
|
if v {
|
||||||
|
if c.replicationStateStore == nil {
|
||||||
|
c.replicationStateStore = &replicationStateStore{
|
||||||
|
store: []string{},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
c.replicationStateStore.store = append(c.replicationStateStore.store,
|
||||||
|
fmt.Sprintf("%s-%d", tt.name, i))
|
||||||
|
expectStateStore = c.replicationStateStore
|
||||||
|
}
|
||||||
|
assertSetReadYourRights(t, c, v, expectStateStore)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
3
changelog/13486.txt
Normal file
3
changelog/13486.txt
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
```release-note:bug
|
||||||
|
api/client: Fixes an issue where the `replicateStateStore` was being set to `nil` upon consecutive calls to `client.SetReadYourWrites(true)`.
|
||||||
|
```
|
||||||
Reference in New Issue
Block a user