MCPcopy Index your code
hub / github.com/cloudflare/cloudflared / testActiveSessionNotClosed

Function testActiveSessionNotClosed

datagramsession/session_test.go:114–183  ·  view source on GitHub ↗
(t *testing.T, readFromDst bool, writeToDst bool)

Source from the content-addressed store, hash-verified

112}
113
114func testActiveSessionNotClosed(t *testing.T, readFromDst bool, writeToDst bool) {
115 const closeAfterIdle = time.Millisecond * 100
116 const activeTime = time.Millisecond * 500
117
118 sessionID := uuid.New()
119 cfdConn, originConn := net.Pipe()
120 payload := testPayload(sessionID)
121
122 respChan := make(chan *packet.Session)
123 sender := newMockTransportSender(sessionID, payload)
124 mg := NewManager(&nopLogger, sender.muxSession, respChan)
125 session := mg.newSession(sessionID, cfdConn)
126
127 startTime := time.Now()
128 activeUntil := startTime.Add(activeTime)
129 ctx, cancel := context.WithCancel(t.Context())
130 errGroup, ctx := errgroup.WithContext(ctx)
131 errGroup.Go(func() error {
132 _, _ = session.Serve(ctx, closeAfterIdle)
133 if time.Now().Before(startTime.Add(activeTime)) {
134 return fmt.Errorf("session closed while it's still active")
135 }
136 return nil
137 })
138
139 if readFromDst {
140 errGroup.Go(func() error {
141 for {
142 if time.Now().After(activeUntil) {
143 return nil
144 }
145 if _, err := originConn.Write(payload); err != nil {
146 return err
147 }
148 time.Sleep(closeAfterIdle / 2)
149 }
150 })
151 }
152 if writeToDst {
153 errGroup.Go(func() error {
154 readBuffer := make([]byte, len(payload))
155 for {
156 n, err := originConn.Read(readBuffer)
157 if err != nil {
158 if err == io.EOF || err == io.ErrClosedPipe {
159 return nil
160 }
161 return err
162 }
163 if !bytes.Equal(payload, readBuffer[:n]) {
164 return fmt.Errorf("payload %v is not equal to %v", readBuffer[:n], payload)
165 }
166 }
167 })
168 errGroup.Go(func() error {
169 for {
170 if time.Now().After(activeUntil) {
171 return nil

Calls 13

testPayloadFunction · 0.85
newMockTransportSenderFunction · 0.85
NewManagerFunction · 0.85
newSessionMethod · 0.80
ErrorfMethod · 0.80
transportToDstMethod · 0.80
WaitMethod · 0.80
AddMethod · 0.65
ContextMethod · 0.65
ServeMethod · 0.65
WriteMethod · 0.65
EqualMethod · 0.65

Tested by

no test coverage detected