2018-02-05 16:05:59 -05:00
|
|
|
package memory // import "github.com/docker/docker/pkg/discovery/memory"
|
2016-01-06 17:57:02 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2019-08-08 20:38:35 -04:00
|
|
|
"github.com/docker/docker/internal/test/suite"
|
2016-01-06 17:57:02 -05:00
|
|
|
"github.com/docker/docker/pkg/discovery"
|
2020-02-07 08:39:24 -05:00
|
|
|
"gotest.tools/v3/assert"
|
2016-01-06 17:57:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
// Hook up gocheck into the "go test" runner.
|
2019-08-08 20:38:35 -04:00
|
|
|
func Test(t *testing.T) {
|
|
|
|
suite.Run(t, &discoverySuite{})
|
|
|
|
}
|
2016-01-06 17:57:02 -05:00
|
|
|
|
|
|
|
type discoverySuite struct{}
|
|
|
|
|
2019-09-09 17:05:55 -04:00
|
|
|
func (s *discoverySuite) TestWatch(c *testing.T) {
|
2016-01-06 17:57:02 -05:00
|
|
|
d := &Discovery{}
|
|
|
|
d.Initialize("foo", 1000, 0, nil)
|
|
|
|
stopCh := make(chan struct{})
|
|
|
|
ch, errCh := d.Watch(stopCh)
|
|
|
|
|
|
|
|
// We have to drain the error channel otherwise Watch will get stuck.
|
|
|
|
go func() {
|
|
|
|
for range errCh {
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
expected := discovery.Entries{
|
|
|
|
&discovery.Entry{Host: "1.1.1.1", Port: "1111"},
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, d.Register("1.1.1.1:1111") == nil)
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.DeepEqual(c, <-ch, expected)
|
2016-01-06 17:57:02 -05:00
|
|
|
|
|
|
|
expected = discovery.Entries{
|
|
|
|
&discovery.Entry{Host: "1.1.1.1", Port: "1111"},
|
|
|
|
&discovery.Entry{Host: "2.2.2.2", Port: "2222"},
|
|
|
|
}
|
|
|
|
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, d.Register("2.2.2.2:2222") == nil)
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.DeepEqual(c, <-ch, expected)
|
2016-01-06 17:57:02 -05:00
|
|
|
|
|
|
|
// Stop and make sure it closes all channels.
|
|
|
|
close(stopCh)
|
2019-09-09 17:05:57 -04:00
|
|
|
assert.Assert(c, <-ch == nil)
|
|
|
|
assert.Assert(c, <-errCh == nil)
|
2016-01-06 17:57:02 -05:00
|
|
|
}
|