-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Other: Added basic services test case
- Loading branch information
Showing
2 changed files
with
52 additions
and
24 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
}); | ||
}); | ||
|
||
}); |