-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #900 from Bhashinee/constraint
Implement dispatching to custom remote functions
- Loading branch information
Showing
17 changed files
with
394 additions
and
13 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
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,189 @@ | ||
// Copyright (c) 2023 WSO2 LLC. (//www.wso2.org) All Rights Reserved. | ||
// | ||
// WSO2 Inc. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// //www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/test; | ||
import ballerina/io; | ||
|
||
listener Listener customDispatchingLis = new(21401); | ||
|
||
@ServiceConfig {dispatcherKey: "event"} | ||
service on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new SingleMessageService(); | ||
} | ||
} | ||
|
||
service class SingleMessageService { | ||
*Service; | ||
remote function onPing(Caller caller, json data) returns Error? { | ||
io:println(data); | ||
check caller->writeMessage({"event": "pong"}); | ||
} | ||
} | ||
|
||
@ServiceConfig {dispatcherKey: "event"} | ||
service /subscribe on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new SubscribeMessageService(); | ||
} | ||
} | ||
|
||
service class SubscribeMessageService { | ||
*Service; | ||
remote function onSubscribe(Caller caller, json data) returns Error? { | ||
io:println(data); | ||
check caller->writeMessage({"type": "subscribe", "id":"1", "payload":{"query": "{ __schema { types { name } } }"}}); | ||
} | ||
|
||
remote function onHeartbeat(Caller caller, json data) returns json { | ||
return {"event": "heartbeat"}; | ||
} | ||
} | ||
|
||
@ServiceConfig {dispatcherKey: "event"} | ||
service /onMessage on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new DefaultRemoteFunctionService(); | ||
} | ||
} | ||
|
||
service class DefaultRemoteFunctionService { | ||
*Service; | ||
remote function onMessage(Caller caller, json data) returns Error? { | ||
check caller->writeMessage({"event": "heartbeat"}); | ||
} | ||
} | ||
|
||
@ServiceConfig {dispatcherKey: "event"} | ||
service /noRemoteMethod on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new NoRemoteFunctionService(); | ||
} | ||
} | ||
|
||
service class NoRemoteFunctionService { | ||
*Service; | ||
remote function onMessages(Caller caller, json data) returns Error? { | ||
check caller->writeMessage({"event": "onMessages"}); | ||
} | ||
} | ||
|
||
@ServiceConfig {dispatcherKey: "event"} | ||
service /dataBindingFailure on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new DataBindingFailureService(); | ||
} | ||
} | ||
|
||
service class DataBindingFailureService { | ||
*Service; | ||
remote function onMessages(Caller caller, byte[] data) returns Error? { | ||
check caller->writeMessage({"event": "onMessages"}); | ||
} | ||
} | ||
|
||
@ServiceConfig {dispatcherKey: "type"} | ||
service /underscore on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new UnderscoreService(); | ||
} | ||
} | ||
|
||
service class UnderscoreService { | ||
*Service; | ||
remote function onPing(Caller caller, json data) returns Error? { | ||
check caller->writeMessage({"event": "onMessages"}); | ||
} | ||
} | ||
|
||
@ServiceConfig {dispatcherKey: "type"} | ||
service /lotofunderscores on customDispatchingLis { | ||
resource function get .() returns Service|Error { | ||
return new LotOfUnderscoreService(); | ||
} | ||
} | ||
|
||
service class LotOfUnderscoreService { | ||
*Service; | ||
remote function onThisPingMessage(Caller caller, json data) returns Error? { | ||
check caller->writeMessage({"event": "onMessages"}); | ||
} | ||
} | ||
|
||
@test:Config {} | ||
public function testPingMessage() returns Error? { | ||
Client cl = check new("ws://localhost:21401"); | ||
check cl->writeMessage({"event": "ping"}); | ||
json resp = check cl->readMessage(); | ||
test:assertEquals(resp, {"event": "pong"}); | ||
} | ||
|
||
@test:Config {} | ||
public function testSubscribeMessage() returns Error? { | ||
Client cl = check new("ws://localhost:21401/subscribe"); | ||
check cl->writeMessage({"event": "subscribe", "pair": ["XBT/USD", "XBT/EUR"], "subscription": {"name": "ticker"}}); | ||
json resp = check cl->readMessage(); | ||
test:assertEquals(resp, {"type": "subscribe", "id":"1", "payload":{"query": "{ __schema { types { name } } }"}}); | ||
check cl->writeMessage({"event": "heartbeat"}); | ||
json resp2 = check cl->readMessage(); | ||
test:assertEquals(resp2, {"event": "heartbeat"}); | ||
} | ||
|
||
@test:Config {} | ||
public function testDispatchingToDefaultRemoteMethod() returns Error? { | ||
Client cl = check new("ws://localhost:21401/onMessage"); | ||
check cl->writeMessage({"event": "heartbeat"}); | ||
check cl->writeMessage({"event": "heartbeat"}); | ||
json resp2 = check cl->readMessage(); | ||
test:assertEquals(resp2, {"event": "heartbeat"}); | ||
} | ||
|
||
@test:Config {} | ||
public function testDispatchingToNone() returns Error? { | ||
Client cl = check new("ws://localhost:21401/noRemoteMethod"); | ||
check cl->writeMessage({"event": "heartbeat"}); | ||
check cl->writeMessage({"event": "Messages"}); | ||
json resp2 = check cl->readMessage(); | ||
test:assertEquals(resp2, {"event": "onMessages"}); | ||
} | ||
|
||
@test:Config {} | ||
public function testDatabindingFailure() returns Error? { | ||
Client cl = check new("ws://localhost:21401/dataBindingFailure"); | ||
check cl->writeMessage({"event": "Messages"}); | ||
json|Error resp2 = cl->readMessage(); | ||
if resp2 is Error { | ||
test:assertTrue(resp2.message().startsWith("data binding failed:")); | ||
} else { | ||
test:assertFail("Expected a binding error"); | ||
} | ||
} | ||
|
||
@test:Config {} | ||
public function testUnderscore() returns Error? { | ||
Client cl = check new("ws://localhost:21401/underscore"); | ||
check cl->writeMessage({"type": "_ping"}); | ||
json resp = check cl->readMessage(); | ||
test:assertEquals(resp, {"event": "onMessages"}); | ||
} | ||
|
||
@test:Config {} | ||
public function testUnderscoresAndSpaces() returns Error? { | ||
Client cl = check new("ws://localhost:21401/lotofunderscores"); | ||
check cl->writeMessage({"type": "this_ping message"}); | ||
json resp = check cl->readMessage(); | ||
test:assertEquals(resp, {"event": "onMessages"}); | ||
} |
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
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
4 changes: 4 additions & 0 deletions
4
compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_57/Ballerina.toml
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,4 @@ | ||
[package] | ||
org = "websocket_test" | ||
name = "sample_57" | ||
version = "0.1.0" |
40 changes: 40 additions & 0 deletions
40
compiler-plugin-tests/src/test/resources/ballerina_sources/sample_package_57/server.bal
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,40 @@ | ||
// Copyright (c) 2023 WSO2 LLC. (www.wso2.com) All Rights Reserved. | ||
// | ||
// WSO2 LLC. licenses this file to you under the Apache License, | ||
// Version 2.0 (the "License"); you may not use this file except | ||
// in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// //www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, | ||
// software distributed under the License is distributed on an | ||
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
// KIND, either express or implied. See the License for the | ||
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
import ballerina/websocket; | ||
|
||
listener websocket:Listener localListener = new(8080); | ||
|
||
@websocket:ServiceConfig {dispatcherKey: "type"} | ||
service / on localListener { | ||
resource function get .() returns websocket:Service|error { | ||
return new WsService(); | ||
} | ||
} | ||
|
||
service class WsService { | ||
*websocket:Service; | ||
|
||
remote isolated function onOpen123(websocket:Caller caller) returns stream<string>|error { | ||
string[] greets = ["Hi Sam", "Hey Sam", "GM Sam"]; | ||
return greets.toStream(); | ||
} | ||
|
||
remote isolated function onMessagexxx(json data) returns stream<string, error?>|error { | ||
string[] greets = ["Hi Sam", "Hey Sam", "GM Sam"]; | ||
return greets.toStream(); | ||
} | ||
} |
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
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
Oops, something went wrong.