github.com/nitinawathare/ethereumassignment3@v0.0.0-20211021213010-f07344c2b868/go-ethereum/swarm/api/http/test_server.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 http
    18  
    19  import (
    20  	"io/ioutil"
    21  	"net/http"
    22  	"net/http/httptest"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/ethereum/go-ethereum/swarm/api"
    27  	"github.com/ethereum/go-ethereum/swarm/storage"
    28  	"github.com/ethereum/go-ethereum/swarm/storage/feed"
    29  )
    30  
    31  type TestServer interface {
    32  	ServeHTTP(http.ResponseWriter, *http.Request)
    33  }
    34  
    35  func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer, resolver api.Resolver) *TestSwarmServer {
    36  	swarmDir, err := ioutil.TempDir("", "swarm-storage-test")
    37  	if err != nil {
    38  		t.Fatal(err)
    39  	}
    40  
    41  	storeParams := storage.NewDefaultLocalStoreParams()
    42  	storeParams.DbCapacity = 5000000
    43  	storeParams.CacheCapacity = 5000
    44  	storeParams.Init(swarmDir)
    45  	localStore, err := storage.NewLocalStore(storeParams, nil)
    46  	if err != nil {
    47  		os.RemoveAll(swarmDir)
    48  		t.Fatal(err)
    49  	}
    50  	fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams())
    51  	// Swarm feeds test setup
    52  	feedsDir, err := ioutil.TempDir("", "swarm-feeds-test")
    53  	if err != nil {
    54  		t.Fatal(err)
    55  	}
    56  
    57  	feeds, err := feed.NewTestHandler(feedsDir, &feed.HandlerParams{})
    58  	if err != nil {
    59  		t.Fatal(err)
    60  	}
    61  
    62  	swarmApi := api.NewAPI(fileStore, resolver, feeds.Handler, nil)
    63  	apiServer := httptest.NewServer(serverFunc(swarmApi))
    64  
    65  	tss := &TestSwarmServer{
    66  		Server:    apiServer,
    67  		FileStore: fileStore,
    68  		dir:       swarmDir,
    69  		Hasher:    storage.MakeHashFunc(storage.DefaultHash)(),
    70  		cleanup: func() {
    71  			apiServer.Close()
    72  			fileStore.Close()
    73  			feeds.Close()
    74  			os.RemoveAll(swarmDir)
    75  			os.RemoveAll(feedsDir)
    76  		},
    77  		CurrentTime: 42,
    78  	}
    79  	feed.TimestampProvider = tss
    80  	return tss
    81  }
    82  
    83  type TestSwarmServer struct {
    84  	*httptest.Server
    85  	Hasher      storage.SwarmHash
    86  	FileStore   *storage.FileStore
    87  	dir         string
    88  	cleanup     func()
    89  	CurrentTime uint64
    90  }
    91  
    92  func (t *TestSwarmServer) Close() {
    93  	t.cleanup()
    94  }
    95  
    96  func (t *TestSwarmServer) Now() feed.Timestamp {
    97  	return feed.Timestamp{Time: t.CurrentTime}
    98  }