2018-03-30 13:56:59 -04:00
|
|
|
package stats_test
|
|
|
|
|
|
|
|
import (
|
2018-03-30 17:17:28 -04:00
|
|
|
"context"
|
2018-03-30 13:56:59 -04:00
|
|
|
"testing"
|
2020-09-04 03:07:42 -04:00
|
|
|
"time"
|
2018-03-30 13:56:59 -04:00
|
|
|
|
2021-02-16 15:31:50 -05:00
|
|
|
. "github.com/v2fly/v2ray-core/v4/app/stats"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/common"
|
|
|
|
"github.com/v2fly/v2ray-core/v4/features/stats"
|
2018-03-30 13:56:59 -04:00
|
|
|
)
|
|
|
|
|
2020-09-04 03:07:42 -04:00
|
|
|
func TestInterface(t *testing.T) {
|
2019-01-08 17:27:02 -05:00
|
|
|
_ = (stats.Manager)(new(Manager))
|
2018-03-30 13:56:59 -04:00
|
|
|
}
|
2018-03-30 17:17:28 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
func TestStatsChannelRunnable(t *testing.T) {
|
2018-03-30 17:17:28 -04:00
|
|
|
raw, err := common.CreateObject(context.Background(), &Config{})
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
2018-03-30 17:17:28 -04:00
|
|
|
|
2018-10-11 15:14:53 -04:00
|
|
|
m := raw.(stats.Manager)
|
2020-09-15 11:04:26 -04:00
|
|
|
|
|
|
|
ch1, err := m.RegisterChannel("test.channel.1")
|
|
|
|
c1 := ch1.(*Channel)
|
2019-01-08 17:27:02 -05:00
|
|
|
common.Must(err)
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if c1.Running() {
|
|
|
|
t.Fatalf("unexpected running channel: test.channel.%d", 1)
|
2019-01-08 17:27:02 -05:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
common.Must(m.Start())
|
2018-03-30 17:17:28 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if !c1.Running() {
|
|
|
|
t.Fatalf("unexpected non-running channel: test.channel.%d", 1)
|
2019-01-08 17:27:02 -05:00
|
|
|
}
|
2020-09-04 03:07:42 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
ch2, err := m.RegisterChannel("test.channel.2")
|
|
|
|
c2 := ch2.(*Channel)
|
2020-09-04 03:07:42 -04:00
|
|
|
common.Must(err)
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if !c2.Running() {
|
|
|
|
t.Fatalf("unexpected non-running channel: test.channel.%d", 2)
|
2020-09-04 03:07:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-23 10:35:13 -04:00
|
|
|
s1, err := c1.Subscribe()
|
|
|
|
common.Must(err)
|
2020-09-15 11:04:26 -04:00
|
|
|
common.Must(c1.Close())
|
2020-09-04 03:07:42 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if c1.Running() {
|
|
|
|
t.Fatalf("unexpected running channel: test.channel.%d", 1)
|
2020-09-04 03:07:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
select { // Check all subscribers in closed channel are closed
|
|
|
|
case _, ok := <-s1:
|
|
|
|
if ok {
|
|
|
|
t.Fatalf("unexpected non-closed subscriber in channel: test.channel.%d", 1)
|
2020-09-04 03:07:42 -04:00
|
|
|
}
|
2020-09-15 11:04:26 -04:00
|
|
|
case <-time.After(500 * time.Millisecond):
|
|
|
|
t.Fatalf("unexpected non-closed subscriber in channel: test.channel.%d", 1)
|
2020-09-04 03:07:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if len(c1.Subscribers()) != 0 { // Check subscribers in closed channel are emptied
|
|
|
|
t.Fatalf("unexpected non-empty subscribers in channel: test.channel.%d", 1)
|
|
|
|
}
|
2020-09-04 03:07:42 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
common.Must(m.Close())
|
2020-09-04 03:07:42 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if c2.Running() {
|
|
|
|
t.Fatalf("unexpected running channel: test.channel.%d", 2)
|
2020-09-04 03:07:42 -04:00
|
|
|
}
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
ch3, err := m.RegisterChannel("test.channel.3")
|
|
|
|
c3 := ch3.(*Channel)
|
2020-09-04 03:07:42 -04:00
|
|
|
common.Must(err)
|
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if c3.Running() {
|
|
|
|
t.Fatalf("unexpected running channel: test.channel.%d", 3)
|
|
|
|
}
|
2020-09-04 03:07:42 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
common.Must(c3.Start())
|
|
|
|
common.Must(m.UnregisterChannel("test.channel.3"))
|
2020-09-04 03:07:42 -04:00
|
|
|
|
2020-09-15 11:04:26 -04:00
|
|
|
if c3.Running() { // Test that unregistering will close the channel.
|
|
|
|
t.Fatalf("unexpected running channel: test.channel.%d", 3)
|
2020-09-04 03:07:42 -04:00
|
|
|
}
|
|
|
|
}
|