server_utils.go 1.1 KB

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