本文整理汇总了Golang中github.com/maybebtc/interplanetary/Godeps/_workspace/src/code/google/com/p/go/net/context.Background函数的典型用法代码示例。如果您正苦于以下问题:Golang Background函数的具体用法?Golang Background怎么用?Golang Background使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Background函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestGetBlockFromPeerAfterPeerAnnounces
func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
net := tn.VirtualNetwork()
rs := mock.VirtualRoutingServer()
block := blocks.NewBlock([]byte("block"))
g := NewSessionGenerator(net, rs)
hasBlock := g.Next()
if err := hasBlock.blockstore.Put(block); err != nil {
t.Fatal(err)
}
if err := hasBlock.exchange.HasBlock(context.Background(), *block); err != nil {
t.Fatal(err)
}
wantsBlock := g.Next()
ctx, _ := context.WithTimeout(context.Background(), time.Second)
received, err := wantsBlock.exchange.Block(ctx, block.Key())
if err != nil {
t.Log(err)
t.Fatal("Expected to succeed")
}
if !bytes.Equal(block.Data, received.Data) {
t.Fatal("Data doesn't match")
}
}
示例2: TestClientFindProviders
func TestClientFindProviders(t *testing.T) {
peer := peer.WithIDString("42")
rs := VirtualRoutingServer()
client := rs.Client(peer)
k := u.Key("hello")
err := client.Provide(context.Background(), k)
if err != nil {
t.Fatal(err)
}
max := 100
providersFromHashTable := rs.Providers(k)
isInHT := false
for _, p := range providersFromHashTable {
if bytes.Equal(p.ID(), peer.ID()) {
isInHT = true
}
}
if !isInHT {
t.Fatal("Despite client providing key, peer wasn't in hash table as a provider")
}
providersFromClient := client.FindProvidersAsync(context.Background(), u.Key("hello"), max)
isInClient := false
for p := range providersFromClient {
if bytes.Equal(p.ID(), peer.ID()) {
isInClient = true
}
}
if !isInClient {
t.Fatal("Despite client providing key, client didn't receive peer when finding providers")
}
}
示例3: TestServiceHandler
func TestServiceHandler(t *testing.T) {
ctx := context.Background()
h := &ReverseHandler{}
s := NewService(ctx, h)
peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
d, err := wrapData([]byte("beep"), nil)
if err != nil {
t.Error(err)
}
m1 := msg.New(peer1, d)
s.GetPipe().Incoming <- m1
m2 := <-s.GetPipe().Outgoing
d, rid, err := unwrapData(m2.Data())
if err != nil {
t.Error(err)
}
if rid != nil {
t.Error("RequestID should be nil")
}
if !bytes.Equal(d, []byte("peeb")) {
t.Errorf("service handler data incorrect: %v != %v", d, "oof")
}
}
示例4: TestClientOverMax
func TestClientOverMax(t *testing.T) {
rs := VirtualRoutingServer()
k := u.Key("hello")
numProvidersForHelloKey := 100
for i := 0; i < numProvidersForHelloKey; i++ {
peer := peer.WithIDString(string(i))
err := rs.Announce(peer, k)
if err != nil {
t.Fatal(err)
}
}
providersFromHashTable := rs.Providers(k)
if len(providersFromHashTable) != numProvidersForHelloKey {
t.Log(1 == len(providersFromHashTable))
t.Fatal("not all providers were returned")
}
max := 10
peer := peer.WithIDString("TODO")
client := rs.Client(peer)
providersFromClient := client.FindProvidersAsync(context.Background(), k, max)
i := 0
for _ = range providersFromClient {
i++
}
if i != max {
t.Fatal("Too many providers returned")
}
}
示例5: TestMulticonnClose
func TestMulticonnClose(t *testing.T) {
// t.Skip("fooo")
log.Info("TestMulticonnSendUnderlying")
ctx := context.Background()
c1, c2 := setupMultiConns(t, ctx)
for _, c := range c1.conns {
c.Close()
}
for _, c := range c2.conns {
c.Close()
}
timeout := time.After(100 * time.Millisecond)
select {
case <-c1.Closed():
case <-timeout:
t.Fatal("timeout")
}
select {
case <-c2.Closed():
case <-timeout:
t.Fatal("timeout")
}
}
示例6: TestServiceRequest
func TestServiceRequest(t *testing.T) {
ctx := context.Background()
s1 := NewService(ctx, &ReverseHandler{})
s2 := NewService(ctx, &ReverseHandler{})
peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
// patch services together
go func() {
for {
select {
case m := <-s1.GetPipe().Outgoing:
s2.GetPipe().Incoming <- m
case m := <-s2.GetPipe().Outgoing:
s1.GetPipe().Incoming <- m
case <-ctx.Done():
return
}
}
}()
m1 := msg.New(peer1, []byte("beep"))
m2, err := s1.SendRequest(ctx, m1)
if err != nil {
t.Error(err)
}
if !bytes.Equal(m2.Data(), []byte("peeb")) {
t.Errorf("service handler data incorrect: %v != %v", m2.Data(), "oof")
}
}
示例7: TestServiceRequestTimeout
func TestServiceRequestTimeout(t *testing.T) {
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond)
s1 := NewService(ctx, &ReverseHandler{})
s2 := NewService(ctx, &ReverseHandler{})
peer1 := newPeer(t, "11140beec7b5ea3f0fdbc95d0dd47f3c5bc275aaaaaa")
// patch services together
go func() {
for {
<-time.After(time.Millisecond)
select {
case m := <-s1.GetPipe().Outgoing:
s2.GetPipe().Incoming <- m
case m := <-s2.GetPipe().Outgoing:
s1.GetPipe().Incoming <- m
case <-ctx.Done():
return
}
}
}()
m1 := msg.New(peer1, []byte("beep"))
m2, err := s1.SendRequest(ctx, m1)
if err == nil || m2 != nil {
t.Error("should've timed out")
}
}
示例8: TestCancel
func TestCancel(t *testing.T) {
// t.Skip("Skipping in favor of another test")
ctx, cancel := context.WithCancel(context.Background())
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/5534", "/ip4/127.0.0.1/tcp/5545")
select {
case <-c1.Closed():
t.Fatal("done before close")
case <-c2.Closed():
t.Fatal("done before close")
default:
}
c1.Close()
c2.Close()
cancel() // listener
// wait to ensure other goroutines run and close things.
<-time.After(time.Microsecond * 10)
// test that cancel called Close.
select {
case <-c1.Closed():
default:
t.Fatal("not done after cancel")
}
select {
case <-c2.Closed():
default:
t.Fatal("not done after cancel")
}
}
示例9: TestClose
func TestClose(t *testing.T) {
// t.Skip("Skipping in favor of another test")
ctx, cancel := context.WithCancel(context.Background())
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/5534", "/ip4/127.0.0.1/tcp/5545")
select {
case <-c1.Closed():
t.Fatal("done before close")
case <-c2.Closed():
t.Fatal("done before close")
default:
}
c1.Close()
select {
case <-c1.Closed():
default:
t.Fatal("not done after cancel")
}
c2.Close()
select {
case <-c2.Closed():
default:
t.Fatal("not done after cancel")
}
cancel() // close the listener :P
}
示例10: TestProvides
func TestProvides(t *testing.T) {
// t.Skip("skipping test to debug another")
ctx := context.Background()
u.Debug = false
_, peers, dhts := setupDHTS(ctx, 4, t)
defer func() {
for i := 0; i < 4; i++ {
dhts[i].Close()
defer dhts[i].dialer.(inet.Network).Close()
}
}()
_, err := dhts[0].Connect(ctx, peers[1])
if err != nil {
t.Fatal(err)
}
_, err = dhts[1].Connect(ctx, peers[2])
if err != nil {
t.Fatal(err)
}
_, err = dhts[1].Connect(ctx, peers[3])
if err != nil {
t.Fatal(err)
}
err = dhts[3].putLocal(u.Key("hello"), []byte("world"))
if err != nil {
t.Fatal(err)
}
bits, err := dhts[3].getLocal(u.Key("hello"))
if err != nil && bytes.Equal(bits, []byte("world")) {
t.Fatal(err)
}
err = dhts[3].Provide(ctx, u.Key("hello"))
if err != nil {
t.Fatal(err)
}
time.Sleep(time.Millisecond * 60)
ctxT, _ := context.WithTimeout(ctx, time.Second)
provchan := dhts[0].FindProvidersAsync(ctxT, u.Key("hello"), 1)
after := time.After(time.Second)
select {
case prov := <-provchan:
if prov == nil {
t.Fatal("Got back nil provider")
}
case <-after:
t.Fatal("Did not get a provider back.")
}
}
示例11: TestValueGetSet
func TestValueGetSet(t *testing.T) {
// t.Skip("skipping test to debug another")
ctx := context.Background()
u.Debug = false
addrA, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/11235")
if err != nil {
t.Fatal(err)
}
addrB, err := ma.NewMultiaddr("/ip4/127.0.0.1/tcp/15679")
if err != nil {
t.Fatal(err)
}
peerA := makePeer(addrA)
peerB := makePeer(addrB)
dhtA := setupDHT(ctx, t, peerA)
dhtB := setupDHT(ctx, t, peerB)
vf := func(u.Key, []byte) error {
return nil
}
dhtA.Validators["v"] = vf
dhtB.Validators["v"] = vf
defer dhtA.Close()
defer dhtB.Close()
defer dhtA.dialer.(inet.Network).Close()
defer dhtB.dialer.(inet.Network).Close()
_, err = dhtA.Connect(ctx, peerB)
if err != nil {
t.Fatal(err)
}
ctxT, _ := context.WithTimeout(ctx, time.Second)
dhtA.PutValue(ctxT, "/v/hello", []byte("world"))
ctxT, _ = context.WithTimeout(ctx, time.Second*2)
val, err := dhtA.GetValue(ctxT, "/v/hello")
if err != nil {
t.Fatal(err)
}
if string(val) != "world" {
t.Fatalf("Expected 'world' got '%s'", string(val))
}
ctxT, _ = context.WithTimeout(ctx, time.Second*2)
val, err = dhtB.GetValue(ctxT, "/v/hello")
if err != nil {
t.Fatal(err)
}
if string(val) != "world" {
t.Fatalf("Expected 'world' got '%s'", string(val))
}
}
示例12: TestCloseLeak
func TestCloseLeak(t *testing.T) {
if testing.Short() {
t.SkipNow()
}
if os.Getenv("TRAVIS") == "true" {
t.Skip("this doesn't work well on travis")
}
var wg sync.WaitGroup
runPair := func(p1, p2, num int) {
a1 := strconv.Itoa(p1)
a2 := strconv.Itoa(p2)
ctx, cancel := context.WithCancel(context.Background())
c1, c2 := setupConn(t, ctx, "/ip4/127.0.0.1/tcp/"+a1, "/ip4/127.0.0.1/tcp/"+a2)
for i := 0; i < num; i++ {
b1 := []byte("beep")
c1.Out() <- b1
b2 := <-c2.In()
if !bytes.Equal(b1, b2) {
panic("bytes not equal")
}
b2 = []byte("boop")
c2.Out() <- b2
b1 = <-c1.In()
if !bytes.Equal(b1, b2) {
panic("bytes not equal")
}
<-time.After(time.Microsecond * 5)
}
c1.Close()
c2.Close()
cancel() // close the listener
wg.Done()
}
var cons = 20
var msgs = 100
fmt.Printf("Running %d connections * %d msgs.\n", cons, msgs)
for i := 0; i < cons; i++ {
wg.Add(1)
go runPair(2000+i, 2001+i, msgs)
}
fmt.Printf("Waiting...\n")
wg.Wait()
// done!
<-time.After(time.Millisecond * 150)
if runtime.NumGoroutine() > 20 {
// panic("uncomment me to debug")
t.Fatal("leaking goroutines:", runtime.NumGoroutine())
}
}
示例13: TestBlockReturnsErr
func TestBlockReturnsErr(t *testing.T) {
off := NewOfflineExchange()
_, err := off.Block(context.Background(), u.Key("foo"))
if err != nil {
return // as desired
}
t.Fail()
}
示例14: TestHasBlockReturnsNil
func TestHasBlockReturnsNil(t *testing.T) {
off := NewOfflineExchange()
block := blocks.NewBlock([]byte("data"))
err := off.HasBlock(context.Background(), *block)
if err != nil {
t.Fatal("")
}
}
示例15: getOrFail
func getOrFail(bitswap instance, b *blocks.Block, t *testing.T, wg *sync.WaitGroup) {
if _, err := bitswap.blockstore.Get(b.Key()); err != nil {
_, err := bitswap.exchange.Block(context.Background(), b.Key())
if err != nil {
t.Fatal(err)
}
}
wg.Done()
}