cli/rpc_test.go
2021-07-06 09:12:43 -04:00

27 lines
649 B
Go

package liveshare
import (
"context"
"testing"
"time"
"github.com/sourcegraph/jsonrpc2"
)
func TestRPCHandlerEvents(t *testing.T) {
rpcHandler := newRPCHandler()
eventCh := rpcHandler.registerEventHandler("somethingHappened")
go func() {
time.Sleep(1 * time.Second)
rpcHandler.Handle(context.Background(), nil, &jsonrpc2.Request{Method: "somethingHappened"})
}()
ctx, _ := context.WithDeadline(context.Background(), time.Now().Add(5*time.Second))
select {
case event := <-eventCh:
if event.Method != "somethingHappened" {
t.Error("event.Method is not the expect value")
}
case <-ctx.Done():
t.Error("Test time out")
}
}