-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5888 from heyitsanthony/v2-one-shot
client: make set/delete one shot operations
- Loading branch information
Showing
8 changed files
with
196 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
// Copyright 2016 The etcd Authors | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package integration | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"strings" | ||
"sync/atomic" | ||
"testing" | ||
|
||
"golang.org/x/net/context" | ||
|
||
"github.com/coreos/etcd/client" | ||
"github.com/coreos/etcd/integration" | ||
"github.com/coreos/etcd/pkg/testutil" | ||
) | ||
|
||
// TestV2NoRetryEOF tests destructive api calls won't retry on a disconnection. | ||
func TestV2NoRetryEOF(t *testing.T) { | ||
defer testutil.AfterTest(t) | ||
// generate an EOF response; specify address so appears first in sorted ep list | ||
lEOF := integration.NewListenerWithAddr(t, fmt.Sprintf("eof:123.%d.sock", os.Getpid())) | ||
defer lEOF.Close() | ||
tries := uint32(0) | ||
go func() { | ||
for { | ||
conn, err := lEOF.Accept() | ||
if err != nil { | ||
return | ||
} | ||
atomic.AddUint32(&tries, 1) | ||
conn.Close() | ||
} | ||
}() | ||
eofURL := integration.UrlScheme + "://" + lEOF.Addr().String() | ||
cli := integration.MustNewHTTPClient(t, []string{eofURL, eofURL}, nil) | ||
kapi := client.NewKeysAPI(cli) | ||
for i, f := range noRetryList(kapi) { | ||
startTries := atomic.LoadUint32(&tries) | ||
if err := f(); err == nil { | ||
t.Errorf("#%d: expected EOF error, got nil", i) | ||
} | ||
endTries := atomic.LoadUint32(&tries) | ||
if startTries+1 != endTries { | ||
t.Errorf("#%d: expected 1 try, got %d", i, endTries-startTries) | ||
} | ||
} | ||
} | ||
|
||
// TestV2NoRetryNoLeader tests destructive api calls won't retry if given an error code. | ||
func TestV2NoRetryNoLeader(t *testing.T) { | ||
defer testutil.AfterTest(t) | ||
|
||
lHttp := integration.NewListenerWithAddr(t, fmt.Sprintf("errHttp:123.%d.sock", os.Getpid())) | ||
eh := &errHandler{errCode: http.StatusServiceUnavailable} | ||
srv := httptest.NewUnstartedServer(eh) | ||
defer lHttp.Close() | ||
defer srv.Close() | ||
srv.Listener = lHttp | ||
go srv.Start() | ||
lHttpURL := integration.UrlScheme + "://" + lHttp.Addr().String() | ||
|
||
cli := integration.MustNewHTTPClient(t, []string{lHttpURL, lHttpURL}, nil) | ||
kapi := client.NewKeysAPI(cli) | ||
// test error code | ||
for i, f := range noRetryList(kapi) { | ||
reqs := eh.reqs | ||
if err := f(); err == nil || !strings.Contains(err.Error(), "no leader") { | ||
t.Errorf("#%d: expected \"no leader\", got %v", i, err) | ||
} | ||
if eh.reqs != reqs+1 { | ||
t.Errorf("#%d: expected 1 request, got %d", i, eh.reqs-reqs) | ||
} | ||
} | ||
} | ||
|
||
// TestV2RetryRefuse tests destructive api calls will retry if a connection is refused. | ||
func TestV2RetryRefuse(t *testing.T) { | ||
defer testutil.AfterTest(t) | ||
cl := integration.NewCluster(t, 1) | ||
cl.Launch(t) | ||
defer cl.Terminate(t) | ||
// test connection refused; expect no error failover | ||
cli := integration.MustNewHTTPClient(t, []string{integration.UrlScheme + "://refuseconn:123", cl.URL(0)}, nil) | ||
kapi := client.NewKeysAPI(cli) | ||
if _, err := kapi.Set(context.Background(), "/delkey", "def", nil); err != nil { | ||
t.Fatal(err) | ||
} | ||
for i, f := range noRetryList(kapi) { | ||
if err := f(); err != nil { | ||
t.Errorf("#%d: unexpected retry failure (%v)", i, err) | ||
} | ||
} | ||
} | ||
|
||
type errHandler struct { | ||
errCode int | ||
reqs int | ||
} | ||
|
||
func (eh *errHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) { | ||
req.Body.Close() | ||
eh.reqs++ | ||
w.WriteHeader(eh.errCode) | ||
} | ||
|
||
func noRetryList(kapi client.KeysAPI) []func() error { | ||
return []func() error{ | ||
func() error { | ||
opts := &client.SetOptions{PrevExist: client.PrevNoExist} | ||
_, err := kapi.Set(context.Background(), "/setkey", "bar", opts) | ||
return err | ||
}, | ||
func() error { | ||
_, err := kapi.Delete(context.Background(), "/delkey", nil) | ||
return err | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2013 The Go Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style | ||
// license that can be found in the LICENSE file. | ||
|
||
package integration | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/coreos/etcd/pkg/testutil" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
v := m.Run() | ||
if v == 0 && testutil.CheckLeakedGoroutine() { | ||
os.Exit(1) | ||
} | ||
os.Exit(v) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.