Skip to content

Commit

Permalink
Fix #13 read not return (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangxu19830126 authored Sep 23, 2021
1 parent 565a457 commit 6cf310b
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
12 changes: 11 additions & 1 deletion application.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,19 @@ func (s *server) Stop() error {
return fmt.Errorf("error state %d", current)
}

atomic.StoreInt32(&s.state, stateStopped)
s.listener.Close()
close(s.startCh)
atomic.StoreInt32(&s.state, stateStopped)
go func() {
for _, m := range s.sessions {
m.Lock()
for k, rs := range m.sessions {
delete(m.sessions, k)
rs.Close()
}
m.Unlock()
}
}()
return nil
}

Expand Down
29 changes: 29 additions & 0 deletions application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,35 @@ func TestCloseBlock(t *testing.T) {
assert.NoError(t, conn.Close())
}

func TestIssue13(t *testing.T) {
app := newTestTCPApp(t, nil).(*server)
assert.NoError(t, app.Start())

conn := newTestIOSession(t, WithEnableAsyncWrite(16), WithLogger(zap.NewExample()))
ok, err := conn.Connect(testAddr, time.Second)
assert.NoError(t, err)
assert.True(t, ok)

errC := make(chan error)
go func() {
_, err := conn.Read()
if err != nil {
errC <- err
return
}
}()

time.Sleep(time.Millisecond * 100)
assert.NoError(t, app.Stop())

select {
case <-errC:
return
case <-time.After(time.Second * 1):
assert.Fail(t, "timeout")
}
}

func newTestTCPApp(t *testing.T, handleFunc func(IOSession, interface{}, uint64) error, opts ...AppOption) NetApplication {
encoder, decoder := simple.NewStringCodec()
opts = append(opts, WithAppSessionOptions(WithCodec(encoder, decoder)))
Expand Down

0 comments on commit 6cf310b

Please sign in to comment.