Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for '$/cancelRequest' method #514

Merged
merged 1 commit into from
May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions internal/langserver/handlers/cancel_request_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package handlers

import (
"context"
"fmt"
"log"
"sync"
"testing"
"time"

"github.com/creachadair/jrpc2"
"github.com/creachadair/jrpc2/handler"
"github.com/hashicorp/terraform-ls/internal/langserver"
)

func TestLangServer_cancelRequest(t *testing.T) {
tmpDir := TempDir(t)

ls := langserver.NewLangServerMock(t, NewMockSession(&MockSessionInput{
AdditionalHandlers: map[string]handler.Func{
"$/sleepTicker": func(ctx context.Context, req *jrpc2.Request) (interface{}, error) {
ticker := time.NewTicker(100 * time.Millisecond)

ctx, cancelFunc := context.WithTimeout(ctx, 1*time.Second)
t.Cleanup(cancelFunc)

var wg sync.WaitGroup
wg.Add(1)
go func(ctx context.Context) {
defer wg.Done()
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
log.Printf("tick at %s", time.Now())
}
}
}(ctx)
wg.Wait()

return nil, ctx.Err()
},
},
}))
stop := ls.Start(t)
defer stop()

ls.Call(t, &langserver.CallRequest{
Method: "initialize",
ReqParams: fmt.Sprintf(`{
"capabilities": {},
"rootUri": %q,
"processId": 12345
}`, tmpDir.URI())})
ls.Notify(t, &langserver.CallRequest{
Method: "initialized",
ReqParams: "{}",
})

var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
ls.CallAndExpectError(t, &langserver.CallRequest{
Method: "$/sleepTicker",
ReqParams: `{}`,
}, context.Canceled)
}()
time.Sleep(100 * time.Millisecond)
ls.Call(t, &langserver.CallRequest{
Method: "$/cancelRequest",
ReqParams: fmt.Sprintf(`{"id": %d}`, 2),
})
wg.Wait()
}
9 changes: 9 additions & 0 deletions internal/langserver/handlers/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ type service struct {
newWalker module.WalkerFactory
tfDiscoFunc discovery.DiscoveryFunc
tfExecFactory exec.ExecutorFactory

additionalHandlers map[string]rpch.Func
}

var discardLogs = log.New(ioutil.Discard, "", 0)
Expand Down Expand Up @@ -344,6 +346,13 @@ func (svc *service) Assigner() (jrpc2.Assigner, error) {
},
}

// For use in tests, e.g. to test request cancellation
if len(svc.additionalHandlers) > 0 {
for methodName, handlerFunc := range svc.additionalHandlers {
m[methodName] = handlerFunc
}
}

return convertMap(m), nil
}

Expand Down
38 changes: 22 additions & 16 deletions internal/langserver/handlers/session_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sync"
"testing"

"github.com/creachadair/jrpc2/handler"
"github.com/hashicorp/terraform-ls/internal/filesystem"
"github.com/hashicorp/terraform-ls/internal/langserver/session"
"github.com/hashicorp/terraform-ls/internal/terraform/discovery"
Expand All @@ -16,8 +17,9 @@ import (
)

type MockSessionInput struct {
Filesystem filesystem.Filesystem
TerraformCalls *exec.TerraformMockCalls
Filesystem filesystem.Filesystem
TerraformCalls *exec.TerraformMockCalls
AdditionalHandlers map[string]handler.Func
}

type mockSession struct {
Expand All @@ -40,10 +42,13 @@ func (ms *mockSession) new(srvCtx context.Context) session.Session {
}

var fs filesystem.Filesystem
if ms.mockInput != nil && ms.mockInput.Filesystem != nil {
fs = ms.mockInput.Filesystem
} else {
fs = filesystem.NewFilesystem()
fs = filesystem.NewFilesystem()
var handlers map[string]handler.Func
if ms.mockInput != nil {
if ms.mockInput.Filesystem != nil {
fs = ms.mockInput.Filesystem
}
handlers = ms.mockInput.AdditionalHandlers
}

var tfCalls *exec.TerraformMockCalls
Expand All @@ -56,16 +61,17 @@ func (ms *mockSession) new(srvCtx context.Context) session.Session {
}

svc := &service{
logger: testLogger(),
srvCtx: srvCtx,
sessCtx: sessCtx,
stopSession: ms.stop,
fs: fs,
newModuleManager: module.NewModuleManagerMock(input),
newWatcher: module.MockWatcher(),
newWalker: module.SyncWalker,
tfDiscoFunc: d.LookPath,
tfExecFactory: exec.NewMockExecutor(tfCalls),
logger: testLogger(),
srvCtx: srvCtx,
sessCtx: sessCtx,
stopSession: ms.stop,
fs: fs,
newModuleManager: module.NewModuleManagerMock(input),
newWatcher: module.MockWatcher(),
newWalker: module.SyncWalker,
tfDiscoFunc: d.LookPath,
tfExecFactory: exec.NewMockExecutor(tfCalls),
additionalHandlers: handlers,
}

return svc
Expand Down