github.com/cheng762/platon-go@v1.8.17-0.20190529111256-7deff2d7be26/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" 22 "net/http/httptest" 23 "os" 24 "testing" 25 26 "github.com/PlatONnetwork/PlatON-Go/swarm/api" 27 "github.com/PlatONnetwork/PlatON-Go/swarm/storage" 28 "github.com/PlatONnetwork/PlatON-Go/swarm/storage/mru" 29 ) 30 31 type TestServer interface { 32 ServeHTTP(http.ResponseWriter, *http.Request) 33 } 34 35 // simulated timeProvider 36 type fakeTimeProvider struct { 37 currentTime uint64 38 } 39 40 func (f *fakeTimeProvider) Tick() { 41 f.currentTime++ 42 } 43 44 func (f *fakeTimeProvider) Now() mru.Timestamp { 45 return mru.Timestamp{Time: f.currentTime} 46 } 47 48 func NewTestSwarmServer(t *testing.T, serverFunc func(*api.API) TestServer, resolver api.Resolver) *TestSwarmServer { 49 dir, err := ioutil.TempDir("", "swarm-storage-test") 50 if err != nil { 51 t.Fatal(err) 52 } 53 storeparams := storage.NewDefaultLocalStoreParams() 54 storeparams.DbCapacity = 5000000 55 storeparams.CacheCapacity = 5000 56 storeparams.Init(dir) 57 localStore, err := storage.NewLocalStore(storeparams, nil) 58 if err != nil { 59 os.RemoveAll(dir) 60 t.Fatal(err) 61 } 62 fileStore := storage.NewFileStore(localStore, storage.NewFileStoreParams()) 63 64 // mutable resources test setup 65 resourceDir, err := ioutil.TempDir("", "swarm-resource-test") 66 if err != nil { 67 t.Fatal(err) 68 } 69 70 fakeTimeProvider := &fakeTimeProvider{ 71 currentTime: 42, 72 } 73 mru.TimestampProvider = fakeTimeProvider 74 rhparams := &mru.HandlerParams{} 75 rh, err := mru.NewTestHandler(resourceDir, rhparams) 76 if err != nil { 77 t.Fatal(err) 78 } 79 80 a := api.NewAPI(fileStore, resolver, rh.Handler, nil) 81 srv := httptest.NewServer(serverFunc(a)) 82 return &TestSwarmServer{ 83 Server: srv, 84 FileStore: fileStore, 85 dir: dir, 86 Hasher: storage.MakeHashFunc(storage.DefaultHash)(), 87 timestampProvider: fakeTimeProvider, 88 cleanup: func() { 89 srv.Close() 90 rh.Close() 91 os.RemoveAll(dir) 92 os.RemoveAll(resourceDir) 93 }, 94 } 95 } 96 97 type TestSwarmServer struct { 98 *httptest.Server 99 Hasher storage.SwarmHash 100 FileStore *storage.FileStore 101 dir string 102 cleanup func() 103 timestampProvider *fakeTimeProvider 104 } 105 106 func (t *TestSwarmServer) Close() { 107 t.cleanup() 108 } 109 110 func (t *TestSwarmServer) GetCurrentTime() mru.Timestamp { 111 return t.timestampProvider.Now() 112 }