Skip to content

Commit

Permalink
Error on invalid capability acquire/release request (#8133)
Browse files Browse the repository at this point in the history
`capability/acquire` with an invalid method `executionContext/canModify` would previously timeout because the capability router simply didn't support that kind of capability request.
Now rather than timing out, indicating a failure on LS, we report an error.

Closes #8038.
  • Loading branch information
hubertp authored Oct 24, 2023
1 parent c1259cb commit 61a0c8c
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import akka.actor.{Actor, ActorRef, Props}
import com.typesafe.scalalogging.LazyLogging
import org.enso.languageserver.capability.CapabilityProtocol.{
AcquireCapability,
CapabilityAcquisitionBadRequest,
CapabilityReleaseBadRequest,
ReleaseCapability
}
import org.enso.languageserver.data.{
Expand Down Expand Up @@ -65,6 +67,10 @@ class CapabilityRouter(
CapabilityRegistration(ReceivesSuggestionsDatabaseUpdates())
) =>
suggestionsHandler.forward(msg)
case AcquireCapability(_, _) =>
sender() ! CapabilityAcquisitionBadRequest
case ReleaseCapability(_, _) =>
sender() ! CapabilityReleaseBadRequest
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.enso.languageserver.websocket.json

import io.circe.literal._
import io.circe.parser.parse
import io.circe.syntax.EncoderOps
import org.enso.polyglot.runtime.Runtime.Api

import java.util.UUID

class CapabilitiesTest extends BaseServerTest {

"capability/acquire" must {

"return an error response to the client when requesting it for `executionContext/canModify`" in {
val client = getInitialisedWsClient()
val contextId = createExecutionContext(client)
client.send(json"""
{ "jsonrpc": "2.0",
"method": "capability/acquire",
"id": 1,
"params": {
"method" : "executionContext/canModify",
"registerOptions": {
"contextId" : $contextId
}
}
}
""")
val response = parse(client.expectMessage()).rightValue.asObject.value
response("jsonrpc") shouldEqual Some("2.0".asJson)
response("id") shouldEqual Some(1.asJson)
val err = response("error").value.asObject.value
err("message") shouldEqual Some("Service error".asJson)
}
}

"capability/release" must {

"return an error response to the client when requesting it for `executionContext/canModify`" in {
val client = getInitialisedWsClient()
val contextId = createExecutionContext(client)
client.send(json"""
{ "jsonrpc": "2.0",
"method": "capability/release",
"id": 1,
"params": {
"method" : "executionContext/canModify",
"registerOptions": {
"contextId" : $contextId
}
}
}
""")
val response = parse(client.expectMessage()).rightValue.asObject.value
response("jsonrpc") shouldEqual Some("2.0".asJson)
response("id") shouldEqual Some(1.asJson)
val err = response("error").value.asObject.value
err("message") shouldEqual Some("Service error".asJson)
}
}

private def createExecutionContext(client: WsTestClient): UUID = {
client.send(ExecutionContextJsonMessages.executionContextCreateRequest(0))
val (requestId, contextId) =
runtimeConnectorProbe.receiveN(1).head match {
case Api.Request(requestId, Api.CreateContextRequest(contextId)) =>
(requestId, contextId)
case msg =>
fail(s"Unexpected message: $msg")
}

runtimeConnectorProbe.lastSender ! Api.Response(
requestId,
Api.CreateContextResponse(contextId)
)
client.expectJson(
ExecutionContextJsonMessages.executionContextCreateResponse(0, contextId)
)
contextId
}
}

0 comments on commit 61a0c8c

Please sign in to comment.