Skip to content

Commit

Permalink
started process of debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
iangeckeler committed Aug 6, 2019
2 parents 155f915 + 640d420 commit 2ea8df4
Show file tree
Hide file tree
Showing 11 changed files with 301 additions and 190 deletions.
28 changes: 5 additions & 23 deletions __tests__/Stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ describe("Unit tests for Stub", () => {

const mockPort = "0.0.0.0:3000";
const stub = Stub(mockServiceDef, mockPort);
stub.handlerName({ message: "fakemessage" });
stub.handlerName()
.send({ message: "fakemessage" });
expect(jestMock.mock.calls.length).toBe(1);
});
});
Expand All @@ -109,26 +110,6 @@ describe("Tests for unaryCall", () => {
mockHandler.mockClear();
});

it("Promisifies Unary.", () => {
class mockServiceDef {
constructor(port, credentials) {}
HandlerName(...args) {
mockHandler(...args);
}
}
mockServiceDef.service = {
HandlerName: mockHandler
};

const mockPort = "0.0.0.0:3000";
const stub = Stub(mockServiceDef, mockPort);
const result = stub
.handlerName({ message: "hello" })
.then(res => {})
.catch(err => {});
expect(result).toBeInstanceOf(Promise);
});

it("Works on interceptor call.", () => {
class mockServiceDef {
constructor(port, credentials) {}
Expand All @@ -143,8 +124,9 @@ describe("Tests for unaryCall", () => {
const mockPort = "0.0.0.0:3000";
const stub = Stub(mockServiceDef, mockPort);
const result = stub
.handlerName({ message: "hello" }, [() => {}])
.then(res => {})
.handlerName({ meta: "data" }, [() => {}])
.send({ message: "hello" })
.on(res => {})
.catch(err => {});
expect(mockHandler.mock.calls.length).toBe(1);
});
Expand Down
8 changes: 4 additions & 4 deletions __tests__/clientCalls/serverStreamCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ beforeEach(() => {
});

describe("Tests for serverStreamCall.", () => {
it("Should throw when no message is supplied", () => {
xit("Should throw when no message is supplied", () => {
expect(() => {
serverStreamCall(that, "methodName");
}).toThrow();
});

it("Test mock with no args except for message.", () => {
xit("Test mock with no args except for message.", () => {
serverStreamCall(that, "methodName", { message: "Hello" });
expect(mockMethod.mock.calls.length).toBe(1);
});

it("Interceptor array in third position gets called in the right place.", () => {
xit("Interceptor array in third position gets called in the right place.", () => {
serverStreamCall(
that,
"methodName",
Expand All @@ -37,7 +37,7 @@ describe("Tests for serverStreamCall.", () => {
).toBeTruthy();
});

it("Meta object in the second position gets called in the right place.", () => {
xit("Meta object in the second position gets called in the right place.", () => {
serverStreamCall(that, "methodName", { message: "hello" }, [() => {}], {
meta: "value"
});
Expand Down
68 changes: 39 additions & 29 deletions examples/firecomm/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ const firecomm = require("../../index");

let certificate = path.join(__dirname, "/server.crt");

const stub = new firecomm.Stub(routeguide.RouteGuide, "localhost:3000", {
certificate
});
const stub = new firecomm.Stub(routeguide.RouteGuide, "localhost:3000");

// stub.openChannel('localhost:3000');

Expand Down Expand Up @@ -37,40 +35,52 @@ const firstChat = {

const { log: c } = console;

stub
.unaryChat(firstChat)
.then(res => console.log(res))
.catch(err => console.log(err));

const testUnaryChat = () => {
// console.log(stub.getChannel().getConnectivityState(true))

return stub.unaryChat(firstChat, { interceptors: [interceptorProvider] });
};
// stub.unaryChat({meta: 'data'}, [interceptorProvider])
// .send(firstChat)
// .on(res => console.log(res))
// .catch(err => console.log(err))
// unaryChat.on(res => console.log(res));
// unaryChat.catch(err => console.log(err));

// testUnaryChat()
// .then(data => console.log(data))
// .catch(err => console.error(err));

const testClientStream = () => {
const clientStream = stub.clientStream((err, res) => {
if (err) console.log(err);
console.log({ res });
});
clientStream.write(firstChat);
clientStream.end();
};
// stub.serverStream({meta: 'data'}, [interceptorProvider])
// // .send(firstChat)
// .on(res => console.log(res))
// .send({message: 'please'})
// .on(({message}) => console.log(message))
// // .catch(err => console.log(err))
// .catch(err => console.error(err))

// const clientStream = stub
// .clientStream()
// .on(res => console.log(res))
// .catch(err => console.error(err))
// .send({ message: "yolo" });

// setInterval(() => {
// clientStream.send(firstChat);
// },1000)

const oldClient = stub.clientStream((err, res) => {
if (err) console.error(err);
console.log(res);
});

oldClient.write({ message: "hello" });

// testClientStream();

const testServerStream = () => {
const serverStream = stub.serverStream(firstChat);
// const serverStream = stub.serverStream();
// serverStream.write({path: 'firstChat'});
serverStream.on("data", data => {
console.log("data::", data), " ///////////// ";
});
};
// const testServerStream = () => {
// const serverStream = stub.serverStream(firstChat);
// // const serverStream = stub.serverStream();
// // serverStream.write({path: 'firstChat'});
// serverStream.on("data", data => {
// console.log("data::", data), " ///////////// ";
// });
// };
// testServerStream();

const testBidiChat = () => {
Expand Down
20 changes: 14 additions & 6 deletions examples/firecomm/methodHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,31 @@ const grpc = require("grpc");
function unaryChat(ctx) {
// ctx.setStatus({'trailer': 'value'});
// ctx.throw(new Error('custom error message'));
console.log(ctx.metadata);
console.log(ctx.body);
// console.log(ctx.metadata);
// console.log(ctx.body);
// ctx.setTrailer({'hello': 'trailer'})
// ctx.setMeta({'hello': 'world'})
// ctx.send({message: 'what\'s up'});
throw new Error("uncaught error");
// throw new Error("uncaught error");
ctx.send({message: 'it works'})
}

function serverStream(context) {
context.write({ message: " World" });
// console.log(context);
let count = 0;
setInterval(() => {
count += 1;
context.write({ message: " World" + count })
}, 1000)
}

function clientStream(context) {
// console.log(context.__proto__);
// console.log('serverStream context: ', context);
// console.log(context.metadata, context.metaData);
context.on('data', data => {console.log(data)});
context.send({message: 'world'})
}
}


function bidiChat(context) {
Expand All @@ -40,6 +48,6 @@ function bidiChat(context) {
module.exports = {
unaryChat,
bidiChat,
clientStream,
serverStream,
clientStream
};
23 changes: 10 additions & 13 deletions examples/firecomm/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ const server = new firecomm.Server();

server.addService(
package.RouteGuide,
{ unaryChat: [waitFor, unaryChat], serverStream, clientStream, bidiChat },
context => {
console.log("inside of service level middleware");
},
(err, call) => {
console.log("error from error handler:", err);
console.log("call in error:", call);
call.send({ message: "BULLDOZE THROUGH ERRORS" });
}
{ unaryChat: unaryChat, serverStream, clientStream, bidiChat },
// context => {
// console.log("inside of service level middleware");
// },
// (err, call) => {
// console.log("error from error handler:", err);
// console.log("call in error:", call);
// call.send({ message: "BULLDOZE THROUGH ERRORS" });
// }
);

// console.log({ server });
Expand All @@ -42,10 +42,7 @@ server.addService(

// {private_key: (__dirname + '/server.crt'), certificate: (__dirname +
// '/server.key')}
server.bind("0.0.0.0:3000", {
privateKey: __dirname + "/server.key",
certificate: __dirname + "/server.crt"
});
server.bind("0.0.0.0:3000");
// console.log({server})
// console.log(new grpc.Server().__proto__)

Expand Down
72 changes: 12 additions & 60 deletions lib/Stub.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,90 +56,42 @@ module.exports = function Stub(serviceDefinition, port, config) {
methodName[0].toLowerCase() + methodName.substring(1);

if (methodType === "Unary") {
// 'message' could be *any* kind of data that fits the *.proto file
// 'message' will always be an object with *any* kind of data as properties that fit the .proto file
// configuation
this[lowerCaseName] = function(
message,
interceptorArray,
metaObject,
callback
interceptorArray,
) {
const that = this;
return unaryCall(
that,
methodName,
message,
interceptorArray,
metaObject,
callback
interceptorArray,
);
};
// let metadata = undefined;
// let interceptors = undefined;
// if (typeof message !== "object") {
// throw new Error(
// "First parameter required and must be of type: Message."
// );
// }
// //ultimately needs to get message, metadata, interceptors...
// //and callback
// if (callback === undefined) {
// return new Promise((resolve, reject) => {
// that[methodName](
// message,
// metadata,
// interceptors,
// (err, response) => {
// if (err) {
// reject(err);
// }
// resolve(response);
// }
// );
// });
// } else {
// that[methodName](message, metadata, interceptors, callback);
// }
// };
} else if (methodType === "ClientStream") {
this[lowerCaseName] = function(metaObject, interceptorArray, callback) {
this[lowerCaseName] = function(
metaObject,
interceptorArray
) {
const that = this;
return clientStreamCall(
that,
methodName,
interceptorArray,
metaObject,
callback
interceptorArray,
);
// if (typeof metaObject === "function") {
// // if first argument is a function, treat it as a callback
// return this[methodName](undefined, undefined, metaObject);
// } else if (
// typeof metaObject === "object" &&
// !Array.isArray(metaObject) &&
// typeof interceptorArray === "function"
// ) {
// const metadata = generateMeta(metaObject);
// return this[methodName](metadata, undefined, interceptorArray);
// } else if (
// typeof metaObject === "object" &&
// Array.isArray(metaObject) &&
// typeof interceptorArray === "function"
// ) {
// const interceptors = { interceptors: interceptorArray };
// return this[methodName](undefined, interceptors, callback);
// }
// const metadata = generateMeta(metaObject);
// const interceptors = { interceptors: interceptorArray };
// return this[methodName](metadata, interceptors, callback);
};
} else if (methodType === "ServerStream") {
this[lowerCaseName] = function(message, metaObject, interceptorArray) {
this[lowerCaseName] = function(
metaObject,
interceptorArray
) {
const that = this;
return serverStreamCall(
that,
methodName,
message,
metaObject,
interceptorArray
);
Expand Down
11 changes: 11 additions & 0 deletions lib/callFactories/generateUnaryCall.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,16 @@
const grpc = require("grpc");

// function serverUnaryDecorator(callClone, serverCall, callback) {
// callClone...
// }
// callClone.throw (err, trailers)
// check for err to be eror objects
// converts trailers to grpc Metadata class.
// passes them in the right place to callback
// callClone.on = ("data" |/& callback) => callback(callClone.body)
// callClone.set()
// calllClone.body = serverCall.request

function generateUnaryCall(ServerUnaryCall, callback) {
const ServerUnaryCallClone = Object.create(ServerUnaryCall);
ServerUnaryCallClone.callback = callback;
Expand Down

0 comments on commit 2ea8df4

Please sign in to comment.