diff options
Diffstat (limited to 'swarm/testutil/http.go')
-rw-r--r-- | swarm/testutil/http.go | 86 |
1 files changed, 61 insertions, 25 deletions
diff --git a/swarm/testutil/http.go b/swarm/testutil/http.go index f2922fab0..debf0b14b 100644 --- a/swarm/testutil/http.go +++ b/swarm/testutil/http.go @@ -17,56 +17,92 @@ package testutil import ( + "context" "io/ioutil" + "math/big" + "net/http" "net/http/httptest" "os" "testing" + "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/swarm/api" - httpapi "github.com/ethereum/go-ethereum/swarm/api/http" "github.com/ethereum/go-ethereum/swarm/storage" + "github.com/ethereum/go-ethereum/swarm/storage/mru" ) -func NewTestSwarmServer(t *testing.T) *TestSwarmServer { +type TestServer interface { + ServeHTTP(http.ResponseWriter, *http.Request) +} + +type fakeBackend struct { + blocknumber int64 +} + +func (f *fakeBackend) HeaderByNumber(context context.Context, _ string, bigblock *big.Int) (*types.Header, error) { + f.blocknumber++ + biggie := big.NewInt(f.blocknumber) + return &types.Header{ + Number: biggie, + }, nil +} + +func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer) *TestSwarmServer { dir, err := ioutil.TempDir("", "swarm-storage-test") if err != nil { t.Fatal(err) } - storeparams := &storage.StoreParams{ - ChunkDbPath: dir, - DbCapacity: 5000000, - CacheCapacity: 5000, - Radius: 0, - } - localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams) + storeparams := storage.NewDefaultLocalStoreParams() + storeparams.DbCapacity = 5000000 + storeparams.CacheCapacity = 5000 + storeparams.Init(dir) + localStore, err := storage.NewLocalStore(storeparams, nil) if err != nil { os.RemoveAll(dir) t.Fatal(err) } - chunker := storage.NewTreeChunker(storage.NewChunkerParams()) - dpa := &storage.DPA{ - Chunker: chunker, - ChunkStore: localStore, + fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams()) + + // mutable resources test setup + resourceDir, err := ioutil.TempDir("", "swarm-resource-test") + if err != nil { + t.Fatal(err) + } + rhparams := &mru.HandlerParams{ + QueryMaxPeriods: &mru.LookupParams{}, + HeaderGetter: &fakeBackend{ + blocknumber: 42, + }, + } + rh, err := mru.NewTestHandler(resourceDir, rhparams) + if err != nil { + t.Fatal(err) } - dpa.Start() - a := api.NewApi(dpa, nil) - srv := httptest.NewServer(httpapi.NewServer(a)) + + a := api.NewAPI(fileStore, nil, rh) + srv := httptest.NewServer(serverFunc(a)) return &TestSwarmServer{ - Server: srv, - Dpa: dpa, - dir: dir, + Server: srv, + FileStore: fileStore, + dir: dir, + Hasher: storage.MakeHashFunc(storage.DefaultHash)(), + cleanup: func() { + srv.Close() + rh.Close() + os.RemoveAll(dir) + os.RemoveAll(resourceDir) + }, } } type TestSwarmServer struct { *httptest.Server - - Dpa *storage.DPA - dir string + Hasher storage.SwarmHash + FileStore *storage.FileStore + dir string + cleanup func() } func (t *TestSwarmServer) Close() { - t.Server.Close() - t.Dpa.Stop() - os.RemoveAll(t.dir) + t.cleanup() } |