github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/discovery/memory/memory_test.go (about) 1 package memory // import "github.com/demonoid81/moby/pkg/discovery/memory" 2 3 import ( 4 "testing" 5 6 "github.com/demonoid81/moby/internal/test/suite" 7 "github.com/demonoid81/moby/pkg/discovery" 8 "gotest.tools/v3/assert" 9 ) 10 11 // Hook up gocheck into the "go test" runner. 12 func Test(t *testing.T) { 13 suite.Run(t, &discoverySuite{}) 14 } 15 16 type discoverySuite struct{} 17 18 func (s *discoverySuite) TestWatch(c *testing.T) { 19 d := &Discovery{} 20 d.Initialize("foo", 1000, 0, nil) 21 stopCh := make(chan struct{}) 22 ch, errCh := d.Watch(stopCh) 23 24 // We have to drain the error channel otherwise Watch will get stuck. 25 go func() { 26 for range errCh { 27 } 28 }() 29 30 expected := discovery.Entries{ 31 &discovery.Entry{Host: "1.1.1.1", Port: "1111"}, 32 } 33 34 assert.Assert(c, d.Register("1.1.1.1:1111") == nil) 35 assert.DeepEqual(c, <-ch, expected) 36 37 expected = discovery.Entries{ 38 &discovery.Entry{Host: "1.1.1.1", Port: "1111"}, 39 &discovery.Entry{Host: "2.2.2.2", Port: "2222"}, 40 } 41 42 assert.Assert(c, d.Register("2.2.2.2:2222") == nil) 43 assert.DeepEqual(c, <-ch, expected) 44 45 // Stop and make sure it closes all channels. 46 close(stopCh) 47 assert.Assert(c, <-ch == nil) 48 assert.Assert(c, <-errCh == nil) 49 }