Skip to content

Commit

Permalink
add test for limitation of schema definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
endel committed Jul 14, 2020
1 parent 788db59 commit 0ca448c
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 25 deletions.
44 changes: 22 additions & 22 deletions benchmark/FossilDeltaComparison.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ console.log("(@colyseus/state) INITIAL STATE SIZE:", encoded.length);
console.log("(msgpack) INITIAL STATE SIZE:", firstStateEncoded.length);
console.log("");

{
// CHANGE X/Y OF A SINGLE ENTITY
const x = Math.floor(Math.random() * 200);
const y = Math.floor(Math.random() * 200);
state.players[fixedUnitId].x = x
state.players[fixedUnitId].y = y
msgpackState.players[fixedUnitId].x = x
msgpackState.players[fixedUnitId].y = y
}
// {
// // CHANGE X/Y OF A SINGLE ENTITY
// const x = Math.floor(Math.random() * 200);
// const y = Math.floor(Math.random() * 200);
// state.players[fixedUnitId].x = x
// state.players[fixedUnitId].y = y
// msgpackState.players[fixedUnitId].x = x
// msgpackState.players[fixedUnitId].y = y
// }

// {
// // CHANGE X/Y OF 50 ENTITIES
Expand All @@ -79,19 +79,19 @@ console.log("");
// }
// }

// {
// // CHANGE ALL X/Y VALUES
// let i = 0;
// for (let id in msgpackState.players) {
// const x = Math.floor(Math.random() * 200);
// const y = Math.floor(Math.random() * 200);
// state.players[id].x = x
// state.players[id].y = y
// msgpackState.players[id].x = x
// msgpackState.players[id].y = y
// i++;
// }
// }
{
// CHANGE ALL X/Y VALUES
let i = 0;
for (let id in msgpackState.players) {
const x = Math.floor(Math.random() * 200);
const y = Math.floor(Math.random() * 200);
state.players[id].x = x
state.players[id].y = y
msgpackState.players[id].x = x
msgpackState.players[id].y = y
i++;
}
}

encoded = state.encode();
decodedState.decode(encoded);
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@colyseus/schema",
"version": "1.0.0-alpha.9",
"version": "1.0.0-alpha.10",
"description": "Schema-based binary serializer / de-serializer.",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
50 changes: 49 additions & 1 deletion test/EdgeCasesTest.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,58 @@
import * as assert from "assert";
import * as nanoid from "nanoid";
import { MapSchema, Schema, type, ArraySchema, dumpChanges } from "../src";
import { MapSchema, Schema, type, ArraySchema, dumpChanges, defineTypes, Reflection, Context } from "../src";

import { State, Player } from "./Schema";

describe("Edge cases", () => {
it("Schema should support more than 256 fields", () => {
const maxFields = 1000;
class State extends Schema {};

const schema = {};
for (let i = 0; i < maxFields; i++) { schema[`field_${i}`] = "string"; }
defineTypes(State, schema);

const state = new State();
for (let i = 0; i < maxFields; i++) { state[`field_${i}`] = "value " + i; }

const decodedState = Reflection.decode(Reflection.encode(state));
decodedState.decode(state.encode());

for (let i = 0; i < maxFields; i++) {
assert.equal("value " + i, decodedState[`field_${i}`]);
}
});

it("Should support up to 255 schema types", () => {
const maxSchemaTypes = 255;
const context = new Context();
const type = Context.create(context);

class State extends Schema {}

const schemas: any = {};

for (let i = 0; i < maxSchemaTypes; i++) {
class Child extends Schema { @type("string") str: string; };
schemas[i] = Child;
defineTypes(State, { [`field_${i}`]: Child, }, context);
}

const state = new State();
for (let i = 0; i < maxSchemaTypes; i++) {
const child = new schemas[i]();
child.str = "value " + i;
state[`field_${i}`] = child;
}

const decodedState = Reflection.decode(Reflection.encode(state));
decodedState.decode(state.encode());

for (let i = 0; i < maxSchemaTypes; i++) {
assert.equal("value " + i, decodedState[`field_${i}`].str);
}
});

it("NIL check should not collide", () => {
class State extends Schema {
Expand Down
2 changes: 1 addition & 1 deletion test/NextIterationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -670,7 +670,7 @@ describe("Next Iteration", () => {
player1.name = "This field should not be encoded!";

const encoded = state.encode();
console.log("LAST ENCODE (THIS SHOULD BE EMPTY) =>", encoded.length, encoded);
assert.equal(0, encoded.length);
});

});
Expand Down

0 comments on commit 0ca448c

Please sign in to comment.