server_utils.go 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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 = 50
  15. testElectionTimeout = 200
  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. ps := server.NewPeerServer(testName, path, "http://" + testRaftURL, testRaftURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, registry, store, testSnapshotCount)
  24. ps.MaxClusterSize = 9
  25. ps.ElectionTimeout = testElectionTimeout
  26. ps.HeartbeatTimeout = testHeartbeatTimeout
  27. s := server.New(testName, "http://" + testClientURL, testClientURL, &server.TLSConfig{Scheme: "http"}, &server.TLSInfo{}, ps, registry, store)
  28. ps.SetServer(s)
  29. // Start up peer server.
  30. c := make(chan bool)
  31. go func() {
  32. c <- true
  33. ps.ListenAndServe(false, []string{})
  34. }()
  35. <-c
  36. // Start up etcd server.
  37. go func() {
  38. c <- true
  39. s.ListenAndServe()
  40. }()
  41. <-c
  42. // Wait to make sure servers have started.
  43. time.Sleep(50 * time.Millisecond)
  44. // Execute the function passed in.
  45. f(s)
  46. // Clean up servers.
  47. ps.Close()
  48. s.Close()
  49. }