Unverified Commit 07573124 authored by Joway's avatar Joway Committed by GitHub
Browse files

fix: check is active when flush (#118)

Showing with 33 additions and 2 deletions
+33 -2
......@@ -205,7 +205,7 @@ func (c *connection) MallocLen() (length int) {
// If empty, it will call syscall.Write to send data directly,
// otherwise the buffer will be sent asynchronously by the epoll trigger.
func (c *connection) Flush() error {
if !c.lock(flushing) {
if !c.IsActive() || !c.lock(flushing) {
return Exception(ErrConnClosed, "when flush")
}
defer c.unlock(flushing)
......@@ -267,7 +267,7 @@ func (c *connection) Read(p []byte) (n int, err error) {
// Write will Flush soon.
func (c *connection) Write(p []byte) (n int, err error) {
if !c.lock(flushing) {
if !c.IsActive() || !c.lock(flushing) {
return 0, Exception(ErrConnClosed, "when write")
}
defer c.unlock(flushing)
......
......@@ -245,6 +245,37 @@ func TestCloseCallbackWhenOnConnect(t *testing.T) {
MustNil(t, err)
}
func TestCloseAndWrite(t *testing.T) {
var network, address = "tcp", ":18888"
var sendMsg = []byte("hello")
var loop = newTestEventLoop(network, address,
func(ctx context.Context, connection Connection) error {
_, err := connection.Reader().Next(len(sendMsg))
MustNil(t, err)
err = connection.Close()
MustNil(t, err)
return nil
},
)
var conn, err = DialConnection(network, address, time.Second)
MustNil(t, err)
_, err = conn.Writer().WriteBinary(sendMsg)
MustNil(t, err)
err = conn.Writer().Flush()
MustNil(t, err)
time.Sleep(time.Millisecond * 100) // wait for poller close connection
_, err = conn.Writer().WriteBinary(sendMsg)
MustNil(t, err)
err = conn.Writer().Flush()
MustTrue(t, errors.Is(err, ErrConnClosed))
err = loop.Shutdown(context.Background())
MustNil(t, err)
}
func newTestEventLoop(network, address string, onRequest OnRequest, opts ...Option) EventLoop {
var listener, _ = CreateListener(network, address)
var eventLoop, _ = NewEventLoop(onRequest, opts...)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment