server_utils.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. testHeartbeatTimeout = time.Duration(50) * time.Millisecond
  15. testElectionTimeout = time.Duration(200) * time.Millisecond
  16. )
  17. // Starts a server in a temporary directory.
  18. func RunServer(f func(*server.Server)) {
  19. path, _ := ioutil.TempDir("", "etcd-")
  20. defer os.RemoveAll(path)
  21. store := store.New()
  22. registry := server.NewRegistry(store)
  23. psConfig := server.PeerServerConfig{
  24. Name: testName,
  25. Path: path,
  26. URL: "http://"+testRaftURL,
  27. BindAddr: testRaftURL,
  28. SnapshotCount: testSnapshotCount,
  29. HeartbeatTimeout: testHeartbeatTimeout,
  30. ElectionTimeout: testElectionTimeout,
  31. MaxClusterSize: 9,
  32. }
  33. ps := server.NewPeerServer(psConfig, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, nil)
  34. s := server.New(testName, "http://"+testClientURL, testClientURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, ps, registry, store, nil)
  35. ps.SetServer(s)
  36. // Start up peer server.
  37. c := make(chan bool)
  38. go func() {
  39. c <- true
  40. ps.ListenAndServe(false, []string{})
  41. }()
  42. <-c
  43. // Start up etcd server.
  44. go func() {
  45. c <- true
  46. s.ListenAndServe()
  47. }()
  48. <-c
  49. // Wait to make sure servers have started.
  50. time.Sleep(50 * time.Millisecond)
  51. // Execute the function passed in.
  52. f(s)
  53. // Clean up servers.
  54. ps.Close()
  55. s.Close()
  56. }