-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b49ae2a
commit 0aa7c0f
Showing
3 changed files
with
98 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/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(10 * 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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters