Skip to content

Commit

Permalink
fix(getter): fix freeze and combine errors return
Browse files Browse the repository at this point in the history
  • Loading branch information
zelig committed Feb 28, 2024
1 parent 652977f commit 45f6c49
Show file tree
Hide file tree
Showing 5 changed files with 326 additions and 314 deletions.
4 changes: 2 additions & 2 deletions pkg/api/bzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ func (s *Service) serveReference(logger log.Logger, address swarm.Address, pathV
strategyTimeout := getter.DefaultStrategyTimeout.String()

ctx := r.Context()
ctx, err := getter.SetConfigInContext(ctx, headers.Strategy, headers.FallbackMode, headers.ChunkRetrievalTimeout, &strategyTimeout, logger)
ctx, err := getter.SetConfigInContext(ctx, headers.Strategy, headers.FallbackMode, headers.ChunkRetrievalTimeout, &strategyTimeout)
if err != nil {
logger.Error(err, err.Error())
jsonhttp.BadRequest(w, "could not parse headers")
Expand Down Expand Up @@ -521,7 +521,7 @@ func (s *Service) downloadHandler(logger log.Logger, w http.ResponseWriter, r *h
strategyTimeout := getter.DefaultStrategyTimeout.String()

ctx := r.Context()
ctx, err := getter.SetConfigInContext(ctx, headers.Strategy, headers.FallbackMode, headers.ChunkRetrievalTimeout, &strategyTimeout, logger)
ctx, err := getter.SetConfigInContext(ctx, headers.Strategy, headers.FallbackMode, headers.ChunkRetrievalTimeout, &strategyTimeout)
if err != nil {
logger.Error(err, err.Error())
jsonhttp.BadRequest(w, "could not parse headers")
Expand Down
28 changes: 8 additions & 20 deletions pkg/api/bzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,33 +151,20 @@ func TestBzzUploadDownloadWithRedundancy(t *testing.T) {
if rLevel == 0 {
t.Skip("NA")
}
req, err := http.NewRequestWithContext(context.Background(), "GET", fileDownloadResource(refResponse.Reference.String()), nil)
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
req, err := http.NewRequestWithContext(ctx, "GET", fileDownloadResource(refResponse.Reference.String()), nil)
if err != nil {
t.Fatal(err)
}
req.Header.Set(api.SwarmLookAheadBufferSizeHeader, "0")
req.Header.Set(api.SwarmRedundancyStrategyHeader, "0")
req.Header.Set(api.SwarmRedundancyFallbackModeHeader, "false")
req.Header.Set(api.SwarmChunkRetrievalTimeoutHeader, fetchTimeout.String())

resp, err := client.Do(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
t.Fatalf("expected status %d; got %d", http.StatusOK, resp.StatusCode)
}
_, err = dataReader.Seek(0, io.SeekStart)
if err != nil {
t.Fatal(err)
}
ok, err := dataReader.Equal(resp.Body)
if err != nil {
t.Fatal(err)
}
if ok {
t.Fatal("there should be missing data")
_, err = client.Do(req)
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("expected error %v; got %v", io.ErrUnexpectedEOF, err)
}
})

Expand All @@ -186,6 +173,7 @@ func TestBzzUploadDownloadWithRedundancy(t *testing.T) {
if err != nil {
t.Fatal(err)
}
req.Header.Set(api.SwarmLookAheadBufferSizeHeader, "0")
req.Header.Set(api.SwarmRedundancyStrategyHeader, "3")
req.Header.Set(api.SwarmRedundancyFallbackModeHeader, "true")
req.Header.Set(api.SwarmChunkRetrievalTimeoutHeader, fetchTimeout.String())
Expand Down
Loading

0 comments on commit 45f6c49

Please sign in to comment.