mirror of
https://github.com/ehang-io/nps.git
synced 2025-07-03 04:53:50 +00:00
test for user close problem
This commit is contained in:
parent
9d3df6be7e
commit
1c1be202b7
@ -52,6 +52,7 @@ func NewConn(connId int32, mux *Mux) *conn {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *conn) Read(buf []byte) (n int, err error) {
|
func (s *conn) Read(buf []byte) (n int, err error) {
|
||||||
|
logs.Warn("starting conn read", s.connId)
|
||||||
if s.isClose || buf == nil {
|
if s.isClose || buf == nil {
|
||||||
return 0, errors.New("the conn has closed")
|
return 0, errors.New("the conn has closed")
|
||||||
}
|
}
|
||||||
@ -315,7 +316,7 @@ func (Self *window) allowRead() (closed bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (Self *window) Read(p []byte) (n int, err error) {
|
func (Self *window) Read(p []byte) (n int, err error) {
|
||||||
//logs.Warn("starting window read method len ", Self.len())
|
logs.Warn("starting window read method len ", Self.len())
|
||||||
if Self.closeOp {
|
if Self.closeOp {
|
||||||
return 0, io.EOF // Write method receive close signal, returns eof
|
return 0, io.EOF // Write method receive close signal, returns eof
|
||||||
}
|
}
|
||||||
|
@ -179,12 +179,14 @@ func (s *Mux) readSession() {
|
|||||||
switch pack.Flag {
|
switch pack.Flag {
|
||||||
case common.MUX_NEW_CONN: //new connection
|
case common.MUX_NEW_CONN: //new connection
|
||||||
logs.Warn("rec mux new connection", pack.Id)
|
logs.Warn("rec mux new connection", pack.Id)
|
||||||
conn := NewConn(pack.Id, s)
|
connection := NewConn(pack.Id, s)
|
||||||
s.connMap.Set(pack.Id, conn) //it has been set before send ok
|
s.connMap.Set(pack.Id, connection) //it has been set before send ok
|
||||||
s.newConnCh <- conn
|
go func(connection *conn) {
|
||||||
go conn.sendWindow.SetAllowSize(512) // set the initial receive window
|
connection.sendWindow.SetAllowSize(512) // set the initial receive window
|
||||||
s.sendInfo(common.MUX_NEW_CONN_OK, pack.Id, nil)
|
}(connection)
|
||||||
logs.Warn("send mux new connection ok", pack.Id)
|
s.newConnCh <- connection
|
||||||
|
s.sendInfo(common.MUX_NEW_CONN_OK, connection.connId, nil)
|
||||||
|
logs.Warn("send mux new connection ok", connection.connId)
|
||||||
continue
|
continue
|
||||||
case common.MUX_PING_FLAG: //ping
|
case common.MUX_PING_FLAG: //ping
|
||||||
//logs.Warn("send mux ping return")
|
//logs.Warn("send mux ping return")
|
||||||
@ -202,7 +204,7 @@ func (s *Mux) readSession() {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
connection.receiveWindow.WriteWg.Add(1)
|
connection.receiveWindow.WriteWg.Add(1)
|
||||||
logs.Warn("rec mux new msg ", pack.Id, string(pack.Content[0:15]))
|
logs.Warn("rec mux new msg ", connection.connId, string(pack.Content[0:15]))
|
||||||
go func(connection *conn, content []byte) { // do not block read session
|
go func(connection *conn, content []byte) { // do not block read session
|
||||||
_, err := connection.receiveWindow.Write(content)
|
_, err := connection.receiveWindow.Write(content)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -214,7 +216,7 @@ func (s *Mux) readSession() {
|
|||||||
connection.receiveWindow.WindowFull = true
|
connection.receiveWindow.WindowFull = true
|
||||||
}
|
}
|
||||||
s.sendInfo(common.MUX_MSG_SEND_OK, connection.connId, size)
|
s.sendInfo(common.MUX_MSG_SEND_OK, connection.connId, size)
|
||||||
logs.Warn("send mux new msg ok", pack.Id, size)
|
logs.Warn("send mux new msg ok", connection.connId, size)
|
||||||
connection.receiveWindow.WriteWg.Done()
|
connection.receiveWindow.WriteWg.Done()
|
||||||
}(connection, pack.Content)
|
}(connection, pack.Content)
|
||||||
continue
|
continue
|
||||||
@ -241,7 +243,9 @@ func (s *Mux) readSession() {
|
|||||||
s.connMap.Delete(pack.Id)
|
s.connMap.Delete(pack.Id)
|
||||||
connection.closeFlag = true
|
connection.closeFlag = true
|
||||||
go func(connection *conn) {
|
go func(connection *conn) {
|
||||||
|
logs.Warn("receive mux connection close, wg waiting", connection.connId)
|
||||||
connection.receiveWindow.WriteWg.Wait()
|
connection.receiveWindow.WriteWg.Wait()
|
||||||
|
logs.Warn("receive mux connection close, wg waited", connection.connId)
|
||||||
connection.receiveWindow.WriteEndOp <- struct{}{} // close signal to receive window
|
connection.receiveWindow.WriteEndOp <- struct{}{} // close signal to receive window
|
||||||
logs.Warn("receive mux connection close, finish", connection.connId)
|
logs.Warn("receive mux connection close, finish", connection.connId)
|
||||||
}(connection)
|
}(connection)
|
||||||
|
@ -6,6 +6,7 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/http"
|
"net/http"
|
||||||
_ "net/http/pprof"
|
_ "net/http/pprof"
|
||||||
|
"sync"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@ -37,20 +38,29 @@ func TestNewMux(t *testing.T) {
|
|||||||
logs.Warn(err)
|
logs.Warn(err)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
wg := sync.WaitGroup{}
|
||||||
|
wg.Add(1)
|
||||||
go func() {
|
go func() {
|
||||||
_, err = common.CopyBuffer(c2, c)
|
_, err = common.CopyBuffer(c2, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Warn("close npc by copy from nps", err)
|
|
||||||
c2.Close()
|
c2.Close()
|
||||||
c.Close()
|
c.Close()
|
||||||
|
logs.Warn("close npc by copy from nps", err)
|
||||||
}
|
}
|
||||||
|
wg.Done()
|
||||||
}()
|
}()
|
||||||
_, err = common.CopyBuffer(c, c2)
|
wg.Add(1)
|
||||||
if err != nil {
|
go func() {
|
||||||
logs.Warn("close npc by copy from server", err)
|
_, err = common.CopyBuffer(c, c2)
|
||||||
c2.Close()
|
if err != nil {
|
||||||
c.Close()
|
c2.Close()
|
||||||
}
|
c.Close()
|
||||||
|
logs.Warn("close npc by copy from server", err)
|
||||||
|
}
|
||||||
|
wg.Done()
|
||||||
|
}()
|
||||||
|
logs.Warn("npc wait")
|
||||||
|
wg.Wait()
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
@ -77,17 +87,17 @@ func TestNewMux(t *testing.T) {
|
|||||||
go func() {
|
go func() {
|
||||||
_, err := common.CopyBuffer(tmpCpnn, conn)
|
_, err := common.CopyBuffer(tmpCpnn, conn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Warn("close nps by copy from user", tmpCpnn.connId)
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
tmpCpnn.Close()
|
tmpCpnn.Close()
|
||||||
|
logs.Warn("close nps by copy from user", tmpCpnn.connId)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
//time.Sleep(time.Second)
|
//time.Sleep(time.Second)
|
||||||
_, err = common.CopyBuffer(conn, tmpCpnn)
|
_, err = common.CopyBuffer(conn, tmpCpnn)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logs.Warn("close nps by copy from npc ", tmpCpnn.connId)
|
|
||||||
conn.Close()
|
conn.Close()
|
||||||
tmpCpnn.Close()
|
tmpCpnn.Close()
|
||||||
|
logs.Warn("close nps by copy from npc ", tmpCpnn.connId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user