diff --git a/melody.go b/melody.go index 7d76403..d355c9e 100644 --- a/melody.go +++ b/melody.go @@ -91,13 +91,14 @@ type Melody struct { type DebugInfo struct { UserCountMutex sync.Mutex UserCount int + Tag string } // EnableDebug - var EnableDebug = false -// New creates a new melody instance with default Upgrader and Config. -func New() *Melody { +// NewWithTag creates a new melody instance with default Upgrader, Config and identity. +func NewWithTag(tag string) *Melody { upgrader := &websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 1024, @@ -125,10 +126,15 @@ func New() *Melody { pingHandler: func(*Session) {}, pongHandler: func(*Session) {}, hub: hub, - debugInfo: DebugInfo{}, + debugInfo: DebugInfo{Tag: tag}, } } +// New creates a new melody instance with default Upgrader and Config. +func New() *Melody { + return NewWithTag("") +} + // HandleConnect fires fn when a session connects. func (m *Melody) HandleConnect(fn func(*Session)) { m.connectHandler = fn diff --git a/melody_test.go b/melody_test.go index b6828c1..876b636 100644 --- a/melody_test.go +++ b/melody_test.go @@ -126,53 +126,62 @@ func TestWriteClosed(t *testing.T) { } func TestLen(t *testing.T) { - log.Println("TestLen") - rand.Seed(time.Now().UnixNano()) - - connect := int(rand.Int31n(100)) - disconnect := rand.Float32() - conns := make([]*websocket.Conn, connect) - defer func() { - for _, conn := range conns { - if conn != nil { - conn.Close() + for count := 0; count < 10; count++ { + t.Run("TestLen", func(t *testing.T) { + t.Parallel() + rand.Seed(time.Now().UnixNano()) + + connect := int(rand.Int31n(100)) + disconnect := rand.Float32() + conns := make([]*websocket.Conn, connect) + + m := NewWithTag(t.Name()) + echo := &TestServer{ + m: m, } - } - }() - echo := NewTestServerHandler(func(session *Session, msg []byte) {}) - server := httptest.NewServer(echo) - defer server.Close() + server := httptest.NewServer(echo) + defer server.Close() - disconnected := 0 - for i := 0; i < connect; i++ { - conn, err := NewDialer(server.URL) + disconnected := 0 + for i := 0; i < connect; i++ { + conn, err := NewDialer(server.URL) - if err != nil { - t.Error(err) - } + if err != nil { + t.Error(err) + } - if rand.Float32() < disconnect { - conns[i] = nil - disconnected++ - conn.Close() - continue - } + if rand.Float32() < disconnect { + conns[i] = nil + disconnected++ + conn.Close() + continue + } - conns[i] = conn + conns[i] = conn + } + block := make(chan int) + go func() { + time.Sleep(1 * time.Second) + + connected := connect - disconnected + + if echo.m.Len() != connected { + t.Errorf("melody len %d should equal %d", echo.m.Len(), connected) + } + block <- 0 + }() + <-block + + func() { + for _, conn := range conns { + if conn != nil { + conn.Close() + } + } + }() + }) } - block := make(chan int) - go func() { - time.Sleep(time.Millisecond) - - connected := connect - disconnected - - if echo.m.Len() != connected { - t.Errorf("melody len %d should equal %d", echo.m.Len(), connected) - } - block <- 0 - }() - <-block } func TestEchoBinary(t *testing.T) {