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

Function TestWSBasic

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

Source from the content-addressed store, hash-verified

59}
60
61func TestWSBasic(t *testing.T) {
62 sopts := testWSGetDefaultOptions(t, false)
63 s := RunServerWithOptions(sopts)
64 defer s.Shutdown()
65
66 url := fmt.Sprintf("ws://127.0.0.1:%d", sopts.Websocket.Port)
67 nc, err := nats.Connect(url)
68 if err != nil {
69 t.Fatalf("Error on connect: %v", err)
70 }
71 defer nc.Close()
72
73 sub, err := nc.SubscribeSync("foo")
74 if err != nil {
75 t.Fatalf("Error on subscribe: %v", err)
76 }
77
78 msgs := make([][]byte, 100)
79 for i := 0; i < len(msgs); i++ {
80 msg := make([]byte, rand.Intn(70000))
81 for j := 0; j < len(msg); j++ {
82 msg[j] = 'A' + byte(rand.Intn(26))
83 }
84 msgs[i] = msg
85 }
86 for i, msg := range msgs {
87 if err := nc.Publish("foo", msg); err != nil {
88 t.Fatalf("Error on publish: %v", err)
89 }
90 // Make sure that masking does not overwrite user data
91 if !bytes.Equal(msgs[i], msg) {
92 t.Fatalf("User content has been changed: %v, got %v", msgs[i], msg)
93 }
94 }
95
96 for i := 0; i < len(msgs); i++ {
97 msg, err := sub.NextMsg(time.Second)
98 if err != nil {
99 t.Fatalf("Error getting next message: %v", err)
100 }
101 if !bytes.Equal(msgs[i], msg.Data) {
102 t.Fatalf("Expected message: %v, got %v", msgs[i], msg)
103 }
104 }
105}
106
107func TestWSControlFrames(t *testing.T) {
108 sopts := testWSGetDefaultOptions(t, false)

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