本文整理汇总了Golang中github.com/coreos/locksmith/Godeps/_workspace/src/golang.org/x/net/context.Background函数的典型用法代码示例。如果您正苦于以下问题:Golang Background函数的具体用法?Golang Background怎么用?Golang Background使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Background函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestSimpleHTTPClientDoCancelContextResponseBodyClosed
func TestSimpleHTTPClientDoCancelContextResponseBodyClosed(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}
// create an already-cancelled context
ctx, cancel := context.WithCancel(context.Background())
cancel()
body := &checkableReadCloser{ReadCloser: ioutil.NopCloser(strings.NewReader("foo"))}
go func() {
// wait that simpleHTTPClient knows the context is already timed out,
// and calls CancelRequest
testutil.WaitSchedule()
// response is returned before cancel effects
tr.respchan <- &http.Response{Body: body}
}()
_, _, err := c.Do(ctx, &fakeAction{})
if err == nil {
t.Fatalf("expected non-nil error, got nil")
}
if !body.closed {
t.Fatalf("expected closed body")
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:27,代码来源:client_test.go
示例2: TestSimpleHTTPClientDoCancelContextWaitForRoundTrip
func TestSimpleHTTPClientDoCancelContextWaitForRoundTrip(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}
donechan := make(chan struct{})
ctx, cancel := context.WithCancel(context.Background())
go func() {
c.Do(ctx, &fakeAction{})
close(donechan)
}()
// This should call CancelRequest and begin the cancellation process
cancel()
select {
case <-donechan:
t.Fatalf("simpleHTTPClient.Do should not have exited yet")
default:
}
tr.finishCancel <- struct{}{}
select {
case <-donechan:
//expected behavior
return
case <-time.After(time.Second):
t.Fatalf("simpleHTTPClient.Do did not exit within 1s")
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:30,代码来源:client_test.go
示例3: TestHTTPKeysAPIGetResponse
func TestHTTPKeysAPIGetResponse(t *testing.T) {
client := &staticHTTPClient{
resp: http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"X-Etcd-Index": []string{"42"}},
},
body: []byte(`{"action":"get","node":{"key":"/pants/foo/bar","modifiedIndex":25,"createdIndex":19,"nodes":[{"key":"/pants/foo/bar/baz","value":"snarf","createdIndex":21,"modifiedIndex":25}]}}`),
}
wantResponse := &Response{
Action: "get",
Node: &Node{
Key: "/pants/foo/bar",
Nodes: []*Node{
{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: 21, ModifiedIndex: 25},
},
CreatedIndex: uint64(19),
ModifiedIndex: uint64(25),
},
Index: uint64(42),
}
kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
resp, err := kAPI.Get(context.Background(), "/foo/bar", &GetOptions{Recursive: true})
if err != nil {
t.Errorf("non-nil error: %#v", err)
}
if !reflect.DeepEqual(wantResponse, resp) {
t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:31,代码来源:keys_test.go
示例4: TestHTTPClusterClientSyncFail
func TestHTTPClusterClientSyncFail(t *testing.T) {
cf := newStaticHTTPClientFactory([]staticHTTPResponse{
{err: errors.New("fail!")},
})
hc := &httpClusterClient{
clientFactory: cf,
rand: rand.New(rand.NewSource(0)),
}
err := hc.SetEndpoints([]string{"http://127.0.0.1:2379"})
if err != nil {
t.Fatalf("unexpected error during setup: %#v", err)
}
want := []string{"http://127.0.0.1:2379"}
got := hc.Endpoints()
if !reflect.DeepEqual(want, got) {
t.Fatalf("incorrect endpoints: want=%#v got=%#v", want, got)
}
err = hc.Sync(context.Background())
if err == nil {
t.Fatalf("got nil error during Sync")
}
got = hc.Endpoints()
if !reflect.DeepEqual(want, got) {
t.Fatalf("incorrect endpoints after failed Sync: want=%#v got=%#v", want, got)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:30,代码来源:client_test.go
示例5: TestHTTPKeysAPIDeleteResponse
func TestHTTPKeysAPIDeleteResponse(t *testing.T) {
client := &staticHTTPClient{
resp: http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"X-Etcd-Index": []string{"22"}},
},
body: []byte(`{"action":"delete","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":22,"createdIndex":19},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
}
wantResponse := &Response{
Action: "delete",
Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(22)},
PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
Index: uint64(22),
}
kAPI := &httpKeysAPI{client: client, prefix: "/pants"}
resp, err := kAPI.Delete(context.Background(), "/foo/bar/baz", nil)
if err != nil {
t.Errorf("non-nil error: %#v", err)
}
if !reflect.DeepEqual(wantResponse, resp) {
t.Errorf("incorrect Response: want=%#v got=%#v", wantResponse, resp)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:25,代码来源:keys_test.go
示例6: TestHTTPKeysAPIDeleteError
func TestHTTPKeysAPIDeleteError(t *testing.T) {
tests := []httpClient{
// generic HTTP client failure
&staticHTTPClient{
err: errors.New("fail!"),
},
// unusable status code
&staticHTTPClient{
resp: http.Response{
StatusCode: http.StatusTeapot,
},
},
// etcd Error response
&staticHTTPClient{
resp: http.Response{
StatusCode: http.StatusInternalServerError,
},
body: []byte(`{"errorCode":300,"message":"Raft internal error","cause":"/foo","index":18}`),
},
}
for i, tt := range tests {
kAPI := httpKeysAPI{client: tt}
resp, err := kAPI.Delete(context.Background(), "/foo", nil)
if err == nil {
t.Errorf("#%d: received nil error", i)
}
if resp != nil {
t.Errorf("#%d: received non-nil Response: %#v", i, resp)
}
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:34,代码来源:keys_test.go
示例7: TestHTTPMembersAPIListError
func TestHTTPMembersAPIListError(t *testing.T) {
tests := []httpClient{
// generic httpClient failure
&staticHTTPClient{err: errors.New("fail!")},
// unrecognized HTTP status code
&staticHTTPClient{
resp: http.Response{StatusCode: http.StatusTeapot},
},
// fail to unmarshal body on StatusOK
&staticHTTPClient{
resp: http.Response{
StatusCode: http.StatusOK,
},
body: []byte(`[{"id":"XX`),
},
}
for i, tt := range tests {
mAPI := &httpMembersAPI{client: tt}
ms, err := mAPI.List(context.Background())
if err == nil {
t.Errorf("#%d: got nil err", i)
}
if ms != nil {
t.Errorf("#%d: got non-nil Member slice", i)
}
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:30,代码来源:members_test.go
示例8: TestHTTPMembersAPIListSuccess
func TestHTTPMembersAPIListSuccess(t *testing.T) {
wantAction := &membersAPIActionList{}
mAPI := &httpMembersAPI{
client: &actionAssertingHTTPClient{
t: t,
act: wantAction,
resp: http.Response{
StatusCode: http.StatusOK,
},
body: []byte(`{"members":[{"id":"94088180e21eb87b","name":"node2","peerURLs":["http://127.0.0.1:7002"],"clientURLs":["http://127.0.0.1:4002"]}]}`),
},
}
wantResponseMembers := []Member{
{
ID: "94088180e21eb87b",
Name: "node2",
PeerURLs: []string{"http://127.0.0.1:7002"},
ClientURLs: []string{"http://127.0.0.1:4002"},
},
}
m, err := mAPI.List(context.Background())
if err != nil {
t.Errorf("got non-nil err: %#v", err)
}
if !reflect.DeepEqual(wantResponseMembers, m) {
t.Errorf("incorrect Members: want=%#v got=%#v", wantResponseMembers, m)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:30,代码来源:members_test.go
示例9: TestHTTPClusterClientDoDeadlineExceedContext
func TestHTTPClusterClientDoDeadlineExceedContext(t *testing.T) {
fakeURL := url.URL{}
tr := newFakeTransport()
tr.finishCancel <- struct{}{}
c := &httpClusterClient{
clientFactory: newHTTPClientFactory(tr, DefaultCheckRedirect, 0),
endpoints: []url.URL{fakeURL},
}
errc := make(chan error)
go func() {
ctx, cancel := context.WithTimeout(context.Background(), time.Millisecond)
defer cancel()
_, _, err := c.Do(ctx, &fakeAction{})
errc <- err
}()
select {
case err := <-errc:
if err != context.DeadlineExceeded {
t.Errorf("err = %+v, want %+v", err, context.DeadlineExceeded)
}
case <-time.After(time.Second):
t.Fatalf("unexpected timeout when waitting for request to deadline exceed")
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:26,代码来源:client_test.go
示例10: TestHTTPMembersAPIAddSuccess
func TestHTTPMembersAPIAddSuccess(t *testing.T) {
wantAction := &membersAPIActionAdd{
peerURLs: types.URLs([]url.URL{
{Scheme: "http", Host: "127.0.0.1:7002"},
}),
}
mAPI := &httpMembersAPI{
client: &actionAssertingHTTPClient{
t: t,
act: wantAction,
resp: http.Response{
StatusCode: http.StatusCreated,
},
body: []byte(`{"id":"94088180e21eb87b","peerURLs":["http://127.0.0.1:7002"]}`),
},
}
wantResponseMember := &Member{
ID: "94088180e21eb87b",
PeerURLs: []string{"http://127.0.0.1:7002"},
}
m, err := mAPI.Add(context.Background(), "http://127.0.0.1:7002")
if err != nil {
t.Errorf("got non-nil err: %#v", err)
}
if !reflect.DeepEqual(wantResponseMember, m) {
t.Errorf("incorrect Member: want=%#v got=%#v", wantResponseMember, m)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:31,代码来源:members_test.go
示例11: TestNoTimeout
func TestNoTimeout(t *testing.T) {
ctx := context.Background()
resp, err := doRequest(ctx)
if resp == nil || err != nil {
t.Fatalf("error received from client: %v %v", err, resp)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:8,代码来源:ctxhttp_test.go
示例12: TestHTTPKeysAPICreateInOrderAction
func TestHTTPKeysAPICreateInOrderAction(t *testing.T) {
act := &createInOrderAction{
Dir: "/foo",
Value: "bar",
TTL: 0,
}
kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
kAPI.CreateInOrder(context.Background(), "/foo", "bar", nil)
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:9,代码来源:keys_test.go
示例13: TestSimpleHTTPClientDoError
func TestSimpleHTTPClientDoError(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}
tr.errchan <- errors.New("fixture")
_, _, err := c.Do(context.Background(), &fakeAction{})
if err == nil {
t.Fatalf("expected non-nil error, got nil")
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:11,代码来源:client_test.go
示例14: TestSimpleHTTPClientDoCancelContext
func TestSimpleHTTPClientDoCancelContext(t *testing.T) {
tr := newFakeTransport()
c := &simpleHTTPClient{transport: tr}
tr.startCancel <- struct{}{}
tr.finishCancel <- struct{}{}
_, _, err := c.Do(context.Background(), &fakeAction{})
if err == nil {
t.Fatalf("expected non-nil error, got nil")
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:12,代码来源:client_test.go
示例15: TestHTTPKeysAPIDeleteAction
func TestHTTPKeysAPIDeleteAction(t *testing.T) {
tests := []struct {
key string
value string
opts *DeleteOptions
wantAction httpAction
}{
// nil DeleteOptions
{
key: "/foo",
opts: nil,
wantAction: &deleteAction{
Key: "/foo",
PrevValue: "",
PrevIndex: 0,
Recursive: false,
},
},
// empty DeleteOptions
{
key: "/foo",
opts: &DeleteOptions{},
wantAction: &deleteAction{
Key: "/foo",
PrevValue: "",
PrevIndex: 0,
Recursive: false,
},
},
// populated DeleteOptions
{
key: "/foo",
opts: &DeleteOptions{
PrevValue: "baz",
PrevIndex: 13,
Recursive: true,
},
wantAction: &deleteAction{
Key: "/foo",
PrevValue: "baz",
PrevIndex: 13,
Recursive: true,
},
},
}
for i, tt := range tests {
client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
kAPI := httpKeysAPI{client: client}
kAPI.Delete(context.Background(), tt.key, tt.opts)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:52,代码来源:keys_test.go
示例16: ExampleWithTimeout
func ExampleWithTimeout() {
// Pass a context with a timeout to tell a blocking function that it
// should abandon its work after the timeout elapses.
ctx, _ := context.WithTimeout(context.Background(), 100*time.Millisecond)
select {
case <-time.After(200 * time.Millisecond):
fmt.Println("overslept")
case <-ctx.Done():
fmt.Println(ctx.Err()) // prints "context deadline exceeded"
}
// Output:
// context deadline exceeded
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:13,代码来源:withtimeout_test.go
示例17: TestHTTPKeysAPIUpdateAction
func TestHTTPKeysAPIUpdateAction(t *testing.T) {
act := &setAction{
Key: "/foo",
Value: "bar",
PrevExist: PrevExist,
PrevIndex: 0,
PrevValue: "",
TTL: 0,
}
kAPI := httpKeysAPI{client: &actionAssertingHTTPClient{t: t, act: act}}
kAPI.Update(context.Background(), "/foo", "bar")
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:13,代码来源:keys_test.go
示例18: TestHTTPWatcherNextWaitAction
func TestHTTPWatcherNextWaitAction(t *testing.T) {
initAction := waitAction{
Prefix: "/pants",
Key: "/foo/bar",
Recursive: true,
WaitIndex: 19,
}
client := &actionAssertingHTTPClient{
t: t,
act: &initAction,
resp: http.Response{
StatusCode: http.StatusOK,
Header: http.Header{"X-Etcd-Index": []string{"42"}},
},
body: []byte(`{"action":"update","node":{"key":"/pants/foo/bar/baz","value":"snarf","modifiedIndex":21,"createdIndex":19},"prevNode":{"key":"/pants/foo/bar/baz","value":"snazz","modifiedIndex":20,"createdIndex":19}}`),
}
wantResponse := &Response{
Action: "update",
Node: &Node{Key: "/pants/foo/bar/baz", Value: "snarf", CreatedIndex: uint64(19), ModifiedIndex: uint64(21)},
PrevNode: &Node{Key: "/pants/foo/bar/baz", Value: "snazz", CreatedIndex: uint64(19), ModifiedIndex: uint64(20)},
Index: uint64(42),
}
wantNextWait := waitAction{
Prefix: "/pants",
Key: "/foo/bar",
Recursive: true,
WaitIndex: 22,
}
watcher := &httpWatcher{
client: client,
nextWait: initAction,
}
resp, err := watcher.Next(context.Background())
if err != nil {
t.Errorf("non-nil error: %#v", err)
}
if !reflect.DeepEqual(wantResponse, resp) {
t.Errorf("received incorrect Response: want=%#v got=%#v", wantResponse, resp)
}
if !reflect.DeepEqual(wantNextWait, watcher.nextWait) {
t.Errorf("nextWait incorrect: want=%#v got=%#v", wantNextWait, watcher.nextWait)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:50,代码来源:keys_test.go
示例19: TestCancelAfterRequest
func TestCancelAfterRequest(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
resp, err := doRequest(ctx)
// Cancel before reading the body.
// Request.Body should still be readable after the context is canceled.
cancel()
b, err := ioutil.ReadAll(resp.Body)
if err != nil || string(b) != requestBody {
t.Fatalf("could not read body: %q %v", b, err)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:14,代码来源:ctxhttp_test.go
示例20: TestHTTPKeysAPIGetAction
func TestHTTPKeysAPIGetAction(t *testing.T) {
tests := []struct {
key string
opts *GetOptions
wantAction httpAction
}{
// nil GetOptions
{
key: "/foo",
opts: nil,
wantAction: &getAction{
Key: "/foo",
Sorted: false,
Recursive: false,
},
},
// empty GetOptions
{
key: "/foo",
opts: &GetOptions{},
wantAction: &getAction{
Key: "/foo",
Sorted: false,
Recursive: false,
},
},
// populated GetOptions
{
key: "/foo",
opts: &GetOptions{
Sort: true,
Recursive: true,
Quorum: true,
},
wantAction: &getAction{
Key: "/foo",
Sorted: true,
Recursive: true,
Quorum: true,
},
},
}
for i, tt := range tests {
client := &actionAssertingHTTPClient{t: t, num: i, act: tt.wantAction}
kAPI := httpKeysAPI{client: client}
kAPI.Get(context.Background(), tt.key, tt.opts)
}
}
开发者ID:carriercomm,项目名称:locksmith,代码行数:49,代码来源:keys_test.go
注:本文中的github.com/coreos/locksmith/Godeps/_workspace/src/golang.org/x/net/context.Background函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论