proxy_test.go 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package test
  2. import (
  3. "bytes"
  4. "fmt"
  5. "os"
  6. "testing"
  7. "time"
  8. "github.com/coreos/etcd/server"
  9. "github.com/coreos/etcd/tests"
  10. "github.com/coreos/etcd/third_party/github.com/coreos/go-etcd/etcd"
  11. "github.com/coreos/etcd/third_party/github.com/stretchr/testify/assert"
  12. )
  13. // Create a full cluster and then add extra an extra proxy node.
  14. func TestProxy(t *testing.T) {
  15. clusterSize := 10 // DefaultActiveSize + 1
  16. _, etcds, err := CreateCluster(clusterSize, &os.ProcAttr{Files: []*os.File{nil, os.Stdout, os.Stderr}}, false)
  17. assert.NoError(t, err)
  18. defer DestroyCluster(etcds)
  19. if err != nil {
  20. t.Fatal("cannot create cluster")
  21. }
  22. c := etcd.NewClient(nil)
  23. c.SyncCluster()
  24. // Set key.
  25. time.Sleep(time.Second)
  26. if _, err := c.Set("foo", "bar", 0); err != nil {
  27. panic(err)
  28. }
  29. time.Sleep(time.Second)
  30. // Check that all peers and proxies have the value.
  31. for i, _ := range etcds {
  32. resp, err := tests.Get(fmt.Sprintf("http://localhost:%d/v2/keys/foo", 4000 + (i+1)))
  33. if assert.NoError(t, err) {
  34. body := tests.ReadBodyJSON(resp)
  35. if node, _ := body["node"].(map[string]interface{}); assert.NotNil(t, node) {
  36. assert.Equal(t, node["value"], "bar")
  37. }
  38. }
  39. }
  40. time.Sleep(server.ActiveMonitorTimeout + (1 * time.Second))
  41. // Reconfigure with larger active size (10 nodes) and wait for promotion.
  42. resp, _ := tests.Put("http://localhost:7001/config", "application/json", bytes.NewBufferString(`{"activeSize":10, "promoteDelay":1800}`))
  43. if !assert.Equal(t, resp.StatusCode, 200) {
  44. t.FailNow()
  45. }
  46. time.Sleep(server.ActiveMonitorTimeout + (1 * time.Second))
  47. // Verify that the proxy node is now a peer.
  48. fmt.Println("CHECK!")
  49. time.Sleep(30 * time.Second)
  50. }