|
| 1 | +package groups |
| 2 | + |
| 3 | +import ( |
| 4 | + "slices" |
| 5 | + "sync" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | + |
| 9 | + "github.com/sagernet/sing-box/adapter" |
| 10 | + "github.com/sagernet/sing-box/experimental/clashapi/trafficontrol" |
| 11 | + "github.com/sagernet/sing-box/log" |
| 12 | + "github.com/stretchr/testify/assert" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | +) |
| 15 | + |
| 16 | +func TestRemovalQueue(t *testing.T) { |
| 17 | + logger := log.NewNOPFactory().Logger() |
| 18 | + tag := "outbound" |
| 19 | + tests := []struct { |
| 20 | + name string |
| 21 | + outMgr *mockOutboundManager |
| 22 | + epMgr *mockEndpointManager |
| 23 | + connMgr *mockConnectionManager |
| 24 | + pending map[string]item |
| 25 | + forceAfter time.Duration |
| 26 | + assertFn func(t *testing.T, rq *removalQueue) |
| 27 | + }{ |
| 28 | + { |
| 29 | + name: "remove outbound", |
| 30 | + outMgr: &mockOutboundManager{tags: []string{tag}}, |
| 31 | + connMgr: &mockConnectionManager{}, |
| 32 | + pending: map[string]item{tag: {tag, false, time.Now()}}, |
| 33 | + forceAfter: time.Minute, |
| 34 | + assertFn: func(t *testing.T, rq *removalQueue) { |
| 35 | + assert.NotContains(t, rq.outMgr.(*mockOutboundManager).tags, tag, "tag should be removed") |
| 36 | + }, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "remove endpoint", |
| 40 | + epMgr: &mockEndpointManager{tags: []string{tag}}, |
| 41 | + connMgr: &mockConnectionManager{}, |
| 42 | + pending: map[string]item{tag: {tag, true, time.Now()}}, |
| 43 | + forceAfter: time.Minute, |
| 44 | + assertFn: func(t *testing.T, rq *removalQueue) { |
| 45 | + assert.NotContains(t, rq.epMgr.(*mockEndpointManager).tags, tag, "tag should be removed") |
| 46 | + }, |
| 47 | + }, |
| 48 | + { |
| 49 | + name: "force removal after duration", |
| 50 | + outMgr: &mockOutboundManager{tags: []string{tag}}, |
| 51 | + connMgr: &mockConnectionManager{ |
| 52 | + conns: []trafficontrol.TrackerMetadata{{Outbound: tag, ClosedAt: time.Time{}}}, |
| 53 | + }, |
| 54 | + pending: map[string]item{ |
| 55 | + tag: {tag, false, time.Now().Add(-time.Second * 10)}, |
| 56 | + }, |
| 57 | + forceAfter: time.Second, |
| 58 | + assertFn: func(t *testing.T, rq *removalQueue) { |
| 59 | + assert.NotContains(t, rq.outMgr.(*mockOutboundManager).tags, tag, "tag should be removed") |
| 60 | + }, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "don't remove if still in use", |
| 64 | + outMgr: &mockOutboundManager{tags: []string{tag}}, |
| 65 | + connMgr: &mockConnectionManager{ |
| 66 | + conns: []trafficontrol.TrackerMetadata{{Outbound: tag, ClosedAt: time.Time{}}}, |
| 67 | + }, |
| 68 | + pending: map[string]item{tag: {tag, false, time.Now()}}, |
| 69 | + forceAfter: time.Minute, |
| 70 | + assertFn: func(t *testing.T, rq *removalQueue) { |
| 71 | + assert.Contains(t, rq.outMgr.(*mockOutboundManager).tags, tag, "tag should still be present") |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "don't remove if re-added", |
| 76 | + outMgr: &mockOutboundManager{tags: []string{tag}}, |
| 77 | + connMgr: &mockConnectionManager{ |
| 78 | + conns: []trafficontrol.TrackerMetadata{{Outbound: tag, ClosedAt: time.Time{}}}, |
| 79 | + }, |
| 80 | + pending: map[string]item{tag: {tag, false, time.Now()}}, |
| 81 | + forceAfter: time.Minute, |
| 82 | + assertFn: func(t *testing.T, rq *removalQueue) { |
| 83 | + require.Contains(t, rq.outMgr.(*mockOutboundManager).tags, tag, "tag should still be present before re-adding") |
| 84 | + |
| 85 | + rq.dequeue(tag) |
| 86 | + rq.connMgr.Connections()[0].ClosedAt = time.Now() // simulate connection closed |
| 87 | + rq.checkPending() |
| 88 | + assert.Contains(t, rq.outMgr.(*mockOutboundManager).tags, tag, "tag should still be present after re-adding") |
| 89 | + }, |
| 90 | + }, |
| 91 | + } |
| 92 | + for _, tt := range tests { |
| 93 | + t.Run(tt.name, func(t *testing.T) { |
| 94 | + rq := &removalQueue{ |
| 95 | + logger: logger, |
| 96 | + outMgr: tt.outMgr, |
| 97 | + epMgr: tt.epMgr, |
| 98 | + connMgr: tt.connMgr, |
| 99 | + pending: tt.pending, |
| 100 | + forceAfter: tt.forceAfter, |
| 101 | + } |
| 102 | + rq.checkPending() |
| 103 | + tt.assertFn(t, rq) |
| 104 | + }) |
| 105 | + } |
| 106 | +} |
| 107 | + |
| 108 | +type mockOutboundManager struct { |
| 109 | + adapter.OutboundManager |
| 110 | + tags []string |
| 111 | + mu sync.Mutex |
| 112 | +} |
| 113 | + |
| 114 | +func (m *mockOutboundManager) Remove(tag string) error { |
| 115 | + m.mu.Lock() |
| 116 | + defer m.mu.Unlock() |
| 117 | + if idx := slices.Index(m.tags, tag); idx != -1 { |
| 118 | + m.tags = append(m.tags[:idx], m.tags[idx+1:]...) |
| 119 | + } |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +type mockEndpointManager struct { |
| 124 | + adapter.EndpointManager |
| 125 | + tags []string |
| 126 | + mu sync.Mutex |
| 127 | +} |
| 128 | + |
| 129 | +func (m *mockEndpointManager) Remove(tag string) error { |
| 130 | + m.mu.Lock() |
| 131 | + defer m.mu.Unlock() |
| 132 | + if idx := slices.Index(m.tags, tag); idx != -1 { |
| 133 | + m.tags = append(m.tags[:idx], m.tags[idx+1:]...) |
| 134 | + } |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +type mockConnectionManager struct { |
| 139 | + conns []trafficontrol.TrackerMetadata |
| 140 | +} |
| 141 | + |
| 142 | +func (m *mockConnectionManager) Connections() []trafficontrol.TrackerMetadata { |
| 143 | + if m == nil { |
| 144 | + return nil |
| 145 | + } |
| 146 | + return m.conns |
| 147 | +} |
0 commit comments