本文整理汇总了Golang中context.Background函数的典型用法代码示例。如果您正苦于以下问题:Golang Background函数的具体用法?Golang Background怎么用?Golang Background使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Background函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Golang代码示例。
示例1: TestVaultClient_LookupToken
func TestVaultClient_LookupToken(t *testing.T) {
v := testutil.NewTestVault(t).Start()
defer v.Stop()
logger := log.New(os.Stderr, "", log.LstdFlags)
client, err := NewVaultClient(v.Config, logger, nil)
if err != nil {
t.Fatalf("failed to build vault client: %v", err)
}
client.SetActive(true)
defer client.Stop()
waitForConnection(client, t)
// Lookup ourselves
s, err := client.LookupToken(context.Background(), v.Config.Token)
if err != nil {
t.Fatalf("self lookup failed: %v", err)
}
policies, err := PoliciesFrom(s)
if err != nil {
t.Fatalf("failed to parse policies: %v", err)
}
expected := []string{"root"}
if !reflect.DeepEqual(policies, expected) {
t.Fatalf("Unexpected policies; got %v; want %v", policies, expected)
}
// Create a token with a different set of policies
expected = []string{"default"}
req := vapi.TokenCreateRequest{
Policies: expected,
}
s, err = v.Client.Auth().Token().Create(&req)
if err != nil {
t.Fatalf("failed to create child token: %v", err)
}
// Get the client token
if s == nil || s.Auth == nil {
t.Fatalf("bad secret response: %+v", s)
}
// Lookup new child
s, err = client.LookupToken(context.Background(), s.Auth.ClientToken)
if err != nil {
t.Fatalf("self lookup failed: %v", err)
}
policies, err = PoliciesFrom(s)
if err != nil {
t.Fatalf("failed to parse policies: %v", err)
}
if !reflect.DeepEqual(policies, expected) {
t.Fatalf("Unexpected policies; got %v; want %v", policies, expected)
}
}
开发者ID:achanda,项目名称:nomad,代码行数:60,代码来源:vault_test.go
示例2: TestVaultClient_RevokeTokens_PreEstablishs
func TestVaultClient_RevokeTokens_PreEstablishs(t *testing.T) {
v := testutil.NewTestVault(t)
logger := log.New(os.Stderr, "", log.LstdFlags)
client, err := NewVaultClient(v.Config, logger, nil)
if err != nil {
t.Fatalf("failed to build vault client: %v", err)
}
client.SetActive(true)
defer client.Stop()
// Create some VaultAccessors
vas := []*structs.VaultAccessor{
mock.VaultAccessor(),
mock.VaultAccessor(),
}
if err := client.RevokeTokens(context.Background(), vas, false); err != nil {
t.Fatalf("RevokeTokens failed: %v", err)
}
// Wasn't committed
if len(client.revoking) != 0 {
t.Fatalf("didn't add to revoke loop")
}
if err := client.RevokeTokens(context.Background(), vas, true); err != nil {
t.Fatalf("RevokeTokens failed: %v", err)
}
// Was committed
if len(client.revoking) != 2 {
t.Fatalf("didn't add to revoke loop")
}
}
开发者ID:achanda,项目名称:nomad,代码行数:34,代码来源:vault_test.go
示例3: TestInsert
func TestInsert(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
s, err := mgo.Dial("")
if !assert.NoError(t, err) {
return
}
defer cleanup(s, "testinsert")()
h := NewHandler(s, "testinsert", "test")
items := []*resource.Item{
{
ID: "1234",
ETag: "etag",
Updated: now,
Payload: map[string]interface{}{
"id": "1234",
"foo": "bar",
},
},
}
err = h.Insert(context.Background(), items)
assert.NoError(t, err)
d := map[string]interface{}{}
err = s.DB("testinsert").C("test").FindId("1234").One(&d)
if !assert.NoError(t, err) {
return
}
assert.Equal(t, map[string]interface{}{"foo": "bar", "_id": "1234", "_etag": "etag", "_updated": now}, d)
// Inserting same item twice should return a conflict error
err = h.Insert(context.Background(), items)
assert.Equal(t, resource.ErrConflict, err)
}
开发者ID:rs,项目名称:rest-layer-mongo,代码行数:34,代码来源:mongo_test.go
示例4: TestRoutingResolve
func TestRoutingResolve(t *testing.T) {
dstore := dssync.MutexWrap(ds.NewMapDatastore())
serv := mockrouting.NewServer()
id := testutil.RandIdentityOrFatal(t)
d := serv.ClientWithDatastore(context.Background(), id, dstore)
resolver := NewRoutingResolver(d, 0)
publisher := NewRoutingPublisher(d, dstore)
privk, pubk, err := testutil.RandTestKeyPair(512)
if err != nil {
t.Fatal(err)
}
h := path.FromString("/ipfs/QmZULkCELmmk5XNfCgTnCyFgAVxBRBXyDHGGMVoLFLiXEN")
err = publisher.Publish(context.Background(), privk, h)
if err != nil {
t.Fatal(err)
}
pubkb, err := pubk.Bytes()
if err != nil {
t.Fatal(err)
}
pkhash := u.Hash(pubkb)
res, err := resolver.Resolve(context.Background(), key.Key(pkhash).B58String())
if err != nil {
t.Fatal(err)
}
if res != h {
t.Fatal("Got back incorrect value.")
}
}
开发者ID:qnib,项目名称:go-ipfs,代码行数:35,代码来源:resolve_test.go
示例5: TestHandlerPostListWithReference
func TestHandlerPostListWithReference(t *testing.T) {
s := mem.NewHandler()
s.Insert(context.Background(), []*resource.Item{{ID: "ref", Payload: map[string]interface{}{"id": "ref"}}})
index := resource.NewIndex()
index.Bind("foo", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
bar := index.Bind("bar", schema.Schema{Fields: schema.Fields{
"id": {},
"foo": {Validator: &schema.Reference{Path: "foo"}},
}}, s, resource.DefaultConf)
r, _ := http.NewRequest("POST", "/bar", bytes.NewBufferString(`{"id": "1", "foo": "ref"}`))
rm := &RouteMatch{
ResourcePath: []*ResourcePathComponent{
&ResourcePathComponent{
Name: "bar",
Resource: bar,
},
},
}
ctx := contextWithIndex(context.Background(), index)
status, _, body := listPost(ctx, r, rm)
assert.Equal(t, http.StatusCreated, status)
if assert.IsType(t, &resource.Item{}, body) {
item := body.(*resource.Item)
assert.Equal(t, "1", item.ID)
}
}
开发者ID:rs,项目名称:rest-layer,代码行数:26,代码来源:method_post_test.go
示例6: TestSessionIsActive
func TestSessionIsActive(t *testing.T) {
u := test.URL()
if u == nil {
t.SkipNow()
}
session := sessionClient(u, t)
// Skip test against ESXi -- SessionIsActive is not implemented
if session.client.ServiceContent.About.ApiType != "VirtualCenter" {
t.Skipf("Talking to %s instead of %s", session.client.ServiceContent.About.ApiType, "VirtualCenter")
}
err := session.Login(context.Background(), u.User)
if err != nil {
t.Error("Login Error: ", err)
}
active, err := session.SessionIsActive(context.Background())
if err != nil || !active {
t.Errorf("Expected %t, got %t", true, active)
t.Errorf("Expected nil, got %v", err)
}
session.Logout(context.Background())
active, err = session.SessionIsActive(context.Background())
if err == nil || active {
t.Errorf("Expected %t, got %t", false, active)
t.Errorf("Expected NotAuthenticated, got %v", err)
}
}
开发者ID:vmware,项目名称:vic,代码行数:32,代码来源:manager_test.go
示例7: TestEnumerateChildren
func TestEnumerateChildren(t *testing.T) {
bsi := bstest.Mocks(1)
ds := NewDAGService(bsi[0])
read := io.LimitReader(u.NewTimeSeededRand(), 1024*1024)
root, err := imp.BuildDagFromReader(ds, chunk.NewSizeSplitter(read, 512))
if err != nil {
t.Fatal(err)
}
set := cid.NewSet()
err = EnumerateChildren(context.Background(), ds, root.Cid(), set.Visit, false)
if err != nil {
t.Fatal(err)
}
var traverse func(n node.Node)
traverse = func(n node.Node) {
// traverse dag and check
for _, lnk := range n.Links() {
c := lnk.Cid
if !set.Has(c) {
t.Fatal("missing key in set! ", lnk.Cid.String())
}
child, err := ds.Get(context.Background(), c)
if err != nil {
t.Fatal(err)
}
traverse(child)
}
}
traverse(root)
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:34,代码来源:merkledag_test.go
示例8: TestAddLink
func TestAddLink(t *testing.T) {
ds := mdtest.Mock()
fishnode := dag.NodeWithData([]byte("fishcakes!"))
fk, err := ds.Add(fishnode)
if err != nil {
t.Fatal(err)
}
nd := new(dag.ProtoNode)
nnode, err := addLink(context.Background(), ds, nd, "fish", fishnode)
if err != nil {
t.Fatal(err)
}
fnprime, err := nnode.GetLinkedNode(context.Background(), ds, "fish")
if err != nil {
t.Fatal(err)
}
fnpkey := fnprime.Cid()
if !fnpkey.Equals(fk) {
t.Fatal("wrong child node found!")
}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:25,代码来源:utils_test.go
示例9: TestClientOverMax
func TestClientOverMax(t *testing.T) {
rs := NewServer()
k := cid.NewCidV0(u.Hash([]byte("hello")))
numProvidersForHelloKey := 100
for i := 0; i < numProvidersForHelloKey; i++ {
pi := testutil.RandIdentityOrFatal(t)
err := rs.Client(pi).Provide(context.Background(), k)
if err != nil {
t.Fatal(err)
}
}
max := 10
pi := testutil.RandIdentityOrFatal(t)
client := rs.Client(pi)
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
i := 0
for _ = range providersFromClient {
i++
}
if i != max {
t.Fatal("Too many providers returned")
}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:25,代码来源:centralized_test.go
示例10: TestClientFindProviders
func TestClientFindProviders(t *testing.T) {
pi := testutil.RandIdentityOrFatal(t)
rs := NewServer()
client := rs.Client(pi)
k := cid.NewCidV0(u.Hash([]byte("hello")))
err := client.Provide(context.Background(), k)
if err != nil {
t.Fatal(err)
}
// This is bad... but simulating networks is hard
time.Sleep(time.Millisecond * 300)
max := 100
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
isInClient := false
for pi := range providersFromClient {
if pi.ID == pi.ID {
isInClient = true
}
}
if !isInClient {
t.Fatal("Despite client providing key, client didn't receive peer when finding providers")
}
}
开发者ID:VictorBjelkholm,项目名称:go-ipfs,代码行数:26,代码来源:centralized_test.go
示例11: NewAuthenticatedClient
// NewAuthenticatedClient creates a new vim25.Client, authenticates the user
// specified in the test URL, and returns it.
func NewAuthenticatedClient(t *testing.T) *vim25.Client {
u := URL()
if u == nil {
t.SkipNow()
}
soapClient := soap.NewClient(u, true)
vimClient, err := vim25.NewClient(context.Background(), soapClient)
if err != nil {
t.Fatal(err)
}
req := types.Login{
This: *vimClient.ServiceContent.SessionManager,
}
req.UserName = u.User.Username()
if pw, ok := u.User.Password(); ok {
req.Password = pw
}
_, err = methods.Login(context.Background(), vimClient, &req)
if err != nil {
t.Fatal(err)
}
return vimClient
}
开发者ID:vmware,项目名称:vic,代码行数:30,代码来源:helper.go
示例12: scrapeRoute
// scrapeRoute parses and responds to a Scrape by using t.TrackerLogic.
func (t *Frontend) scrapeRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error
start := time.Now()
defer recordResponseDuration("scrape", err, time.Since(start))
req, err := ParseScrape(r)
if err != nil {
WriteError(w, err)
return
}
host, _, err := net.SplitHostPort(r.RemoteAddr)
if err != nil {
log.Errorln("http: unable to determine remote address for scrape:", err)
WriteError(w, err)
return
}
ip := net.ParseIP(host)
ctx := context.WithValue(context.Background(), middleware.ScrapeIsIPv6Key, len(ip) == net.IPv6len)
resp, err := t.logic.HandleScrape(ctx, req)
if err != nil {
WriteError(w, err)
return
}
err = WriteScrapeResponse(w, resp)
if err != nil {
WriteError(w, err)
return
}
go t.logic.AfterScrape(context.Background(), req, resp)
}
开发者ID:mrd0ll4r,项目名称:chihaya,代码行数:36,代码来源:frontend.go
示例13: announceRoute
// announceRoute parses and responds to an Announce by using t.TrackerLogic.
func (t *Frontend) announceRoute(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
var err error
start := time.Now()
defer recordResponseDuration("announce", err, time.Since(start))
req, err := ParseAnnounce(r, t.RealIPHeader, t.AllowIPSpoofing)
if err != nil {
WriteError(w, err)
return
}
resp, err := t.logic.HandleAnnounce(context.Background(), req)
if err != nil {
WriteError(w, err)
return
}
err = WriteAnnounceResponse(w, resp)
if err != nil {
WriteError(w, err)
return
}
go t.logic.AfterAnnounce(context.Background(), req, resp)
}
开发者ID:mrd0ll4r,项目名称:chihaya,代码行数:26,代码来源:frontend.go
示例14: TestClear
func TestClear(t *testing.T) {
if testing.Short() {
t.Skip("skipping test in short mode.")
}
s, err := mgo.Dial("")
if !assert.NoError(t, err) {
return
}
defer cleanup(s, "testupdate")()
h := NewHandler(s, "testupdate", "test")
items := []*resource.Item{
{ID: "1", Payload: map[string]interface{}{"id": "1", "name": "a"}},
{ID: "2", Payload: map[string]interface{}{"id": "2", "name": "b"}},
{ID: "3", Payload: map[string]interface{}{"id": "3", "name": "c"}},
{ID: "4", Payload: map[string]interface{}{"id": "4", "name": "d"}},
}
err = h.Insert(context.Background(), items)
assert.NoError(t, err)
lookup := resource.NewLookupWithQuery(schema.Query{
schema.In{Field: "name", Values: []schema.Value{"c", "d"}},
})
deleted, err := h.Clear(context.Background(), lookup)
assert.NoError(t, err)
assert.Equal(t, 2, deleted)
lookup = resource.NewLookupWithQuery(schema.Query{
schema.Equal{Field: "id", Value: "2"},
})
deleted, err = h.Clear(context.Background(), lookup)
assert.NoError(t, err)
assert.Equal(t, 1, deleted)
}
开发者ID:rs,项目名称:rest-layer-mongo,代码行数:34,代码来源:mongo_test.go
示例15: TestFromContext
func TestFromContext(t *testing.T) {
assert.Equal(t, NopLogger, FromContext(nil))
assert.Equal(t, NopLogger, FromContext(context.Background()))
l := &logger{}
ctx := NewContext(context.Background(), l)
assert.Equal(t, l, FromContext(ctx))
}
开发者ID:rs,项目名称:xlog,代码行数:7,代码来源:handler_test.go
示例16: TestHandlerPostListWithSubSchemaReferenceNotFound
func TestHandlerPostListWithSubSchemaReferenceNotFound(t *testing.T) {
s := mem.NewHandler()
s.Insert(context.Background(), []*resource.Item{{ID: "ref", Payload: map[string]interface{}{"id": "ref"}}})
index := resource.NewIndex()
index.Bind("foo", schema.Schema{Fields: schema.Fields{"id": {}}}, s, resource.DefaultConf)
bar := index.Bind("bar", schema.Schema{Fields: schema.Fields{
"id": {},
"sub": {
Schema: &schema.Schema{
Fields: schema.Fields{
"foo": {Validator: &schema.Reference{Path: "foo"}},
},
},
},
}}, s, resource.DefaultConf)
r, _ := http.NewRequest("POST", "/bar", bytes.NewBufferString(`{"id": "1", "sub": {"foo": "notfound"}}`))
rm := &RouteMatch{
ResourcePath: []*ResourcePathComponent{
&ResourcePathComponent{
Name: "bar",
Resource: bar,
},
},
}
ctx := contextWithIndex(context.Background(), index)
status, _, body := listPost(ctx, r, rm)
assert.Equal(t, http.StatusNotFound, status)
if assert.IsType(t, &Error{}, body) {
err := body.(*Error)
assert.Equal(t, http.StatusNotFound, err.Code)
assert.Equal(t, "Resource reference not found for field `foo'", err.Message)
}
}
开发者ID:rs,项目名称:rest-layer,代码行数:33,代码来源:method_post_test.go
示例17: TestVaultClient_SetActive
func TestVaultClient_SetActive(t *testing.T) {
v := testutil.NewTestVault(t).Start()
defer v.Stop()
logger := log.New(os.Stderr, "", log.LstdFlags)
client, err := NewVaultClient(v.Config, logger, nil)
if err != nil {
t.Fatalf("failed to build vault client: %v", err)
}
defer client.Stop()
waitForConnection(client, t)
// Do a lookup and expect an error about not being active
_, err = client.LookupToken(context.Background(), "123")
if err == nil || !strings.Contains(err.Error(), "not active") {
t.Fatalf("Expected not-active error: %v", err)
}
client.SetActive(true)
// Do a lookup of ourselves
_, err = client.LookupToken(context.Background(), v.RootToken)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
}
开发者ID:achanda,项目名称:nomad,代码行数:27,代码来源:vault_test.go
示例18: NewGCS
func NewGCS(name string, info map[string]string) (Backend, error) {
b := &gcsBackend{
name: name,
bucketName: info["bucket"],
}
keyJSON := []byte(info["key"])
if b.bucketName == "" {
return nil, fmt.Errorf("blobstore: missing Google Cloud Storage bucket param for %s", name)
}
if len(keyJSON) == 0 {
return nil, fmt.Errorf("blobstore: missing Google Cloud Storage key JSON param for %s", name)
}
jwtToken, err := google.JWTConfigFromJSON(keyJSON, "https://www.googleapis.com/auth/devstorage.read_write")
if err != nil {
return nil, fmt.Errorf("blobstore: error loading Google Cloud Storage JSON key: %s", err)
}
tokenSource := jwtToken.TokenSource(context.Background())
// Test getting an OAuth token so we can disambiguate an issue with the
// token and an issue with the bucket permissions below.
if _, err := tokenSource.Token(); err != nil {
return nil, fmt.Errorf("blobstore: error getting Google Cloud Storage OAuth token: %s", err)
}
pemBlock, _ := pem.Decode(jwtToken.PrivateKey)
privateKey, err := x509.ParsePKCS8PrivateKey(pemBlock.Bytes)
if err != nil {
return nil, fmt.Errorf("blobstore: error decoding Google Cloud Storage private key: %s", err)
}
rsaPrivateKey, ok := privateKey.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("blobstore: unexpected Google Cloud Storage key type: %T", privateKey)
}
b.signOpts = func() *storage.SignedURLOptions {
return &storage.SignedURLOptions{
GoogleAccessID: jwtToken.Email,
SignBytes: func(b []byte) ([]byte, error) {
digest := sha256.Sum256(b)
return rsa.SignPKCS1v15(rand.Reader, rsaPrivateKey, crypto.SHA256, digest[:])
},
Method: "GET",
Expires: time.Now().Add(10 * time.Minute),
}
}
client, err := storage.NewClient(context.Background(), option.WithTokenSource(tokenSource))
if err != nil {
return nil, fmt.Errorf("blobstore: error creating Google Cloud Storage client: %s", err)
}
b.bucket = client.Bucket(b.bucketName)
_, err = b.bucket.Attrs(context.Background())
if err != nil {
return nil, fmt.Errorf("blobstore: error checking Google Cloud Storage bucket %q existence, ensure that it exists and Owner access for %s is included the bucket ACL: %q", b.bucketName, jwtToken.Email, err)
}
return b, nil
}
开发者ID:ably-forks,项目名称:flynn,代码行数:59,代码来源:gcs.go
示例19: TestLimitedStreams
func TestLimitedStreams(t *testing.T) {
mn, err := FullMeshConnected(context.Background(), 2)
if err != nil {
t.Fatal(err)
}
var wg sync.WaitGroup
messages := 4
messageSize := 500
handler := func(s inet.Stream) {
b := make([]byte, messageSize)
for i := 0; i < messages; i++ {
if _, err := io.ReadFull(s, b); err != nil {
log.Fatal(err)
}
if !bytes.Equal(b[:4], []byte("ping")) {
log.Fatal("bytes mismatch")
}
wg.Done()
}
s.Close()
}
hosts := mn.Hosts()
for _, h := range mn.Hosts() {
h.SetStreamHandler(protocol.TestingID, handler)
}
peers := mn.Peers()
links := mn.LinksBetweenPeers(peers[0], peers[1])
// 1000 byte per second bandwidth
bps := float64(1000)
opts := links[0].Options()
opts.Bandwidth = bps
for _, link := range links {
link.SetOptions(opts)
}
ctx := context.Background()
s, err := hosts[0].NewStream(ctx, hosts[1].ID(), protocol.TestingID)
if err != nil {
t.Fatal(err)
}
filler := make([]byte, messageSize-4)
data := append([]byte("ping"), filler...)
before := time.Now()
for i := 0; i < messages; i++ {
wg.Add(1)
if _, err := s.Write(data); err != nil {
panic(err)
}
}
wg.Wait()
if !within(time.Since(before), time.Duration(time.Second*2), time.Second/3) {
t.Fatal("Expected 2ish seconds but got ", time.Since(before))
}
}
开发者ID:peckjerry,项目名称:go-libp2p,代码行数:59,代码来源:mock_test.go
示例20: TaskListener
// TaskListener monitors etcd for new tasks
func (m *etcdMinion) TaskListener(c chan<- *task.Task) error {
log.Printf("Task listener is watching %s\n", m.queueDir)
rand.Seed(time.Now().UTC().UnixNano())
b := backoff.Backoff{
Min: 1 * time.Second,
Max: 10 * time.Minute,
Factor: 2.0,
Jitter: true,
}
watcherOpts := &etcdclient.WatcherOptions{
Recursive: true,
}
watcher := m.kapi.Watcher(m.queueDir, watcherOpts)
for {
resp, err := watcher.Next(context.Background())
if err != nil {
// Use a backoff and retry later again
duration := b.Duration()
log.Printf("%s, retrying in %s\n", err, duration)
time.Sleep(duration)
continue
}
// Reset the backoff counter on successful receive
b.Reset()
// Ignore "delete" events when removing a task from the queue
action := strings.ToLower(resp.Action)
if strings.EqualFold(action, "delete") {
continue
}
// Unmarshal and remove task from the queue
t, err := EtcdUnmarshalTask(resp.Node)
m.kapi.Delete(context.Background(), resp.Node.Key, nil)
if err != nil {
log.Printf("Received invalid task %s: %s\n", resp.Node.Key, err)
continue
}
// Send the task for processing
log.Printf("Received task %s\n", t.ID)
t.State = task.TaskStateQueued
t.TimeReceived = time.Now().Unix()
if err := m.SaveTaskResult(t); err != nil {
log.Printf("Unable to save task state: %s\n", err)
continue
}
c <- t
}
return nil
}
开发者ID:dnaeon,项目名称:gru,代码行数:59,代码来源:etcd.go
注:本文中的context.Background函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论