Debian Codegraph @

test/gosrv_test.go

raw
1 // Copyright 2012-2025 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package test
15
16 import (
17 "fmt"
18 "net"
19 "runtime"
20 "testing"
21 "time"
22 )
23
24 func TestSimpleGoServerShutdown(t *testing.T) {
25 base := runtime.NumGoroutine()
26 opts := DefaultTestOptions
27 opts.Port = -1
28 s := RunServer(&opts)
29 s.Shutdown()
30 checkFor(t, time.Second, 100*time.Millisecond, func() error {
31 delta := (runtime.NumGoroutine() - base)
32 if delta > 1 {
33 return fmt.Errorf("%d go routines still exist post Shutdown()", delta)
34 }
35 return nil
36 })
37 }
38
39 func TestGoServerShutdownWithClients(t *testing.T) {
40 base := runtime.NumGoroutine()
41 opts := DefaultTestOptions
42 opts.Port = -1
43 s := RunServer(&opts)
44 addr := s.Addr().(*net.TCPAddr)
45 for i := 0; i < 50; i++ {
46 createClientConn(t, "127.0.0.1", addr.Port)
47 }
48 s.Shutdown()
49 // Wait longer for client connections
50 time.Sleep(1 * time.Second)
51 delta := (runtime.NumGoroutine() - base)
52 // There may be some finalizers or IO, but in general more than
53 // 2 as a delta represents a problem.
54 if delta > 2 {
55 t.Fatalf("%d Go routines still exist post Shutdown()", delta)
56 }
57 }
58
59 func TestGoServerMultiShutdown(t *testing.T) {
60 opts := DefaultTestOptions
61 opts.Port = -1
62 s := RunServer(&opts)
63 s.Shutdown()
64 s.Shutdown()
65 }