memory_test.go 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package memory
  2. import (
  3. "testing"
  4. "github.com/docker/docker/pkg/discovery"
  5. "github.com/go-check/check"
  6. )
  7. // Hook up gocheck into the "go test" runner.
  8. func Test(t *testing.T) { check.TestingT(t) }
  9. type discoverySuite struct{}
  10. var _ = check.Suite(&discoverySuite{})
  11. func (s *discoverySuite) TestWatch(c *check.C) {
  12. d := &Discovery{}
  13. d.Initialize("foo", 1000, 0, nil)
  14. stopCh := make(chan struct{})
  15. ch, errCh := d.Watch(stopCh)
  16. // We have to drain the error channel otherwise Watch will get stuck.
  17. go func() {
  18. for range errCh {
  19. }
  20. }()
  21. expected := discovery.Entries{
  22. &discovery.Entry{Host: "1.1.1.1", Port: "1111"},
  23. }
  24. c.Assert(d.Register("1.1.1.1:1111"), check.IsNil)
  25. c.Assert(<-ch, check.DeepEquals, expected)
  26. expected = discovery.Entries{
  27. &discovery.Entry{Host: "1.1.1.1", Port: "1111"},
  28. &discovery.Entry{Host: "2.2.2.2", Port: "2222"},
  29. }
  30. c.Assert(d.Register("2.2.2.2:2222"), check.IsNil)
  31. c.Assert(<-ch, check.DeepEquals, expected)
  32. // Stop and make sure it closes all channels.
  33. close(stopCh)
  34. c.Assert(<-ch, check.IsNil)
  35. c.Assert(<-errCh, check.IsNil)
  36. }