Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
blesswinsamuel committed Nov 13, 2024
1 parent 1cc2636 commit 0cf339d
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 8 deletions.
44 changes: 41 additions & 3 deletions pkg/listen/listen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,23 @@ var (
upgrader = websocket.Upgrader{}
)

func wsHandler(t *testing.T) http.HandlerFunc {
func wsHandler(t *testing.T, recvBuffer *bytes.Buffer) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
c, err := upgrader.Upgrade(w, r, nil)
require.NoError(t, err)
defer c.Close()
i := 0
finish := 5
go func() {
// Read messages from websocket and write to buffer
for {
_, message, err := c.ReadMessage()
if err != nil {
return
}
recvBuffer.Write(message)
}
}()
for {
// Give the Close test a chance to close before any sent
time.Sleep(time.Millisecond * 10)
Expand Down Expand Up @@ -55,7 +65,7 @@ func wsHandler(t *testing.T) http.HandlerFunc {
}

func TestListener(t *testing.T) {
server := httptest.NewServer(wsHandler(t))
server := httptest.NewServer(wsHandler(t, nil))
defer server.Close()

u := "ws" + strings.TrimPrefix(server.URL, "http")
Expand All @@ -78,7 +88,7 @@ func TestListener(t *testing.T) {
}

func TestListenerWithSchemaFunc(t *testing.T) {
server := httptest.NewServer(wsHandler(t))
server := httptest.NewServer(wsHandler(t, nil))
defer server.Close()

u := "ws" + strings.TrimPrefix(server.URL, "http")
Expand Down Expand Up @@ -112,3 +122,31 @@ func TestListenerWithSchemaFunc(t *testing.T) {
`
require.Equal(t, want, buffer.String())
}

func TestListenerWithInput(t *testing.T) {
wsInBuf := &bytes.Buffer{}
server := httptest.NewServer(wsHandler(t, wsInBuf))
defer server.Close()

u := "ws" + strings.TrimPrefix(server.URL, "http")
url, err := url.Parse(u)
require.NoError(t, err)

inputCh := make(chan []byte, 5)
for i := 0; i < 5; i++ {
inputCh <- []byte{byte('a' + i)}
}
wsOutBuf := &bytes.Buffer{}
listener := NewListener(url, "", nil, wsOutBuf, inputCh)
err = listener.Listen(context.Background())
require.NoError(t, err)

want := `{"message":"1\n"}
{"message":"2\n"}
{"message":"3\n"}
{"message":"4\n"}
{"message":"5\n"}
`
require.Equal(t, want, wsOutBuf.String())
require.Equal(t, "abcde", wsInBuf.String())
}
6 changes: 1 addition & 5 deletions pkg/listen/listen_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,7 @@ func (l *Listener) MonitorResizeEvents(ctx context.Context, resizeEvents chan<-
terminalSize := TerminalSize{Width: width, Height: height}
if terminalSize != prevTerminalSize {
prevTerminalSize = terminalSize
select {
case resizeEvents <- terminalSize:
case <-ctx.Done():
return nil
}
resizeEvents <- terminalSize
}

// sleep to avoid hot looping
Expand Down

0 comments on commit 0cf339d

Please sign in to comment.