mirror of
https://github.com/shtorm-7/sing-box-extended.git
synced 2026-06-18 00:54:32 +03:00
refactor: connection manager
This commit is contained in:
43
route/conn_monitor_test.go
Normal file
43
route/conn_monitor_test.go
Normal file
@@ -0,0 +1,43 @@
|
||||
package route_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/sagernet/sing-box/route"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestMonitor(t *testing.T) {
|
||||
t.Parallel()
|
||||
var closer myCloser
|
||||
closer.Add(1)
|
||||
monitor := route.NewConnectionMonitor()
|
||||
require.NoError(t, monitor.Start())
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
|
||||
monitor.Add(ctx, &closer)
|
||||
done := make(chan struct{})
|
||||
go func() {
|
||||
closer.Wait()
|
||||
close(done)
|
||||
}()
|
||||
select {
|
||||
case <-done:
|
||||
case <-time.After(time.Second + 100*time.Millisecond):
|
||||
t.Fatal("timeout")
|
||||
}
|
||||
cancel()
|
||||
require.NoError(t, monitor.Close())
|
||||
}
|
||||
|
||||
type myCloser struct {
|
||||
sync.WaitGroup
|
||||
}
|
||||
|
||||
func (c *myCloser) Close() error {
|
||||
c.Done()
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user