Skip to content

Commit 35638cb

Browse files
committed
Update tests for serverSharing
1 parent 2328ccb commit 35638cb

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

pkg/liveshare/port_forwarder_test.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,13 @@ func TestNewPortForwarder(t *testing.T) {
2828

2929
func TestPortForwarderStart(t *testing.T) {
3030
streamName, streamCondition := "stream-name", "stream-condition"
31+
port := 8000
32+
sendNotification := make(chan PortUpdate)
3133
serverSharing := func(req *jsonrpc2.Request) (interface{}, error) {
34+
sendNotification <- PortUpdate{
35+
Port: int(port),
36+
ChangeKind: PortChangeKindStart,
37+
}
3238
return Port{StreamName: streamName, StreamCondition: streamCondition}, nil
3339
}
3440
getStream := func(req *jsonrpc2.Request) (interface{}, error) {
@@ -55,10 +61,16 @@ func TestPortForwarderStart(t *testing.T) {
5561
ctx, cancel := context.WithCancel(context.Background())
5662
defer cancel()
5763

64+
go func() {
65+
testServer.WriteToObjectStream(rpcPortTestMessage{
66+
Method: "serverSharing.sharingSucceeded",
67+
Params: <-sendNotification,
68+
})
69+
}()
70+
5871
done := make(chan error)
5972
go func() {
60-
const name, remote = "ssh", 8000
61-
done <- NewPortForwarder(session, name, remote, false).ForwardToListener(ctx, listen)
73+
done <- NewPortForwarder(session, "ssh", port, false).ForwardToListener(ctx, listen)
6274
}()
6375

6476
go func() {

pkg/liveshare/ports.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ func (s *Session) WaitForPortNotification(ctx context.Context, port int, notifTy
8585
notificationUpdate := make(chan PortNotification, 1)
8686
errc := make(chan error, 1)
8787

88-
h := func(success bool) func(*jsonrpc2.Request) {
89-
return func(req *jsonrpc2.Request) {
88+
h := func(success bool) func(*jsonrpc2.Conn, *jsonrpc2.Request) {
89+
return func(conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
9090
var notification PortNotification
9191
if err := json.Unmarshal(*req.Params, &notification); err != nil {
9292
errc <- fmt.Errorf("error unmarshaling notification: %w", err)

pkg/liveshare/rpc.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (r *rpcClient) do(ctx context.Context, method string, args, result interfac
4242
return waiter.Wait(waitCtx, result)
4343
}
4444

45-
type handlerFn func(req *jsonrpc2.Request)
45+
type handlerFn func(conn *jsonrpc2.Conn, req *jsonrpc2.Request)
4646

4747
type requestHandler struct {
4848
handlersMu sync.RWMutex
@@ -73,6 +73,6 @@ func (r *requestHandler) handlerFn(requestType string) []handlerFn {
7373

7474
func (r *requestHandler) Handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) {
7575
for _, handler := range r.handlerFn(req.Method) {
76-
go handler(req)
76+
go handler(conn, req)
7777
}
7878
}

pkg/liveshare/session_test.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,14 @@ func makeMockSession(opts ...livesharetest.ServerOption) (*livesharetest.Server,
4949
return testServer, session, nil
5050
}
5151

52+
type rpcPortTestMessage struct {
53+
Method string
54+
Params PortUpdate
55+
}
56+
5257
func TestServerStartSharing(t *testing.T) {
5358
serverPort, serverProtocol := 2222, "sshd"
59+
sendNotification := make(chan PortUpdate)
5460
startSharing := func(req *jsonrpc2.Request) (interface{}, error) {
5561
var args []interface{}
5662
if err := json.Unmarshal(*req.Params, &args); err != nil {
@@ -59,9 +65,11 @@ func TestServerStartSharing(t *testing.T) {
5965
if len(args) < 3 {
6066
return nil, errors.New("not enough arguments to start sharing")
6167
}
62-
if port, ok := args[0].(float64); !ok {
68+
port, ok := args[0].(float64)
69+
if !ok {
6370
return nil, errors.New("port argument is not an int")
64-
} else if port != float64(serverPort) {
71+
}
72+
if port != float64(serverPort) {
6573
return nil, errors.New("port does not match serverPort")
6674
}
6775
if protocol, ok := args[1].(string); !ok {
@@ -74,6 +82,10 @@ func TestServerStartSharing(t *testing.T) {
7482
} else if browseURL != fmt.Sprintf("http://localhost:%d", serverPort) {
7583
return nil, errors.New("browseURL does not match expected")
7684
}
85+
sendNotification <- PortUpdate{
86+
Port: int(port),
87+
ChangeKind: PortChangeKindStart,
88+
}
7789
return Port{StreamName: "stream-name", StreamCondition: "stream-condition"}, nil
7890
}
7991
testServer, session, err := makeMockSession(
@@ -86,6 +98,13 @@ func TestServerStartSharing(t *testing.T) {
8698
}
8799
ctx := context.Background()
88100

101+
go func() {
102+
testServer.WriteToObjectStream(rpcPortTestMessage{
103+
Method: "serverSharing.sharingSucceeded",
104+
Params: <-sendNotification,
105+
})
106+
}()
107+
89108
done := make(chan error)
90109
go func() {
91110
streamID, err := session.startSharing(ctx, serverProtocol, serverPort)

0 commit comments

Comments
 (0)