MCPcopy Index your code
hub / github.com/coder/coder / testDERPSend

Function testDERPSend

enterprise/wsproxy/wsproxy_test.go:1083–1122  ·  view source on GitHub ↗

testDERPSend sends a message from src to dstKey and waits for it to be received on dstCh. If the packet doesn't arrive within 500ms, it will try to send it again until the context expires. nolint:revive

(t *testing.T, ctx context.Context, dstKey key.NodePublic, dstCh <-chan derp.ReceivedPacket, src *derphttp.Client)

Source from the content-addressed store, hash-verified

1081//
1082//nolint:revive
1083func testDERPSend(t *testing.T, ctx context.Context, dstKey key.NodePublic, dstCh <-chan derp.ReceivedPacket, src *derphttp.Client) {
1084 t.Helper()
1085
1086 // The prefix helps identify where the packet starts if you get garbled data
1087 // in logs.
1088 const msgStrPrefix = "test_packet_"
1089 msgStr, err := cryptorand.String(64 - len(msgStrPrefix))
1090 require.NoError(t, err, "generate random msg string")
1091 msg := []byte(msgStrPrefix + msgStr)
1092
1093 err = src.Send(dstKey, msg)
1094 require.NoError(t, err, "send message via DERP")
1095
1096 ticker := time.NewTicker(time.Millisecond * 500)
1097 defer ticker.Stop()
1098 for {
1099 select {
1100 case pkt := <-dstCh:
1101 if pkt.Source != src.SelfPublicKey() {
1102 t.Logf("packet came from wrong source: %s", pkt.Source)
1103 continue
1104 }
1105 if !bytes.Equal(pkt.Data, msg) {
1106 t.Logf("packet data is wrong: %s", pkt.Data)
1107 continue
1108 }
1109 return
1110 case <-ctx.Done():
1111 t.Fatal("timed out waiting for valid packet")
1112 return
1113 case <-ticker.C:
1114 }
1115
1116 // Send another packet. Since we're sending packets immediately
1117 // after opening the clients, they might not be meshed together
1118 // properly yet.
1119 err = src.Send(dstKey, msg)
1120 require.NoError(t, err, "send message via DERP")
1121 }
1122}
1123
1124type createProxyReplicasOptions struct {
1125 API *coderd.API

Callers 1

TestDERPMeshFunction · 0.85

Calls 8

StringFunction · 0.92
FatalMethod · 0.80
HelperMethod · 0.65
SendMethod · 0.65
StopMethod · 0.65
LogfMethod · 0.65
EqualMethod · 0.45
DoneMethod · 0.45

Tested by

no test coverage detected