Skip to content
This repository was archived by the owner on Aug 2, 2021. It is now read-only.

Commit f5d08ca

Browse files
gbalintnonsense
authored andcommitted
swarm: Adding context to more functions
1 parent ef67a8f commit f5d08ca

23 files changed

+175
-82
lines changed

swarm/api/api.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ func (a *API) Retrieve(ctx context.Context, addr storage.Address) (reader storag
239239
}
240240

241241
// Store wraps the Store API call of the embedded FileStore
242-
func (a *API) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(), err error) {
242+
func (a *API) Store(ctx context.Context, data io.Reader, size int64, toEncrypt bool) (addr storage.Address, wait func(ctx context.Context) error, err error) {
243243
log.Debug("api.store", "size", size)
244244
return a.fileStore.Store(ctx, data, size, toEncrypt)
245245
}
@@ -286,7 +286,7 @@ func (a *API) Resolve(ctx context.Context, uri *URI) (storage.Address, error) {
286286
}
287287

288288
// Put provides singleton manifest creation on top of FileStore store
289-
func (a *API) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (k storage.Address, wait func(), err error) {
289+
func (a *API) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (k storage.Address, wait func(context.Context) error, err error) {
290290
apiPutCount.Inc(1)
291291
r := strings.NewReader(content)
292292
key, waitContent, err := a.fileStore.Store(ctx, r, int64(len(content)), toEncrypt)
@@ -301,9 +301,12 @@ func (a *API) Put(ctx context.Context, content string, contentType string, toEnc
301301
apiPutFail.Inc(1)
302302
return nil, nil, err
303303
}
304-
return key, func() {
305-
waitContent()
306-
waitManifest()
304+
return key, func(ctx context.Context) error {
305+
err := waitContent(ctx)
306+
if err != nil {
307+
return err
308+
}
309+
return waitManifest(ctx)
307310
}, nil
308311
}
309312

swarm/api/api_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,15 @@ func TestApiPut(t *testing.T) {
109109
testAPI(t, func(api *API, toEncrypt bool) {
110110
content := "hello"
111111
exp := expResponse(content, "text/plain", 0)
112-
addr, wait, err := api.Put(context.TODO(), content, exp.MimeType, toEncrypt)
112+
ctx := context.TODO()
113+
addr, wait, err := api.Put(ctx, content, exp.MimeType, toEncrypt)
114+
if err != nil {
115+
t.Fatalf("unexpected error: %v", err)
116+
}
117+
err = wait(ctx)
113118
if err != nil {
114119
t.Fatalf("unexpected error: %v", err)
115120
}
116-
wait()
117121
resp := testGet(t, api, addr.Hex(), "")
118122
checkResponse(t, resp, exp)
119123
})

swarm/api/filesystem.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,13 @@ func (fs *FileSystem) Upload(lpath, index string, toEncrypt bool) (string, error
114114
if err == nil {
115115
stat, _ := f.Stat()
116116
var hash storage.Address
117-
var wait func()
118-
hash, wait, err = fs.api.fileStore.Store(context.TODO(), f, stat.Size(), toEncrypt)
117+
var wait func(context.Context) error
118+
ctx := context.TODO()
119+
hash, wait, err = fs.api.fileStore.Store(ctx, f, stat.Size(), toEncrypt)
119120
if hash != nil {
120121
list[i].Hash = hash.Hex()
121122
}
122-
wait()
123+
err = wait(ctx)
123124
awg.Done()
124125
if err == nil {
125126
first512 := make([]byte, 512)

swarm/api/filesystem_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,13 @@ func TestApiDirUploadModify(t *testing.T) {
106106
t.Errorf("unexpected error: %v", err)
107107
return
108108
}
109-
hash, wait, err := api.Store(context.TODO(), bytes.NewReader(index), int64(len(index)), toEncrypt)
110-
wait()
109+
ctx := context.TODO()
110+
hash, wait, err := api.Store(ctx, bytes.NewReader(index), int64(len(index)), toEncrypt)
111+
if err != nil {
112+
t.Errorf("unexpected error: %v", err)
113+
return
114+
}
115+
err = wait(ctx)
111116
if err != nil {
112117
t.Errorf("unexpected error: %v", err)
113118
return

swarm/api/http/server_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -383,15 +383,19 @@ func testBzzGetPath(encrypted bool, t *testing.T) {
383383

384384
for i, mf := range testmanifest {
385385
reader[i] = bytes.NewReader([]byte(mf))
386-
var wait func()
387-
addr[i], wait, err = srv.FileStore.Store(context.TODO(), reader[i], int64(len(mf)), encrypted)
386+
var wait func(context.Context) error
387+
ctx := context.TODO()
388+
addr[i], wait, err = srv.FileStore.Store(ctx, reader[i], int64(len(mf)), encrypted)
388389
for j := i + 1; j < len(testmanifest); j++ {
389390
testmanifest[j] = strings.Replace(testmanifest[j], fmt.Sprintf("<key%v>", i), addr[i].Hex(), -1)
390391
}
391392
if err != nil {
392393
t.Fatal(err)
393394
}
394-
wait()
395+
err = wait(ctx)
396+
if err != nil {
397+
t.Fatal(err)
398+
}
395399
}
396400

397401
rootRef := addr[2].Hex()

swarm/api/manifest.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (a *API) NewManifest(ctx context.Context, toEncrypt bool) (storage.Address,
6969
return nil, err
7070
}
7171
key, wait, err := a.Store(ctx, bytes.NewReader(data), int64(len(data)), toEncrypt)
72-
wait()
72+
wait(ctx)
7373
return key, err
7474
}
7575

@@ -382,8 +382,12 @@ func (mt *manifestTrie) recalcAndStore() error {
382382
}
383383

384384
sr := bytes.NewReader(manifest)
385-
key, wait, err2 := mt.fileStore.Store(context.TODO(), sr, int64(len(manifest)), mt.encrypted)
386-
wait()
385+
ctx := context.TODO()
386+
key, wait, err2 := mt.fileStore.Store(ctx, sr, int64(len(manifest)), mt.encrypted)
387+
if err2 != nil {
388+
return err2
389+
}
390+
err2 = wait(ctx)
387391
mt.ref = key
388392
return err2
389393
}

swarm/api/storage.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func NewStorage(api *API) *Storage {
4646
// its content type
4747
//
4848
// DEPRECATED: Use the HTTP API instead
49-
func (s *Storage) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (storage.Address, func(), error) {
49+
func (s *Storage) Put(ctx context.Context, content string, contentType string, toEncrypt bool) (storage.Address, func(context.Context) error, error) {
5050
return s.api.Put(ctx, content, contentType, toEncrypt)
5151
}
5252

swarm/api/storage_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,15 @@ func TestStoragePutGet(t *testing.T) {
3232
content := "hello"
3333
exp := expResponse(content, "text/plain", 0)
3434
// exp := expResponse([]byte(content), "text/plain", 0)
35-
bzzkey, wait, err := api.Put(context.TODO(), content, exp.MimeType, toEncrypt)
35+
ctx := context.TODO()
36+
bzzkey, wait, err := api.Put(ctx, content, exp.MimeType, toEncrypt)
37+
if err != nil {
38+
t.Fatalf("unexpected error: %v", err)
39+
}
40+
err = wait(ctx)
3641
if err != nil {
3742
t.Fatalf("unexpected error: %v", err)
3843
}
39-
wait()
4044
bzzhash := bzzkey.Hex()
4145
// to check put against the API#Get
4246
resp0 := testGet(t, api.api, bzzhash, "")

swarm/network/stream/delivery_test.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,13 @@ func testDeliveryFromNodes(t *testing.T, nodes, conns, chunkCount int, skipCheck
345345
// here we distribute chunks of a random file into Stores of nodes 1 to nodes
346346
rrFileStore := storage.NewFileStore(newRoundRobinStore(sim.Stores[1:]...), storage.NewFileStoreParams())
347347
size := chunkCount * chunkSize
348-
fileHash, wait, err := rrFileStore.Store(context.TODO(), io.LimitReader(crand.Reader, int64(size)), int64(size), false)
348+
ctx := context.TODO()
349+
fileHash, wait, err := rrFileStore.Store(ctx, io.LimitReader(crand.Reader, int64(size)), int64(size), false)
349350
// wait until all chunks stored
350-
wait()
351+
if err != nil {
352+
t.Fatal(err.Error())
353+
}
354+
err = wait(ctx)
351355
if err != nil {
352356
t.Fatal(err.Error())
353357
}
@@ -627,9 +631,13 @@ Loop:
627631
hashes := make([]storage.Address, chunkCount)
628632
for i := 0; i < chunkCount; i++ {
629633
// create actual size real chunks
630-
hash, wait, err := remoteFileStore.Store(context.TODO(), io.LimitReader(crand.Reader, int64(chunkSize)), int64(chunkSize), false)
634+
ctx := context.TODO()
635+
hash, wait, err := remoteFileStore.Store(ctx, io.LimitReader(crand.Reader, int64(chunkSize)), int64(chunkSize), false)
636+
if err != nil {
637+
b.Fatalf("expected no error. got %v", err)
638+
}
631639
// wait until all chunks stored
632-
wait()
640+
err = wait(ctx)
633641
if err != nil {
634642
b.Fatalf("expected no error. got %v", err)
635643
}

swarm/network/stream/intervals_test.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,12 @@ func testIntervals(t *testing.T, live bool, history *Range, skipCheck bool) {
117117

118118
fileStore := storage.NewFileStore(sim.Stores[0], storage.NewFileStoreParams())
119119
size := chunkCount * chunkSize
120-
_, wait, err := fileStore.Store(context.TODO(), io.LimitReader(crand.Reader, int64(size)), int64(size), false)
121-
wait()
120+
ctx := context.TODO()
121+
_, wait, err := fileStore.Store(ctx, io.LimitReader(crand.Reader, int64(size)), int64(size), false)
122+
if err != nil {
123+
t.Fatal(err)
124+
}
125+
err = wait(ctx)
122126
if err != nil {
123127
t.Fatal(err)
124128
}

0 commit comments

Comments
 (0)