Skip to content

Commit

Permalink
Fix wrong logic in TestLen testcase - Close all connection too early
Browse files Browse the repository at this point in the history
  • Loading branch information
steven.chiu committed Mar 19, 2019
1 parent 05f754d commit 8537015
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 43 deletions.
12 changes: 9 additions & 3 deletions melody.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
89 changes: 49 additions & 40 deletions melody_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8537015

Please sign in to comment.