server_utils.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package tests
  2. import (
  3. "io/ioutil"
  4. "os"
  5. "time"
  6. "github.com/coreos/etcd/server"
  7. "github.com/coreos/etcd/store"
  8. )
  9. const (
  10. testName = "ETCDTEST"
  11. testClientURL = "localhost:4401"
  12. testRaftURL = "localhost:7701"
  13. testSnapshotCount = 10000
  14. )
  15. // Starts a server in a temporary directory.
  16. func RunServer(f func(*server.Server)) {
  17. path, _ := ioutil.TempDir("", "etcd-")
  18. defer os.RemoveAll(path)
  19. store := store.New()
  20. registry := server.NewRegistry(store)
  21. ps := server.NewPeerServer(testName, path, "http://" + testRaftURL, testRaftURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, testSnapshotCount)
  22. ps.MaxClusterSize = 9
  23. s := server.New(testName, "http://" + testClientURL, testClientURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, ps, registry, store)
  24. ps.SetServer(s)
  25. // Start up peer server.
  26. c := make(chan bool)
  27. go func() {
  28. c <- true
  29. ps.ListenAndServe(false, []string{})
  30. }()
  31. <-c
  32. // Start up etcd server.
  33. go func() {
  34. c <- true
  35. s.ListenAndServe()
  36. }()
  37. <-c
  38. // Wait to make sure servers have started.
  39. time.Sleep(50 * time.Millisecond)
  40. // Execute the function passed in.
  41. f(s)
  42. // Clean up servers.
  43. ps.Close()
  44. s.Close()
  45. }