Skip to content

Commit 21a9f2e

Browse files
committed
fixup! add monitor test
1 parent ee0d696 commit 21a9f2e

File tree

1 file changed

+26
-18
lines changed

1 file changed

+26
-18
lines changed

internal/monitor/monitor_test.go

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,17 @@ import (
66
"net"
77
"testing"
88

9-
"github.com/arduino/arduino-app-cli/pkg/x/ports"
109
"github.com/stretchr/testify/assert"
10+
11+
"github.com/arduino/arduino-app-cli/pkg/x/ports"
1112
)
1213

1314
func TestMonitorHandler(t *testing.T) {
1415
addr := startEcoMonitor(t)
1516

16-
t.Logf("Started echo monitor at %s", addr.String())
17-
18-
// Use pipes to simulate ReadWriteCloser
19-
rOut, wIn := io.Pipe()
20-
rIn, wOut := io.Pipe()
21-
type pipeReadWriteCloser struct {
22-
io.Reader
23-
io.Writer
24-
io.Closer
25-
}
26-
pr := &pipeReadWriteCloser{
27-
Reader: rOut,
28-
Writer: wOut,
29-
Closer: io.NopCloser(nil),
30-
}
17+
rIn, wIn, rwOut := getReadWriteCloser()
3118

32-
handler, err := NewMonitorHandler(pr, addr.String())
19+
handler, err := NewMonitorHandler(rwOut, addr.String())
3320
assert.NoError(t, err)
3421
go handler()
3522

@@ -47,6 +34,23 @@ func TestMonitorHandler(t *testing.T) {
4734
assert.Equal(t, message, string(buf[:n]))
4835
}
4936

37+
func getReadWriteCloser() (io.Reader, io.Writer, io.ReadWriteCloser) {
38+
rOut, wIn := io.Pipe()
39+
rIn, wOut := io.Pipe()
40+
41+
type pipeReadWriteCloser struct {
42+
io.Reader
43+
io.Writer
44+
io.Closer
45+
}
46+
pr := &pipeReadWriteCloser{
47+
Reader: rOut,
48+
Writer: wOut,
49+
Closer: io.NopCloser(nil),
50+
}
51+
return rIn, wIn, pr
52+
}
53+
5054
func startEcoMonitor(t *testing.T) net.Addr {
5155
t.Helper()
5256

@@ -61,7 +65,11 @@ func startEcoMonitor(t *testing.T) net.Addr {
6165
for {
6266
conn, err := ln.Accept()
6367
assert.NoError(t, err)
64-
go io.Copy(conn, conn) // Echo server
68+
go func() {
69+
defer conn.Close()
70+
_, err = io.Copy(conn, conn) // Echo server
71+
assert.NoError(t, err)
72+
}()
6573
}
6674
}()
6775

0 commit comments

Comments
 (0)