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

Function TestLastErrorNoRace

test/conn_test.go:1734–1774  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1732}
1733
1734func TestLastErrorNoRace(t *testing.T) {
1735 s := RunDefaultServer()
1736 defer s.Shutdown()
1737
1738 // Access LastError in disconnection and closed handlers to make sure
1739 // that there is no race. It is possible in some cases that
1740 // nc.LastError() returns a non nil error. We don't care here about the
1741 // returned value.
1742 dch := func(c *nats.Conn) {
1743 c.LastError()
1744 }
1745 closedCh := make(chan struct{})
1746 cch := func(c *nats.Conn) {
1747 c.LastError()
1748 closedCh <- struct{}{}
1749 }
1750 nc, err := nats.Connect(nats.DefaultURL,
1751 nats.DisconnectHandler(dch),
1752 nats.ClosedHandler(cch),
1753 nats.MaxReconnects(-1),
1754 nats.ReconnectWait(5*time.Millisecond),
1755 nats.ReconnectJitter(0, 0))
1756 if err != nil {
1757 t.Fatalf("Unable to connect: %v\n", err)
1758 }
1759 defer nc.Close()
1760
1761 // Restart the server several times to trigger a reconnection.
1762 for i := 0; i < 10; i++ {
1763 s.Shutdown()
1764 time.Sleep(10 * time.Millisecond)
1765 s = RunDefaultServer()
1766 }
1767 nc.Close()
1768 s.Shutdown()
1769 select {
1770 case <-closedCh:
1771 case <-time.After(5 * time.Second):
1772 t.Fatal("Timeout waiting for the closed callback")
1773 }
1774}
1775
1776type customDialer struct {
1777 ch chan bool

Callers

nothing calls this directly

Calls 6

ConnectMethod · 0.80
ClosedHandlerMethod · 0.80
FatalfMethod · 0.80
RunDefaultServerFunction · 0.70
LastErrorMethod · 0.45
CloseMethod · 0.45

Tested by

no test coverage detected