Making all operations synchronous

This commit is contained in:
nikhiljindal
2015-01-21 21:20:57 -08:00
parent 790a78415e
commit de60600608
15 changed files with 117 additions and 527 deletions

View File

@@ -39,7 +39,7 @@ import (
// specialParams lists parameters that are handled specially and which users of Request
// are therefore not allowed to set manually.
var specialParams = util.NewStringSet("sync", "timeout")
var specialParams = util.NewStringSet("timeout")
// PollFunc is called when a server operation returns 202 accepted. The name of the
// operation is extracted from the response and passed to this function. Return a
@@ -107,7 +107,6 @@ type Request struct {
resource string
resourceName string
selector labels.Selector
sync bool
timeout time.Duration
// output
@@ -177,15 +176,6 @@ func (r *Request) Name(resourceName string) *Request {
return r
}
// Sync sets sync/async call status by setting the "sync" parameter to "true"/"false".
func (r *Request) Sync(sync bool) *Request {
if r.err != nil {
return r
}
r.sync = sync
return r
}
// Namespace applies the namespace scope to a request (<resource>/[ns/<namespace>/]<name>)
func (r *Request) Namespace(namespace string) *Request {
if r.err != nil {
@@ -270,7 +260,7 @@ func (r *Request) setParam(paramName, value string) *Request {
}
// Timeout makes the request use the given duration as a timeout. Sets the "timeout"
// parameter. Ignored if sync=false.
// parameter.
func (r *Request) Timeout(d time.Duration) *Request {
if r.err != nil {
return r
@@ -359,13 +349,9 @@ func (r *Request) finalURL() string {
query.Add("namespace", r.namespace)
}
// sync and timeout are handled specially here, to allow setting them
// in any order.
if r.sync {
query.Add("sync", "true")
if r.timeout != 0 {
query.Add("timeout", r.timeout.String())
}
// timeout is handled specially here.
if r.timeout != 0 {
query.Add("timeout", r.timeout.String())
}
finalURL.RawQuery = query.Encode()
return finalURL.String()

View File

@@ -64,8 +64,7 @@ func TestRequestWithErrorWontChange(t *testing.T) {
NoPoll().
Body("foo").
Poller(skipPolling).
Timeout(time.Millisecond).
Sync(true)
Timeout(time.Millisecond)
if changed != &r {
t.Errorf("returned request should point to the same object")
}
@@ -501,7 +500,7 @@ func TestDoRequestNewWay(t *testing.T) {
} else if !reflect.DeepEqual(obj, expectedObj) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo", "POST", &reqBody)
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &reqBody)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
@@ -524,7 +523,6 @@ func TestDoRequestNewWayReader(t *testing.T) {
Name("baz").
Prefix("foo").
SelectorParam("labels", labels.Set{"name": "foo"}.AsSelector()).
Sync(true).
Timeout(time.Second).
Body(bytes.NewBuffer(reqBodyExpected)).
Do().Get()
@@ -538,7 +536,7 @@ func TestDoRequestNewWayReader(t *testing.T) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&sync=true&timeout=1s", "POST", &tmpStr)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
@@ -574,7 +572,7 @@ func TestDoRequestNewWayObj(t *testing.T) {
t.Errorf("Expected: %#v, got %#v", expectedObj, obj)
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
fakeHandler.ValidateRequest(t, "/api/v1beta2/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
@@ -626,7 +624,7 @@ func TestDoRequestNewWayFile(t *testing.T) {
t.Errorf("expected object was not created")
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "POST", &tmpStr)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "POST", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
@@ -669,7 +667,7 @@ func TestWasCreated(t *testing.T) {
}
tmpStr := string(reqBodyExpected)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo", "PUT", &tmpStr)
fakeHandler.ValidateRequest(t, "/api/v1beta1/foo/bar/baz?labels=name%3Dfoo&timeout=1s", "PUT", &tmpStr)
if fakeHandler.RequestReceived.Header["Authorization"] == nil {
t.Errorf("Request is missing authorization header: %#v", *fakeHandler.RequestReceived)
}
@@ -700,22 +698,6 @@ func TestAbsPath(t *testing.T) {
}
}
func TestSync(t *testing.T) {
c := NewOrDie(&Config{})
r := c.Get()
if r.sync {
t.Errorf("sync has wrong default")
}
r.Sync(false)
if r.sync {
t.Errorf("'Sync' doesn't work")
}
r.Sync(true)
if !r.sync {
t.Errorf("'Sync' doesn't work")
}
}
func TestUintParam(t *testing.T) {
table := []struct {
name string
@@ -742,7 +724,6 @@ func TestUnacceptableParamNames(t *testing.T) {
testVal string
expectSuccess bool
}{
{"sync", "foo", false},
{"timeout", "42", false},
}

View File

@@ -55,7 +55,6 @@ type RESTClient struct {
// be called.
Poller PollFunc
Sync bool
PollPeriod time.Duration
Timeout time.Duration
}
@@ -80,10 +79,7 @@ func NewRESTClient(baseURL *url.URL, apiVersion string, c runtime.Codec, legacyB
LegacyBehavior: legacyBehavior,
// Make asynchronous requests by default
Sync: false,
// Poll frequently when asynchronous requests are provided
// Poll frequently
PollPeriod: time.Second * 2,
}
}
@@ -110,7 +106,7 @@ func (c *RESTClient) Verb(verb string) *Request {
if poller == nil {
poller = c.DefaultPoll
}
return NewRequest(c.Client, verb, c.baseURL, c.Codec, c.LegacyBehavior, c.LegacyBehavior).Poller(poller).Sync(c.Sync).Timeout(c.Timeout)
return NewRequest(c.Client, verb, c.baseURL, c.Codec, c.LegacyBehavior, c.LegacyBehavior).Poller(poller).Timeout(c.Timeout)
}
// Post begins a POST request. Short for c.Verb("POST").
@@ -135,7 +131,7 @@ func (c *RESTClient) Delete() *Request {
// PollFor makes a request to do a single poll of the completion of the given operation.
func (c *RESTClient) Operation(name string) *Request {
return c.Get().Resource("operations").Name(name).Sync(false).NoPoll()
return c.Get().Resource("operations").Name(name).NoPoll()
}
// DefaultPoll performs a polling action based on the PollPeriod set on the Client.