Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiplex: add (*Multiplex).CloseChan #89

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions multiplex.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,11 @@ func (mp *Multiplex) IsClosed() bool {
}
}

// CloseChan returns a read-only channel which will be closed when the session is closed
func (mp *Multiplex) CloseChan() <-chan struct{} {
return mp.closed
}

func (mp *Multiplex) sendMsg(timeout, cancel <-chan struct{}, header uint64, data []byte) error {
buf := pool.Get(len(data) + 20)

Expand Down
36 changes: 36 additions & 0 deletions multiplex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,42 @@ func TestClosing(t *testing.T) {
}
}

func TestCloseChan(t *testing.T) {
a, b := net.Pipe()

mpa := NewMultiplex(a, false)
mpb := NewMultiplex(b, true)

ctx, cancel := context.WithTimeout(context.Background(), time.Second*1)
defer cancel()

_, err := mpb.NewStream(ctx)
if err != nil {
t.Fatal(err)
}

_, err = mpa.Accept()
if err != nil {
t.Fatal(err)
}

go func() {
mpa.Close()
}()

select {
case <-ctx.Done():
t.Fatal("did not receive from CloseChan for mpa within timeout")
case <-mpa.CloseChan():
}

select {
case <-ctx.Done():
t.Fatal("did not receive from CloseChan for mpb within timeout")
case <-mpb.CloseChan():
}
}

func TestReset(t *testing.T) {
a, b := net.Pipe()

Expand Down