server_utils.go 1.2 KB

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