Skip to content

Commit

Permalink
fix: exit goroutine on close in ouroboros_mock (#534)
Browse files Browse the repository at this point in the history
Fixes #533
  • Loading branch information
agaffney authored Mar 11, 2024
1 parent 7fbcb28 commit 3a50e03
Showing 1 changed file with 23 additions and 8 deletions.
31 changes: 23 additions & 8 deletions internal/test/ouroboros_mock/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"fmt"
"net"
"reflect"
"sync"
"time"

"github.com/blinklabs-io/gouroboros/cbor"
Expand All @@ -42,6 +43,8 @@ type Connection struct {
conversation []ConversationEntry
muxer *muxer.Muxer
muxerRecvChan chan *muxer.Segment
doneChan chan any
onceClose sync.Once
}

// NewConnection returns a new Connection with the provided conversation entries
Expand All @@ -51,6 +54,7 @@ func NewConnection(
) net.Conn {
c := &Connection{
conversation: conversation,
doneChan: make(chan any),
}
c.conn, c.mockConn = net.Pipe()
// Start a muxer on the mocked side of the connection
Expand Down Expand Up @@ -91,14 +95,20 @@ func (c *Connection) Write(b []byte) (n int, err error) {

// Close closes both sides of the connection. This is needed to satisfy the net.Conn interface
func (c *Connection) Close() error {
c.muxer.Stop()
if err := c.conn.Close(); err != nil {
return err
}
if err := c.mockConn.Close(); err != nil {
return err
}
return nil
var retErr error
c.onceClose.Do(func() {
close(c.doneChan)
c.muxer.Stop()
if err := c.conn.Close(); err != nil {
retErr = err
return
}
if err := c.mockConn.Close(); err != nil {
retErr = err
return
}
})
return retErr
}

// LocalAddr provides a proxy to the client-side connection's LocalAddr function. This is needed to satisfy the net.Conn interface
Expand Down Expand Up @@ -128,6 +138,11 @@ func (c *Connection) SetWriteDeadline(t time.Time) error {

func (c *Connection) asyncLoop() {
for _, entry := range c.conversation {
select {
case <-c.doneChan:
return
default:
}
switch entry.Type {
case EntryTypeInput:
if err := c.processInputEntry(entry); err != nil {
Expand Down

0 comments on commit 3a50e03

Please sign in to comment.