Skip to content

Commit

Permalink
Other: Added basic services test case
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO committed Dec 26, 2016
1 parent b5a068f commit 93e04f1
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 24 deletions.
24 changes: 0 additions & 24 deletions tests/_support.js

This file was deleted.

52 changes: 52 additions & 0 deletions 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();
});
});

});

0 comments on commit 93e04f1

Please sign in to comment.