github.com/sijibomii/docker@v0.0.0-20231230191044-5cf6ca554647/pkg/discovery/memory/memory_test.go (about)

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