swarm/storage: Correct channel for waiting on chunk put

This commit is contained in:
lash 2018-01-23 02:04:57 +01:00
parent 0455925ebf
commit a32681cba4
3 changed files with 26 additions and 12 deletions

View file

@ -23,17 +23,23 @@ import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"os"
"strings" "strings"
"sync" "sync"
"testing" "testing"
"github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/swarm/api" "github.com/ethereum/go-ethereum/swarm/api"
swarm "github.com/ethereum/go-ethereum/swarm/api/client" swarm "github.com/ethereum/go-ethereum/swarm/api/client"
"github.com/ethereum/go-ethereum/swarm/storage" "github.com/ethereum/go-ethereum/swarm/storage"
"github.com/ethereum/go-ethereum/swarm/testutil" "github.com/ethereum/go-ethereum/swarm/testutil"
) )
func init() {
log.Root().SetHandler(log.CallerFileHandler(log.LvlFilterHandler(log.LvlTrace, log.StreamHandler(os.Stderr, log.TerminalFormat(true)))))
}
// \TODO if create -> get -> update -> get, the last get with return 1.1 because 1.2 retrieve is still pending // \TODO if create -> get -> update -> get, the last get with return 1.1 because 1.2 retrieve is still pending
func TestBzzResource(t *testing.T) { func TestBzzResource(t *testing.T) {
srv := testutil.NewTestSwarmServer(t) srv := testutil.NewTestSwarmServer(t)
@ -64,6 +70,22 @@ func TestBzzResource(t *testing.T) {
} }
resp.Body.Close() resp.Body.Close()
// get latest update (1.1) through resource directly
url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes)
resp, err = http.Get(url)
if err != nil {
t.Fatal(err)
} else if resp.StatusCode != http.StatusOK {
t.Fatalf("err %s", resp.Status)
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
} else if !bytes.Equal(databytes, b) {
t.Fatalf("Expected body '%x', got '%x'", databytes, b)
}
resp.Body.Close()
// update 2 // update 2
url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes) url = fmt.Sprintf("%s/bzz-resource:/%x", srv.URL, keybytes)
data := []byte("foo") data := []byte("foo")
@ -82,7 +104,7 @@ func TestBzzResource(t *testing.T) {
} else if resp.StatusCode != http.StatusOK { } else if resp.StatusCode != http.StatusOK {
t.Fatalf("err %s", resp.Status) t.Fatalf("err %s", resp.Status)
} }
b, err := ioutil.ReadAll(resp.Body) b, err = ioutil.ReadAll(resp.Body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} else if !bytes.Equal(data, b) { } else if !bytes.Equal(data, b) {

View file

@ -752,14 +752,16 @@ func (r *resourceChunkStore) Get(key Key) (*Chunk, error) {
select { select {
case <-t.C: case <-t.C:
return nil, errors.New("timeout") return nil, errors.New("timeout")
case <-chunk.C: case <-chunk.Req.C:
log.Trace("Received resource update chunk", "peer", chunk.Req.Source) log.Trace("Received resource update chunk", "peer", chunk.Req.Source)
} }
return chunk, nil return chunk, nil
} }
func (r *resourceChunkStore) Put(chunk *Chunk) { func (r *resourceChunkStore) Put(chunk *Chunk) {
chunk.wg = &sync.WaitGroup{}
r.netStore.Put(chunk) r.netStore.Put(chunk)
chunk.wg.Wait()
} }
func (r *resourceChunkStore) Close() { func (r *resourceChunkStore) Close() {

View file

@ -22,7 +22,6 @@ import (
"math/big" "math/big"
"net/http/httptest" "net/http/httptest"
"os" "os"
"strconv"
"testing" "testing"
"github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/core/types"
@ -117,12 +116,3 @@ func (c *testCloudStore) Deliver(*storage.Chunk) {
func (c *testCloudStore) Retrieve(*storage.Chunk) { func (c *testCloudStore) Retrieve(*storage.Chunk) {
} }
// for faking the rpc service, since we don't need the whole node stack
type FakeRPC struct {
blocknumber uint64
}
func (r *FakeRPC) BlockNumber() (string, error) {
return strconv.FormatUint(r.blocknumber, 10), nil
}