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

Function TestWSCompression

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

Source from the content-addressed store, hash-verified

223}
224
225func TestWSCompression(t *testing.T) {
226 msgSize := rand.Intn(40000)
227 for _, test := range []struct {
228 name string
229 srvCompression bool
230 cliCompression bool
231 }{
232 {"srv_off_cli_off", false, false},
233 {"srv_off_cli_on", false, true},
234 {"srv_on_cli_off", true, false},
235 {"srv_on_cli_on", true, true},
236 } {
237 t.Run(test.name, func(t *testing.T) {
238 sopts := testWSGetDefaultOptions(t, false)
239 sopts.Websocket.Compression = test.srvCompression
240 s := RunServerWithOptions(sopts)
241 defer s.Shutdown()
242
243 url := fmt.Sprintf("ws://127.0.0.1:%d", sopts.Websocket.Port)
244 var opts []nats.Option
245 if test.cliCompression {
246 opts = append(opts, nats.Compression(true))
247 }
248 nc, err := nats.Connect(url, opts...)
249 if err != nil {
250 t.Fatalf("Error on connect: %v", err)
251 }
252 defer nc.Close()
253
254 sub, err := nc.SubscribeSync("foo")
255 if err != nil {
256 t.Fatalf("Error on subscribe: %v", err)
257 }
258
259 msgs := make([][]byte, 100)
260 for i := 0; i < len(msgs); i++ {
261 msg := make([]byte, msgSize)
262 for j := 0; j < len(msg); j++ {
263 msg[j] = 'A'
264 }
265 msgs[i] = msg
266 }
267 for i, msg := range msgs {
268 if err := nc.Publish("foo", msg); err != nil {
269 t.Fatalf("Error on publish: %v", err)
270 }
271 // Make sure that compression/masking does not touch user data
272 if !bytes.Equal(msgs[i], msg) {
273 t.Fatalf("User content has been changed: %v, got %v", msgs[i], msg)
274 }
275 }
276
277 for i := 0; i < len(msgs); i++ {
278 msg, err := sub.NextMsg(time.Second)
279 if err != nil {
280 t.Fatalf("Error getting next message (%d): %v", i+1, err)
281 }
282 if !bytes.Equal(msgs[i], msg.Data) {

Callers

nothing calls this directly

Calls 9

testWSGetDefaultOptionsFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
EqualMethod · 0.80
NextMsgMethod · 0.80
RunServerWithOptionsFunction · 0.70
SubscribeSyncMethod · 0.65
PublishMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected