Skip to content
This repository has been archived by the owner on Mar 16, 2022. It is now read-only.

Commit

Permalink
Add initial node support TCK implementation (#503)
Browse files Browse the repository at this point in the history
  • Loading branch information
pvlugter authored Dec 22, 2020
1 parent 630c9e1 commit a7713c7
Show file tree
Hide file tree
Showing 17 changed files with 7,041 additions and 73 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
before_script:
- sbt update
- cd node-support && nvm install && nvm use && npm install && cd -
- cd samples/js-shopping-cart && nvm install && nvm use && npm install && cd -
- cd node-support/tck && nvm install && nvm use && npm install && cd -
script:
- sbt 'set concurrentRestrictions in Global += Tags.limitAll(1)' tck/it:test

Expand Down
4 changes: 3 additions & 1 deletion node-support/src/cloudstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,9 @@ class CloudState {
serviceVersion: this.options.serviceVersion,
serviceRuntime: process.title + " " + process.version,
supportLibraryName: packageInfo.name,
supportLibraryVersion: packageInfo.version
supportLibraryVersion: packageInfo.version,
protocolMajorVersion: 0,
protocolMinorVersion: 2
}
});
}
Expand Down
3 changes: 3 additions & 0 deletions node-support/tck/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/node_modules/
/proto/
/user-function.desc
1 change: 1 addition & 0 deletions node-support/tck/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
1 change: 1 addition & 0 deletions node-support/tck/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
v12
87 changes: 87 additions & 0 deletions node-support/tck/eventsourced.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2019 Lightbend Inc.
*
* Licensed 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
*
* http://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.
*/

const EventSourced = require("cloudstate").EventSourced;

const tckModel = new EventSourced(
["proto/eventsourced.proto"],
"cloudstate.tck.model.EventSourcedTckModel",
{
persistenceId: "event-sourced-tck-model",
snapshotEvery: 5
}
);

const Response = tckModel.lookupType("cloudstate.tck.model.Response")
const Persisted = tckModel.lookupType("cloudstate.tck.model.Persisted")

tckModel.initial = entityId => Persisted.create({ value: "" });

tckModel.behavior = state => {
return {
commandHandlers: {
Process: process
},
eventHandlers: {
Persisted: persisted
}
};
};

function process(request, state, context) {
request.actions.forEach(action => {
if (action.emit) {
const event = Persisted.create({ value: action.emit.value });
context.emit(event);
// FIXME: events are not emitted immediately, so we also update the function local state directly for responses
state = persisted(event, state);
} else if (action.forward) {
context.thenForward(two.service.methods.Call, { id: action.forward.id });
} else if (action.effect) {
context.effect(two.service.methods.Call, { id: action.effect.id }, action.effect.synchronous);
} else if (action.fail) {
context.fail(action.fail.message);
}
});
return Response.create(state.value ? { message: state.value } : {});
}

function persisted(event, state) {
state.value += event.value;
return state;
}

const two = new EventSourced(
["proto/eventsourced.proto"],
"cloudstate.tck.model.EventSourcedTwo"
);

two.initial = entityId => Persisted.create({ value: "" });

two.behavior = state => {
return {
commandHandlers: {
Call: call
}
};
};

function call(request) {
return Response.create({});
}

module.exports.tckModel = tckModel;
module.exports.two = two;
22 changes: 22 additions & 0 deletions node-support/tck/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* Copyright 2019 Lightbend Inc.
*
* Licensed 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
*
* http://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.
*/

const CloudState = require("cloudstate").CloudState;
const server = new CloudState();
const eventSourced = require("./eventsourced.js");
server.addEntity(eventSourced.tckModel);
server.addEntity(eventSourced.two);
server.start();
Loading

0 comments on commit a7713c7

Please sign in to comment.