Skip to content

Commit

Permalink
fix: move deadlines to constructor
Browse files Browse the repository at this point in the history
chore: treat deadline errors

chore: add comment for timeout
  • Loading branch information
Andrei Sousa authored and sousandrei committed May 6, 2022
1 parent b0f5896 commit 9d2e9c0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 19 deletions.
12 changes: 4 additions & 8 deletions client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,15 @@ func TestUDPEmulator(t *testing.T) {
addrEmulator := "127.0.0.1:24001"
addrClient := "127.0.0.1:24002"

connEmulator, err := xsensemulator.NewUDPSerialPort(addrEmulator, addrClient)
timeout := 100 * time.Millisecond

connEmulator, err := xsensemulator.NewUDPSerialPort(addrEmulator, addrClient, timeout)
assert.NilError(t, err)
defer func() {
assert.NilError(t, connEmulator.Close())
}()

connClient, err := xsensemulator.NewUDPSerialPort(addrClient, addrEmulator)
connClient, err := xsensemulator.NewUDPSerialPort(addrClient, addrEmulator, timeout)
assert.NilError(t, err)

emu := xsensemulator.NewEmulator(connEmulator)
Expand All @@ -294,9 +296,6 @@ func TestUDPEmulator(t *testing.T) {
var g errgroup.Group

g.Go(func() error {
assert.NilError(t, connEmulator.SetReadDeadline(deadline))
assert.NilError(t, connEmulator.SetWriteDeadline(deadline))

err := emu.Receive(ctx)
if !strings.Contains(err.Error(), "timeout") {
return err
Expand All @@ -305,9 +304,6 @@ func TestUDPEmulator(t *testing.T) {
})

g.Go(func() error {
assert.NilError(t, connClient.SetReadDeadline(deadline))
assert.NilError(t, connClient.SetWriteDeadline(deadline))

client := xsens.NewClient(connClient)

assert.NilError(t, client.SetOutputConfiguration(ctx, outputConf))
Expand Down
25 changes: 14 additions & 11 deletions xsensemulator/emulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ var (

type UDPSerialPort struct {
io.ReadWriteCloser
// Timeout for setting read/write deadlines
timeout time.Duration
OriginConn *net.UDPConn
DestinationAddr *net.UDPAddr
}

func NewUDPSerialPort(origin, destination string) (*UDPSerialPort, error) {
func NewUDPSerialPort(origin, destination string, timeout time.Duration) (*UDPSerialPort, error) {
udpOriginAddr, err := net.ResolveUDPAddr("udp", origin)
if err != nil {
return nil, fmt.Errorf("new udp serial port: %w", err)
Expand All @@ -40,30 +42,31 @@ func NewUDPSerialPort(origin, destination string) (*UDPSerialPort, error) {
return &UDPSerialPort{
OriginConn: originConn,
DestinationAddr: udpDestinationAddr,
timeout: timeout,
}, nil
}

func (t UDPSerialPort) Read(p []byte) (int, error) {
n, _, err := t.OriginConn.ReadFromUDP(p)
func (t UDPSerialPort) Read(p []byte) (n int, err error) {
err = t.OriginConn.SetReadDeadline(time.Now().Add(t.timeout))
if err != nil {
return 0, fmt.Errorf("udp serial port read: %w", err)
}
n, _, err = t.OriginConn.ReadFromUDP(p)
return n, err
}

func (t UDPSerialPort) Write(p []byte) (n int, err error) {
err = t.OriginConn.SetWriteDeadline(time.Now().Add(t.timeout))
if err != nil {
return 0, fmt.Errorf("udp serial port write: %w", err)
}
return t.OriginConn.WriteToUDP(p, t.DestinationAddr)
}

func (t UDPSerialPort) Close() error {
return t.OriginConn.Close()
}

func (t UDPSerialPort) SetReadDeadline(t2 time.Time) error {
return t.OriginConn.SetReadDeadline(t2)
}

func (t UDPSerialPort) SetWriteDeadline(t2 time.Time) error {
return t.OriginConn.SetWriteDeadline(t2)
}

type Emulator struct {
port io.ReadWriteCloser
w *bufio.Writer
Expand Down

0 comments on commit 9d2e9c0

Please sign in to comment.