server_utils.go 1.4 KB

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