Fix 'ctx, _' to have explicit cancel
License: MIT Signed-off-by: rht <rhtbot@gmail.com>
rht committed
Aug 23, 2015 at 19:33 UTC
a7202fa94cbb57689940873c19aac9ad786d9f29
9 files changed
+37
-23
blockservice/test/blocks_test.go
+4
-2
@@ -42,7 +42,8 @@ func TestBlocks(t *testing.T) {
42
t.Error("returned key is not equal to block key", err)
43
}
44
45
- ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
45
+ ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
46
+ defer cancel()
47
b2, err := bs.GetBlock(ctx, b.Key())
48
if err != nil {
49
t.Error("failed to retrieve block from BlockService", err)
@@ -75,7 +76,8 @@ func TestGetBlocksSequential(t *testing.T) {
76
t.Log("one instance at a time, get blocks concurrently")
77
78
for i := 1; i < len(servs); i++ {
78
- ctx, _ := context.WithTimeout(context.TODO(), time.Second*50)
79
+ ctx, cancel := context.WithTimeout(context.TODO(), time.Second*50)
80
+ defer cancel()
81
out := servs[i].GetBlocks(ctx, keys)
82
gotten := make(map[key.Key]*blocks.Block)
83
for blk := range out {
core/bootstrap.go
+2
-1
@@ -110,7 +110,8 @@ func Bootstrap(n *IpfsNode, cfg BootstrapConfig) (io.Closer, error) {
110
111
func bootstrapRound(ctx context.Context, host host.Host, cfg BootstrapConfig) error {
112
113
- ctx, _ = context.WithTimeout(ctx, cfg.ConnectionTimeout)
113
+ ctx, cancel := context.WithTimeout(ctx, cfg.ConnectionTimeout)
114
+ defer cancel()
115
id := host.ID()
116
117
// get bootstrap peers from config. retrieving them here makes
core/core.go
+2
-1
@@ -320,7 +320,8 @@ func setupDiscoveryOption(d config.Discovery) DiscoveryOption {
320
321
func (n *IpfsNode) HandlePeerFound(p peer.PeerInfo) {
322
log.Warning("trying peer info: ", p)
323
- ctx, _ := context.WithTimeout(n.Context(), time.Second*10)
323
+ ctx, cancel := context.WithTimeout(n.Context(), time.Second*10)
324
+ defer cancel()
325
if err := n.PeerHost.Connect(ctx, p); err != nil {
326
log.Warning("Failed to connect to peer found by discovery: ", err)
327
}
diagnostics/diag.go
+2
-1
@@ -298,7 +298,8 @@ func (d *Diagnostics) HandleMessage(ctx context.Context, s inet.Stream) error {
298
if timeout < HopTimeoutDecrement {
299
return fmt.Errorf("timeout too short: %s", timeout)
300
}
301
- ctx, _ = context.WithTimeout(ctx, timeout)
301
+ ctx, cancel := context.WithTimeout(ctx, timeout)
302
+ defer cancel()
303
pmes.SetTimeoutDuration(timeout - HopTimeoutDecrement)
304
305
dpeers, err := d.getDiagnosticFromPeers(ctx, d.getPeers(), pmes)
exchange/bitswap/bitswap_test.go
+10
-5
@@ -50,7 +50,8 @@ func TestProviderForKeyButNetworkCannotFind(t *testing.T) { // TODO revisit this
50
solo := g.Next()
51
defer solo.Exchange.Close()
52
53
- ctx, _ := context.WithTimeout(context.Background(), time.Nanosecond)
53
+ ctx, cancel := context.WithTimeout(context.Background(), time.Nanosecond)
54
+ defer cancel()
55
_, err := solo.Exchange.GetBlock(ctx, block.Key())
56
57
if err != context.DeadlineExceeded {
@@ -76,7 +77,8 @@ func TestGetBlockFromPeerAfterPeerAnnounces(t *testing.T) {
77
wantsBlock := peers[1]
78
defer wantsBlock.Exchange.Close()
79
79
- ctx, _ := context.WithTimeout(context.Background(), time.Second)
80
+ ctx, cancel := context.WithTimeout(context.Background(), time.Second)
81
+ defer cancel()
82
received, err := wantsBlock.Exchange.GetBlock(ctx, block.Key())
83
if err != nil {
84
t.Log(err)
@@ -226,14 +228,16 @@ func TestSendToWantingPeer(t *testing.T) {
228
229
alpha := bg.Next()
230
// peerA requests and waits for block alpha
229
- ctx, _ := context.WithTimeout(context.TODO(), waitTime)
231
+ ctx, cancel := context.WithTimeout(context.TODO(), waitTime)
232
+ defer cancel()
233
alphaPromise, err := peerA.Exchange.GetBlocks(ctx, []key.Key{alpha.Key()})
234
if err != nil {
235
t.Fatal(err)
236
}
237
238
// peerB announces to the network that he has block alpha
236
- ctx, _ = context.WithTimeout(context.TODO(), timeout)
239
+ ctx, cancel = context.WithTimeout(context.TODO(), timeout)
240
+ defer cancel()
241
err = peerB.Exchange.HasBlock(ctx, alpha)
242
if err != nil {
243
t.Fatal(err)
@@ -266,7 +270,8 @@ func TestBasicBitswap(t *testing.T) {
270
t.Fatal(err)
271
}
272
269
- ctx, _ := context.WithTimeout(context.TODO(), time.Second*5)
273
+ ctx, cancel := context.WithTimeout(context.TODO(), time.Second*5)
274
+ defer cancel()
275
blk, err := instances[1].Exchange.GetBlock(ctx, blocks[0].Key())
276
if err != nil {
277
t.Fatal(err)
exchange/bitswap/notifications/notifications_test.go
+2
-1
@@ -112,7 +112,8 @@ func TestSubscribeIsANoopWhenCalledWithNoKeys(t *testing.T) {
112
func TestCarryOnWhenDeadlineExpires(t *testing.T) {
113
114
impossibleDeadline := time.Nanosecond
115
- fastExpiringCtx, _ := context.WithTimeout(context.Background(), impossibleDeadline)
115
+ fastExpiringCtx, cancel := context.WithTimeout(context.Background(), impossibleDeadline)
116
+ defer cancel()
117
118
n := New()
119
defer n.Shutdown()
namesys/publisher.go
+5
-4
@@ -60,7 +60,8 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa
60
61
log.Debugf("Storing pubkey at: %s", namekey)
62
// Store associated public key
63
- timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*10))
63
+ timectx, cancel := context.WithDeadline(ctx, time.Now().Add(time.Second*10))
64
+ defer cancel()
65
err = p.routing.PutValue(timectx, namekey, pkbytes)
66
if err != nil {
67
return err
@@ -70,9 +71,9 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value path.Pa
71
72
log.Debugf("Storing ipns entry at: %s", ipnskey)
73
// Store ipns entry at "/ipns/"+b58(h(pubkey))
73
- timectx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second*10))
74
- err = p.routing.PutValue(timectx, ipnskey, data)
75
- if err != nil {
74
+ timectx, cancel = context.WithDeadline(ctx, time.Now().Add(time.Second*10))
75
+ defer cancel()
76
+ if err := p.routing.PutValue(timectx, ipnskey, data); err != nil {
77
return err
78
}
79
pin/pin_test.go
+6
-6
@@ -210,21 +210,21 @@ func TestPinRecursiveFail(t *testing.T) {
210
}
211
212
// Note: this isnt a time based test, we expect the pin to fail
213
- mctx, _ := context.WithTimeout(ctx, time.Millisecond)
213
+ mctx, cancel := context.WithTimeout(ctx, time.Millisecond)
214
+ defer cancel()
215
err = p.Pin(mctx, a, true)
216
if err == nil {
217
t.Fatal("should have failed to pin here")
218
}
219
219
- _, err = dserv.Add(b)
220
- if err != nil {
220
+ if _, err := dserv.Add(b); err != nil {
221
t.Fatal(err)
222
}
223
224
// this one is time based... but shouldnt cause any issues
225
- mctx, _ = context.WithTimeout(ctx, time.Second)
226
- err = p.Pin(mctx, a, true)
227
- if err != nil {
225
+ mctx, cancel = context.WithTimeout(ctx, time.Second)
226
+ defer cancel()
227
+ if err := p.Pin(mctx, a, true); err != nil {
228
t.Fatal(err)
229
}
230
}
routing/dht/ext_test.go
+4
-2
@@ -202,7 +202,8 @@ func TestNotFound(t *testing.T) {
202
}
203
204
// long timeout to ensure timing is not at play.
205
- ctx, _ = context.WithTimeout(ctx, time.Second*20)
205
+ ctx, cancel := context.WithTimeout(ctx, time.Second*20)
206
+ defer cancel()
207
v, err := d.GetValue(ctx, key.Key("hello"))
208
log.Debugf("get value got %v", v)
209
if err != nil {
@@ -274,7 +275,8 @@ func TestLessThanKResponses(t *testing.T) {
275
})
276
}
277
277
- ctx, _ = context.WithTimeout(ctx, time.Second*30)
278
+ ctx, cancel := context.WithTimeout(ctx, time.Second*30)
279
+ defer cancel()
280
if _, err := d.GetValue(ctx, key.Key("hello")); err != nil {
281
switch err {
282
case routing.ErrNotFound: