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

Function TestWSControlFrames

test/ws_test.go:107–182  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

105}
106
107func TestWSControlFrames(t *testing.T) {
108 sopts := testWSGetDefaultOptions(t, false)
109 s := RunServerWithOptions(sopts)
110 defer s.Shutdown()
111
112 rch := make(chan bool, 10)
113 ncSub, err := nats.Connect(s.ClientURL(),
114 nats.ReconnectWait(50*time.Millisecond),
115 nats.ReconnectHandler(func(_ *nats.Conn) { rch <- true }),
116 )
117 if err != nil {
118 t.Fatalf("Error on connect: %v", err)
119 }
120 defer ncSub.Close()
121
122 sub, err := ncSub.SubscribeSync("foo")
123 if err != nil {
124 t.Fatalf("Error on subscribe: %v", err)
125 }
126 if err := ncSub.Flush(); err != nil {
127 t.Fatalf("Error on flush: %v", err)
128 }
129
130 dch := make(chan error, 10)
131 url := fmt.Sprintf("ws://127.0.0.1:%d", sopts.Websocket.Port)
132 nc, err := nats.Connect(url,
133 nats.ReconnectWait(50*time.Millisecond),
134 nats.DisconnectErrHandler(func(_ *nats.Conn, err error) { dch <- err }),
135 nats.ReconnectHandler(func(_ *nats.Conn) { rch <- true }),
136 )
137 if err != nil {
138 t.Fatalf("Error on connect: %v", err)
139 }
140 defer nc.Close()
141
142 // Shutdown the server, which should send a close message, which by
143 // spec the client will try to echo back.
144 s.Shutdown()
145
146 select {
147 case <-dch:
148 // OK
149 case <-time.After(time.Second):
150 t.Fatal("Should have been disconnected")
151 }
152
153 s = RunServerWithOptions(sopts)
154 defer s.Shutdown()
155
156 // Wait for both connections to reconnect
157 if err := Wait(rch); err != nil {
158 t.Fatalf("Should have reconnected: %v", err)
159 }
160 if err := Wait(rch); err != nil {
161 t.Fatalf("Should have reconnected: %v", err)
162 }
163 // Even if the first connection reconnects, there is no guarantee
164 // that the resend of SUB has yet been processed by the server.

Callers

nothing calls this directly

Calls 12

testWSGetDefaultOptionsFunction · 0.85
ConnectMethod · 0.80
ReconnectHandlerMethod · 0.80
FatalfMethod · 0.80
DisconnectErrHandlerMethod · 0.80
NextMsgMethod · 0.80
RunServerWithOptionsFunction · 0.70
WaitFunction · 0.70
SubscribeSyncMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected