-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
serve_test.go
60 lines (50 loc) · 1.17 KB
/
serve_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-FileCopyrightText: 2021 The Go Language Server Authors
// SPDX-License-Identifier: BSD-3-Clause
package jsonrpc2_test
import (
"context"
"errors"
"net"
"sync"
"testing"
"time"
"go.lsp.dev/jsonrpc2"
)
func TestIdleTimeout(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
ln, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatal(err)
}
defer ln.Close()
connect := func() net.Conn {
conn, err := net.DialTimeout("tcp", ln.Addr().String(), 5*time.Second)
if err != nil {
panic(err)
}
return conn
}
server := jsonrpc2.HandlerServer(jsonrpc2.MethodNotFoundHandler)
var (
runErr error
wg sync.WaitGroup
)
wg.Add(1)
go func() {
defer wg.Done()
runErr = jsonrpc2.Serve(ctx, ln, server, 100*time.Millisecond)
}()
// Exercise some connection/disconnection patterns, and then assert that when
// our timer fires, the server exits.
conn1 := connect()
conn2 := connect()
conn1.Close()
conn2.Close()
conn3 := connect()
conn3.Close()
wg.Wait()
if !errors.Is(runErr, jsonrpc2.ErrIdleTimeout) {
t.Errorf("run() returned error %v, want %v", runErr, jsonrpc2.ErrIdleTimeout)
}
}