github.com/n1ghtfa1l/go-vnt@v0.6.4-alpha.6/swarm/testutil/http.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package testutil
    18  
    19  import (
    20  	"io/ioutil"
    21  	"net/http/httptest"
    22  	"os"
    23  	"testing"
    24  
    25  	"github.com/vntchain/go-vnt/swarm/api"
    26  	httpapi "github.com/vntchain/go-vnt/swarm/api/http"
    27  	"github.com/vntchain/go-vnt/swarm/storage"
    28  )
    29  
    30  func NewTestSwarmServer(t *testing.T) *TestSwarmServer {
    31  	dir, err := ioutil.TempDir("", "swarm-storage-test")
    32  	if err != nil {
    33  		t.Fatal(err)
    34  	}
    35  	storeparams := &storage.StoreParams{
    36  		ChunkDbPath:   dir,
    37  		DbCapacity:    5000000,
    38  		CacheCapacity: 5000,
    39  		Radius:        0,
    40  	}
    41  	localStore, err := storage.NewLocalStore(storage.MakeHashFunc("SHA3"), storeparams)
    42  	if err != nil {
    43  		os.RemoveAll(dir)
    44  		t.Fatal(err)
    45  	}
    46  	chunker := storage.NewTreeChunker(storage.NewChunkerParams())
    47  	dpa := &storage.DPA{
    48  		Chunker:    chunker,
    49  		ChunkStore: localStore,
    50  	}
    51  	dpa.Start()
    52  	a := api.NewApi(dpa, nil)
    53  	srv := httptest.NewServer(httpapi.NewServer(a))
    54  	return &TestSwarmServer{
    55  		Server: srv,
    56  		Dpa:    dpa,
    57  		dir:    dir,
    58  	}
    59  }
    60  
    61  type TestSwarmServer struct {
    62  	*httptest.Server
    63  
    64  	Dpa *storage.DPA
    65  	dir string
    66  }
    67  
    68  func (t *TestSwarmServer) Close() {
    69  	t.Server.Close()
    70  	t.Dpa.Stop()
    71  	os.RemoveAll(t.dir)
    72  }