From 93e04f1db4a9ef3accff8d071c75be3d74c0cd4a Mon Sep 17 00:00:00 2001 From: dcodeIO Date: Mon, 26 Dec 2016 18:24:35 +0100 Subject: [PATCH] Other: Added basic services test case --- tests/_support.js | 24 ---------------------- tests/service.js | 52 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 24 deletions(-) delete mode 100644 tests/_support.js create mode 100644 tests/service.js diff --git a/tests/_support.js b/tests/_support.js deleted file mode 100644 index aff343a65..000000000 --- a/tests/_support.js +++ /dev/null @@ -1,24 +0,0 @@ -var tape = require("tape"); - -var protobuf = require(".."); - -function name(fn) { - if (!fn) - return "" + fn; - return fn.name; -} - -var ans = {}; -try { - Object.defineProperty(ans, "wer", { - get: function() { - return 42; - } - }); -} catch (e) {} - -tape.test("environment supports", function(test) { - test.ok(typeof Uint8Array !== "undefined", "Uint8Array"); - test.ok(ans.wer === 42, "getters/setters"); - test.end(); -}); \ No newline at end of file diff --git a/tests/service.js b/tests/service.js new file mode 100644 index 000000000..d1ded96c5 --- /dev/null +++ b/tests/service.js @@ -0,0 +1,52 @@ +var tape = require("tape"); + +var protobuf = require(".."); + +tape.test("services", function(test) { + + protobuf.load("tests/data/service.proto", function(err, root) { + if (err) + return test.fail(err.message); + + var myservice = root.lookup("myservice").resolveAll(), + MyService = myservice.MyService, + DoSomethingRequest = myservice.DoSomethingRequest, + DoSomethingResponse = myservice.DoSomethingResponse, + DoSomething = MyService.get("DoSomething"); + + function rpcImpl(method, requestData, callback) { + if (requestData) { + test.equal(method, DoSomething, "rpcImpl should reference the correct method"); + test.ok(callback, "rpcImpl should provide a callback"); + setTimeout(function() { + callback(null, DoSomethingResponse.create()); + }); + } else { + test.equal(method, null, "rpcImpl should not reference a method when closed"); + test.equal(callback, null, "rpcImpl should not provide a callback when closed"); + } + } + + var service = MyService.create(rpcImpl); + var dataEmitted = false; + service.on("data", function(responseData) { + dataEmitted = true; + test.ok(responseData, "should emit the data event"); + }); + var endCalled = false; + service.on("end", function() { + test.notOk(endCalled, "should not emit the end event twice"); + endCalled = true; + test.pass("should call the end event"); + service.end(); + test.end(); + }); + service.doSomething(DoSomethingRequest.create(), function(err, res) { + test.notOk(err, "should not raise an error"); + test.ok(res instanceof DoSomethingResponse.getCtor(), "should return a properly typed response"); + test.ok(dataEmitted, "should have emitted the data event"); + service.end(); + }); + }); + +}); \ No newline at end of file