| 9 | "github.com/stretchr/testify/assert" |
| 10 | "github.com/stretchr/testify/require" |
| 11 | ) |
| 12 |
| 13 | // fakeLn is a controllable net.Listener for tests. |
| 14 | type fakeLn struct { |
| 15 | connCh chan net.Conn |
| 16 | closed chan struct{} |
| 17 | once sync.Once |
| 18 | } |
| 19 |
| 20 | func newFakeLn() *fakeLn { |
| 21 | return &fakeLn{ |
| 22 | connCh: make(chan net.Conn, 8), |
| 23 | closed: make(chan struct{}), |
| 24 | } |
| 25 | } |
| 26 |
| 27 | func (f *fakeLn) Accept() (net.Conn, error) { |
| 28 | select { |
| 29 | case c := <-f.connCh: |
| 30 | return c, nil |
| 31 | case <-f.closed: |
| 32 | return nil, net.ErrClosed |
| 33 | } |
| 34 | } |
| 35 |
| 36 | func (f *fakeLn) Close() error { |
| 37 | f.once.Do(func() { close(f.closed) }) |
| 38 | return nil |
| 39 | } |
| 40 |
| 41 | func (f *fakeLn) Addr() net.Addr { return fakeAddr("127.0.0.1:9999") } |
| 42 |
| 43 | func (f *fakeLn) inject(c net.Conn) { |
| 44 | select { |
| 45 | case f.connCh <- c: |
| 46 | case <-f.closed: |
| 47 | } |
| 48 | } |
| 49 |
| 50 | func TestBaseGroup_WorkerFanOut(t *testing.T) { |
| 51 | fl := newFakeLn() |
| 52 | var bg baseGroup |
| 53 | bg.initBase("g", "key", fl, func() {}) |
| 54 |
| 55 | go bg.worker(fl, bg.acceptCh) |
| 56 |
| 57 | c1, c2 := net.Pipe() |
| 58 | defer c2.Close() |
| 59 | fl.inject(c1) |
| 60 |
| 61 | select { |
| 62 | case got := <-bg.acceptCh: |
| 63 | assert.Equal(t, c1, got) |
| 64 | got.Close() |
| 65 | case <-time.After(time.Second): |
| 66 | t.Fatal("timed out waiting for connection on acceptCh") |
| 67 | } |
| 68 |
| 69 | fl.Close() |
| 70 | } |
| 71 |
| 72 | func TestBaseGroup_WorkerStopsOnListenerClose(t *testing.T) { |
| 73 | fl := newFakeLn() |
| 74 | var bg baseGroup |
| 75 | bg.initBase("g", "key", fl, func() {}) |
| 76 |
| 77 | done := make(chan struct{}) |
| 78 | go func() { |
| 79 | bg.worker(fl, bg.acceptCh) |
| 80 | close(done) |
| 81 | }() |
| 82 |
| 83 | fl.Close() |
| 84 | select { |
| 85 | case <-done: |
| 86 | case <-time.After(time.Second): |
| 87 | t.Fatal("worker did not stop after listener close") |
| 88 | } |
| 89 | } |
| 90 |
| 91 | func TestBaseGroup_WorkerClosesConnOnClosedChannel(t *testing.T) { |
| 92 | fl := newFakeLn() |
| 93 | var bg baseGroup |
| 94 | bg.initBase("g", "key", fl, func() {}) |
| 95 |
| 96 | // Close acceptCh before worker sends. |
| 97 | close(bg.acceptCh) |
| 98 |
| 99 | done := make(chan struct{}) |
| 100 | go func() { |
| 101 | bg.worker(fl, bg.acceptCh) |
| 102 | close(done) |
| 103 | }() |
| 104 |
| 105 | c1, c2 := net.Pipe() |
| 106 | defer c2.Close() |
| 107 | fl.inject(c1) |
| 108 |
| 109 | select { |
| 110 | case <-done: |
| 111 | case <-time.After(time.Second): |
| 112 | t.Fatal("worker did not stop after panic recovery") |
| 113 | } |
| 114 |
| 115 | // c1 should have been closed by worker's panic recovery path. |
| 116 | buf := make([]byte, 1) |
| 117 | _, err := c1.Read(buf) |
| 118 | assert.Error(t, err, "connection should be closed by worker") |
| 119 | } |
| 120 |
| 121 | func TestBaseGroup_CloseLastListenerTriggersCleanup(t *testing.T) { |
| 122 | fl := newFakeLn() |
| 123 | var bg baseGroup |
| 124 | cleanupCalled := 0 |
| 125 | bg.initBase("g", "key", fl, func() { cleanupCalled++ }) |
| 126 |
| 127 | bg.mu.Lock() |
| 128 | ln1 := bg.newListener(fl.Addr()) |
| 129 | ln2 := bg.newListener(fl.Addr()) |
| 130 | bg.mu.Unlock() |
| 131 |
| 132 | go bg.worker(fl, bg.acceptCh) |
| 133 |
| 134 | ln1.Close() |
| 135 | assert.Equal(t, 0, cleanupCalled, "cleanup should not run while listeners remain") |
| 136 |
| 137 | ln2.Close() |
| 138 | assert.Equal(t, 1, cleanupCalled, "cleanup should run after last listener closed") |
| 139 | } |
| 140 |
| 141 | func TestBaseGroup_CloseOneOfTwoListeners(t *testing.T) { |
| 142 | fl := newFakeLn() |
| 143 | var bg baseGroup |
| 144 | cleanupCalled := 0 |
| 145 | bg.initBase("g", "key", fl, func() { cleanupCalled++ }) |
| 146 |
| 147 | bg.mu.Lock() |
| 148 | ln1 := bg.newListener(fl.Addr()) |
| 149 | ln2 := bg.newListener(fl.Addr()) |
| 150 | bg.mu.Unlock() |
| 151 |
| 152 | go bg.worker(fl, bg.acceptCh) |
| 153 |
| 154 | ln1.Close() |
| 155 | assert.Equal(t, 0, cleanupCalled) |
| 156 |
| 157 | // ln2 should still receive connections. |
| 158 | c1, c2 := net.Pipe() |
| 159 | defer c2.Close() |
| 160 | fl.inject(c1) |
| 161 |
| 162 | got, err := ln2.Accept() |
| 163 | require.NoError(t, err) |
| 164 | assert.Equal(t, c1, got) |
| 165 | got.Close() |
| 166 |
| 167 | ln2.Close() |
| 168 | assert.Equal(t, 1, cleanupCalled) |
| 169 | } |