| 1 | // Copyright 2026 The frp Authors |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package server |
| 16 | |
| 17 | import ( |
| 18 | "errors" |
| 19 | "net" |
| 20 | "testing" |
| 21 | "time" |
| 22 | |
| 23 | "github.com/stretchr/testify/require" |
| 24 | ) |
| 25 | |
| 26 | func TestWriteWithDeadlineTimesOutAndClearsDeadline(t *testing.T) { |
| 27 | serverConn, clientConn := net.Pipe() |
| 28 | defer serverConn.Close() |
| 29 | defer clientConn.Close() |
| 30 | |
| 31 | err := writeWithDeadline(serverConn, 50*time.Millisecond, func() error { |
| 32 | _, writeErr := serverConn.Write([]byte("x")) |
| 33 | return writeErr |
| 34 | }) |
| 35 | require.Error(t, err) |
| 36 | |
| 37 | var netErr net.Error |
| 38 | require.True(t, errors.As(err, &netErr)) |
| 39 | require.True(t, netErr.Timeout()) |
| 40 | |
| 41 | readCh := make(chan byte, 1) |
| 42 | errCh := make(chan error, 1) |
| 43 | go func() { |
| 44 | buf := make([]byte, 1) |
| 45 | if _, readErr := clientConn.Read(buf); readErr != nil { |
| 46 | errCh <- readErr |
| 47 | return |
| 48 | } |
| 49 | readCh <- buf[0] |
| 50 | }() |
| 51 | |
| 52 | _, err = serverConn.Write([]byte("y")) |
| 53 | require.NoError(t, err) |
| 54 | |
| 55 | select { |
| 56 | case b := <-readCh: |
| 57 | require.Equal(t, byte('y'), b) |
| 58 | case err := <-errCh: |
| 59 | require.NoError(t, err) |
| 60 | case <-time.After(time.Second): |
| 61 | t.Fatal("timed out waiting for write after deadline reset") |
| 62 | } |
| 63 | } |