test_server.go 2.6 KB

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