Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update api.Client for isolated read-after-write support #1188

Merged
merged 12 commits into from
Oct 14, 2021
50 changes: 32 additions & 18 deletions vault/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,36 @@ func testHTTPServer(t *testing.T, address string, handler http.Handler) (net.Lis
return ln, server
}

func sendRequest(client *api.Client, headerValue string) (*api.Response, error) {
req := client.NewRequest("GET", "/"+headerValue)
req.Headers.Set(indexHeaderName, headerValue)
resp, err := client.RawRequestWithContext(context.Background(), req)
if err != nil {
return nil, err
}

return resp, nil
}

func setupServer(t *testing.T, handler http.Handler) (*ClientFactory, *http.Server) {
config := api.DefaultConfig()

ln, server := testHTTPServer(t, "127.0.0.1:0", handler)

config.Address = fmt.Sprintf("http://%s", ln.Addr())

w, err := NewClientFactory(config)
if err != nil {
t.Fatal(err)
}

if actual := w.Client(); !reflect.DeepEqual(actual, w.client) {
benashz marked this conversation as resolved.
Show resolved Hide resolved
t.Errorf("Client(): expected %v, actual %v", actual, w.client)
}

return w, server
}

func TestClientFactory_Client(t *testing.T) {
b64enc := func(s string) string {
return base64.StdEncoding.EncodeToString([]byte(s))
Expand Down Expand Up @@ -249,32 +279,18 @@ func TestClientFactory_Client(t *testing.T) {
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
config := api.DefaultConfig()

ln, server := testHTTPServer(t, "127.0.0.1:0", tt.handler)
w, server := setupServer(t, tt.handler)
defer server.Close()

config.Address = fmt.Sprintf("http://%s", ln.Addr())

w, err := NewClientFactory(config)
if err != nil {
t.Fatal(err)
}

if actual := w.Client(); !reflect.DeepEqual(actual, w.client) {
t.Errorf("Client(): expected %v, actual %v", actual, w.client)
}

client := w.Client()
var wg sync.WaitGroup
for _, expected := range tt.states {
wg.Add(1)
go func(expected string) {
defer wg.Done()

req := client.NewRequest("GET", "/"+expected)
req.Headers.Set(indexHeaderName, expected)
resp, err := client.RawRequestWithContext(context.Background(), req)
resp, err := sendRequest(client, expected)
if err != nil {
t.Fatal(err)
}
Expand All @@ -293,5 +309,3 @@ func TestClientFactory_Client(t *testing.T) {
})
}
}

// TODO : add tests for Clone() especially focused on testing concurrency