MCPcopy
hub / github.com/nats-io/nats.go / TestLameDuckMode

Function TestLameDuckMode

nats_test.go:1671–1763  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1669}
1670
1671func TestLameDuckMode(t *testing.T) {
1672 l, err := net.Listen("tcp", "127.0.0.1:0")
1673 if err != nil {
1674 t.Fatalf("Could not listen on an ephemeral port: %v", err)
1675 }
1676 tl := l.(*net.TCPListener)
1677 defer tl.Close()
1678
1679 addr := tl.Addr().(*net.TCPAddr)
1680
1681 wg := sync.WaitGroup{}
1682 wg.Add(1)
1683 go func() {
1684 defer wg.Done()
1685 ldmInfos := []string{"INFO {\"ldm\":true}\r\n", "INFO {\"connect_urls\":[\"127.0.0.1:1234\"],\"ldm\":true}\r\n"}
1686 for _, ldmInfo := range ldmInfos {
1687 conn, err := l.Accept()
1688 if err != nil {
1689 return
1690 }
1691 defer conn.Close()
1692
1693 info := "INFO {\"server_id\":\"foobar\"}\r\n"
1694 conn.Write([]byte(info))
1695
1696 // Read connect and ping commands sent from the client
1697 br := bufio.NewReaderSize(conn, 10*1024)
1698 br.ReadLine()
1699 br.ReadLine()
1700 conn.Write([]byte(pongProto))
1701
1702 // Wait a bit and then send a INFO with LDM
1703 time.Sleep(100 * time.Millisecond)
1704 conn.Write([]byte(ldmInfo))
1705 br.ReadLine()
1706 conn.Close()
1707 }
1708 }()
1709
1710 url := fmt.Sprintf("nats://127.0.0.1:%d", addr.Port)
1711 time.Sleep(100 * time.Millisecond)
1712
1713 for _, test := range []struct {
1714 name string
1715 curls bool
1716 }{
1717 {"without connect urls", false},
1718 {"with connect urls", true},
1719 } {
1720 t.Run(test.name, func(t *testing.T) {
1721 ch := make(chan bool, 1)
1722 errCh := make(chan error, 1)
1723 nc, err := Connect(url,
1724 DiscoveredServersHandler(func(nc *Conn) {
1725 ds := nc.DiscoveredServers()
1726 if !reflect.DeepEqual(ds, []string{"nats://127.0.0.1:1234"}) {
1727 errCh <- fmt.Errorf("wrong discovered servers: %q", ds)
1728 } else {

Callers

nothing calls this directly

Calls 11

ConnectFunction · 0.85
DiscoveredServersHandlerFunction · 0.85
LameDuckModeHandlerFunction · 0.85
FatalfMethod · 0.80
DiscoveredServersMethod · 0.80
ErrorfMethod · 0.80
AddMethod · 0.65
DoneMethod · 0.65
ErrorMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected